-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.c
More file actions
74 lines (56 loc) · 818 Bytes
/
Copy pathGraph.c
File metadata and controls
74 lines (56 loc) · 818 Bytes
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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int n;
struct node* next;
};
struct list
{
struct node* head;
};
struct Graph
{
int V;
struct list* array;
};
struct Graph create(int V)
{
struct Graph g;
g.V=V;
g.array=(struct list*)malloc(V*sizeof(struct list*));
int i;
for(i=0;i<V;i++)
g.array[i].head=NULL;
return g;
}
void addEdge(struct Graph g,int s,int d)
{
struct node* x=malloc(sizeof(struct node*));
(*x).n=d;
(*x).next=g.array[s].head;
g.array[s].head=x;
}
void display(struct Graph g)
{
int i;
for(i=0;i<g.V;i++)
{
printf("%d\t",i);
struct node* x=g.array[i].head;
while(x!=NULL)
{
printf("%d ",(*x).n);
x=(*x).next;
}
printf("\n");
}
}
int main()
{
struct Graph g=create(3);
addEdge(g,1,2);
addEdge(g,0,1);
addEdge(g,1,0);
display(g);
}