-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion8.c
104 lines (91 loc) · 1.88 KB
/
question8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Program to implement a simple graph
METHOD1: linked list
METHOD2: arrays
*/
//METHOD1
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct graph{
int count;
struct node *head;
};
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->next = NULL;
return temp;
}
void addEdge(struct graph *myGraph,int src, int dest){
struct node *temp;
temp = newNode(dest);
if(!myGraph[src].head){
myGraph[src].head = temp;
}else{
temp->next = myGraph[src].head;
myGraph[src].head = temp;
}
myGraph[src].count++;
temp = newNode(src);
if(!myGraph[dest].head){
myGraph[dest].head = temp;
}else{
temp->next = myGraph[dest].head;
myGraph[dest].head = temp;
}
myGraph[dest].count++;
}
void printGraph(struct graph *myGraph){
printf("graph is...\n");
for(int i=0; i<4;i++){
struct node *temp = myGraph[i].head;
printf("%d-->", i);
while(temp){
printf("%d --> ", temp->data);
temp = temp->next;
}
printf("\n");
}
}
int main(){
int size = 4;
struct graph *myGraph = (struct graph *)calloc(size,sizeof(struct graph));
addEdge(myGraph, 0, 1);
addEdge(myGraph, 1, 2);
addEdge(myGraph, 1, 3);
addEdge(myGraph, 2, 3);
printGraph(myGraph);
return 0;
}
//====================================================================================================
//METHOD2
#include <stdio.h>
#include <stdlib.h>
void addEdge(int arr[][4],int src, int dest){
arr[src][dest] = arr[dest][src] = 1;
}
void printGraph(int arr[][4]){
for(int i=0;i<4;i++){
printf("%d --> ", i);
for(int j=0;j<4;j++){
if(arr[i][j]){
printf("%d --> ", j);
}
}
printf("\n");
}
}
int main(){
int size = 4;
int graph[4][4] = {0};
addEdge(graph, 0, 1);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 3);
printGraph(graph);
return 0;
}