-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRAPH.BAK
79 lines (76 loc) · 1.24 KB
/
GRAPH.BAK
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
#include<stdio.h>
#include<conio.h>
struct node
{
int dest;
struct node *next;
} ;
struct node *a[10];
struct list
{
struct node *head;
};
struct graph
{
int v;
struct list *array;
};
struct node *c_node(int dest)
{
struct node *temp=(struct node*) malloc(sizeof(struct node));
temp->dest=dest;
temp->next=NULL;
return temp;
}
struct graph* create_g(int v)
{ int i;
struct graph *g=(struct graph*) malloc(sizeof(struct graph));
g->v=v;
g->array=(struct list*)malloc(v *sizeof(struct list));
for( i=0;i<v;i++)
{
g->array[i].head=NULL;
}
return g;
}
void addedge(struct graph *g ,int src,int dest)
{//undirected graph
struct node *temp=c_node(dest);
temp->next=g->array[src].head;
g->array[src].head=temp;
temp=c_node(src);
temp->next=g->array[dest].head;
g->array[dest].head=temp;
}
void print(struct graph *g)
{
int x;
for(x=0;x<g->v;++x)
{
struct node *p=g->array[x].head;
printf("Adjecency list of vertex",x);
while(p)
{
printf("->%d",p->dest);
p=p->next;
}
printf("\n");
}
}
void main()
{
int v,i,a,b;
struct graph *g;
clrscr();
printf("enter no of nodes");
scanf("%d",&v);
g=create_g(v);
for(i=0;i<v;i++)
{
printf("enter values of node & its addjacent");
scanf("%d%d",&a,&b);
addedge(g,a,b);
}
print(g);
getch();
}