-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjacency List
More file actions
56 lines (55 loc) · 1.01 KB
/
Adjacency List
File metadata and controls
56 lines (55 loc) · 1.01 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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int vertexNumber;
struct Node *next;
};
struct Graph
{
struct Node **Adj;
};
struct Graph *createList();
int main()
{
struct Graph *G=createList();
return 0;
}
struct Graph *createList()
{
int i,j;
int vertices,edges;
struct Graph *G=(struct Graph *)malloc(sizeof(struct Graph));
if(!G)
{
printf("Something went wrong....!\n");
return NULL;
}
struct Node *temp;
printf("Enter vertices: ");
scanf("%d",&vertices);
printf("Enter edges: ");
scanf("%d",&edges);
G->Adj=malloc (vertices * sizeof(struct Node));
for(i=0;i<vertices;i++)
{
G->Adj[i]=(struct Node *) malloc (sizeof(struct Node));
G->Adj[i]->vertexNumber=i;
G->Adj[i]->next=G->Adj[i];
}
printf("Reading edge data........\n");
int u,v;
for(i=0;i<edges;i++)
{
scanf("%d %d",&u,&v);
temp=(struct Node *) malloc (sizeof(struct Node));
temp->vertexNumber=v;
temp->next=G->Adj[u];
G->Adj[u]->next=temp;
temp=(struct Node *) malloc (sizeof(struct Node));
temp->vertexNumber=v;
temp->next=G->Adj[v];
G->Adj[v]->next=temp;
}
return G;
}