mirror of
https://github.com/Hizenberg469/C1-Linux_SYS_Prog-AS-.git
synced 2026-04-19 18:32:24 +03:00
Assignment2
This commit is contained in:
47
Assignment2/array_iterator.c
Normal file
47
Assignment2/array_iterator.c
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: array_iterator.c
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 07/02/24 07:51:54 PM IST
|
||||
* Revision: none
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: YOUR NAME (),
|
||||
* Organization:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
|
||||
#define ITERATE_ARRAY_BEGIN(arrptr, array_size, start_index, scansize, out_index) { \
|
||||
unsigned int cnt; \
|
||||
for( cnt = 0, out_index = start_index ; cnt < scansize ; cnt++ , out_index = (out_index+1)%array_size ){
|
||||
#define ITERATE_ARRAY_END }}
|
||||
|
||||
|
||||
int
|
||||
|
||||
main(int argc, char **argv){
|
||||
|
||||
unsigned int arr[10] = {1,2,3,4,5,6,7,8,9,10};
|
||||
|
||||
unsigned int i;
|
||||
|
||||
ITERATE_ARRAY_BEGIN(arr, 10, 5, 10, i) {
|
||||
|
||||
printf("arr[%u] = %u\n", i, arr[i]);
|
||||
|
||||
} ITERATE_ARRAY_END;
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
141
Assignment2/exercise2q.c
Normal file
141
Assignment2/exercise2q.c
Normal file
@@ -0,0 +1,141 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_NBRS 5
|
||||
#define NAME_SIZE 32
|
||||
|
||||
typedef struct node_{
|
||||
|
||||
char node_name[NAME_SIZE];
|
||||
struct node_ *nbrs[MAX_NBRS];
|
||||
} node_t ;
|
||||
|
||||
void
|
||||
add_nbr(node_t *node1, node_t *node2){
|
||||
|
||||
int i = 0;
|
||||
for(; i < MAX_NBRS; i++){
|
||||
if(node1->nbrs[i]) continue;
|
||||
node1->nbrs[i] = node2;
|
||||
return;
|
||||
}
|
||||
/*Do not try to add more than MAX_NBRS*/
|
||||
assert(0);
|
||||
}
|
||||
|
||||
node_t *
|
||||
create_new_node(char *name){
|
||||
|
||||
node_t *node = calloc(1, sizeof(node_t));
|
||||
strncpy(node->node_name, name, NAME_SIZE);
|
||||
node->node_name[NAME_SIZE] = '\0';
|
||||
/*rest of the members is already NULL as
|
||||
* we have used calloc*/
|
||||
return node;
|
||||
}
|
||||
|
||||
#define ITERATE_NBRS_BEGIN(node, nbrnode) \
|
||||
{ \
|
||||
for( unsigned int i = 0 ; i < 5 && node->nbrs[i] != NULL ; i++ ){ \
|
||||
nbrnode = node->nbrs[i];
|
||||
/*<provide your implementation here>*/
|
||||
|
||||
#define ITERATE_NBRS_END }}
|
||||
/*<provide your implementation here>*/
|
||||
|
||||
#define ITERATE_ONE_HOP_NBRS_BEGIN(node, nbrnode) \
|
||||
{ \
|
||||
for( unsigned int j = 0 ; j < 5 && node->nbrs[j] != NULL ; j++ ){ \
|
||||
for( unsigned int i = 0 ; i < 5 && node->nbrs[j]->nbrs[i] != NULL ; i++ ){ \
|
||||
if( node->nbrs[j]->nbrs[i] == node ) continue; \
|
||||
nbrnode = node->nbrs[j]->nbrs[i];
|
||||
|
||||
/*<provide your implementation here>*/
|
||||
|
||||
#define ITERATE_ONE_HOP_NBRS_END }}}
|
||||
/*<provide your implementation here>*/
|
||||
|
||||
int
|
||||
main(int argc, char **argv){
|
||||
|
||||
/*Problem 2 : In thie Question, I have created a graph as shown in the diagram below.*/
|
||||
|
||||
/*Let us create the following graph now*/
|
||||
/*
|
||||
A-------------B------------C---------H
|
||||
| | |
|
||||
| | |
|
||||
| | |
|
||||
| | |
|
||||
| | |
|
||||
E-------------D------------F---------G
|
||||
*/
|
||||
|
||||
/*First I have created the nodes of the graph*/
|
||||
node_t *A = create_new_node("A");
|
||||
node_t *B = create_new_node("B");
|
||||
node_t *C = create_new_node("C");
|
||||
node_t *D = create_new_node("D");
|
||||
node_t *E = create_new_node("E");
|
||||
node_t *F = create_new_node("F");
|
||||
node_t *G = create_new_node("G");
|
||||
node_t *H = create_new_node("H");
|
||||
|
||||
|
||||
/*Now i am setting up the edges connecting two nodes of the graph using function add_nbr().
|
||||
* Result is - Graph will be created exactly same as drawn above*/
|
||||
add_nbr(A,B);
|
||||
add_nbr(A,E);
|
||||
|
||||
add_nbr(B,A);
|
||||
add_nbr(B,D);
|
||||
add_nbr(B,C);
|
||||
|
||||
add_nbr(C,B);
|
||||
add_nbr(C,F);
|
||||
add_nbr(C,H);
|
||||
|
||||
add_nbr(D,B);
|
||||
add_nbr(D,E);
|
||||
add_nbr(D,F);
|
||||
|
||||
add_nbr(E,A);
|
||||
add_nbr(E,D);
|
||||
|
||||
add_nbr(F,C);
|
||||
add_nbr(F,D);
|
||||
add_nbr(F,G);
|
||||
|
||||
add_nbr(G,F);
|
||||
|
||||
add_nbr(H,C);
|
||||
|
||||
node_t *nbr = NULL;
|
||||
printf("Nbrs of A : \n");
|
||||
ITERATE_NBRS_BEGIN(A, nbr){
|
||||
|
||||
printf("Nbr name = %s\n", nbr->node_name);
|
||||
} ITERATE_NBRS_END;
|
||||
|
||||
printf("Nbrs of H : \n");
|
||||
ITERATE_NBRS_BEGIN(H, nbr){
|
||||
|
||||
printf("Nbr name = %s\n", nbr->node_name);
|
||||
} ITERATE_NBRS_END;
|
||||
|
||||
printf("Nbrs of D : \n");
|
||||
ITERATE_NBRS_BEGIN(D, nbr){
|
||||
|
||||
printf("Nbr name = %s\n", nbr->node_name);
|
||||
} ITERATE_NBRS_END;
|
||||
|
||||
printf("One hop Nbrs of A : \n");
|
||||
ITERATE_ONE_HOP_NBRS_BEGIN(A, nbr){
|
||||
|
||||
printf("Nbr name = %s\n", nbr->node_name);
|
||||
} ITERATE_ONE_HOP_NBRS_END;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user