-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacency.c
More file actions
64 lines (62 loc) · 1.29 KB
/
Copy pathAdjacency.c
File metadata and controls
64 lines (62 loc) · 1.29 KB
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int vertex;
struct node* next;
}node;
typedef struct graph
{
int numVert;
node** adjlist;
}graph;
node* createNode(int v)
{
node* NewNode=malloc(sizeof(node));
NewNode->vertex=v;
NewNode->next=NULL;
return NewNode;
}
graph* createGraph(int verticies)
{
graph* NewGraph=malloc(sizeof(graph));
NewGraph->numVert=verticies;
NewGraph->adjlist=malloc(verticies*sizeof(node *));
for(int i=0;i<verticies;i++)
{
NewGraph->adjlist[i]=NULL;
}
return NewGraph;
}
void Add(graph* graph,int s,int d)
{
node* newNode = createNode(d);
newNode->next = graph->adjlist[s];
graph->adjlist[s] = newNode;
newNode = createNode(s);
newNode->next = graph->adjlist[d];
graph->adjlist[d] = newNode;
}
void print(graph* graph)
{
for(int i=0;i<graph->numVert;i++)
{
node* tmp=graph->adjlist[i];
printf("\nVertex %d\n",i);
while (tmp)
{
printf("%d ->",tmp->vertex);
tmp=tmp->next;
}
printf("\n");
}
}
int main(void)
{
graph* graph=createGraph(4);
Add(graph,0,1);
Add(graph,0,2);
Add(graph,0,3);
Add(graph,1,2);
print(graph);
}