-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
149 lines (140 loc) · 3.51 KB
/
graph.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
// Graphs
struct edge
{
int edata;
struct edge *next;
};
struct vertex
{
int vdata;
struct vertex *vlink;
struct edge *elink;
};
struct vertex *create()
{
struct vertex *G=(struct vertex *)malloc(sizeof(struct vertex));
printf("Enter first vertex value: ");
scanf("%d", &G->vdata);
G->vlink=NULL;
G->elink=NULL;
return G;
}
void addvertex(struct vertex *G, int Vi)
{
struct vertex *a=G;
for(;a->vlink!=NULL;)
a=a->vlink;
struct vertex *nv=(struct vertex *)malloc(sizeof(struct vertex));
nv->vdata=Vi;
a->vlink=nv;
nv->vlink=NULL;
nv->elink=NULL;
}
void addedge(struct vertex *G, int Vi, int Vj)
{
struct vertex *a=G;
for(;a->vdata!=Vi;)
a=a->vlink;
if(a->vdata==Vi)
{struct edge *b=a->elink;
for(;b!=NULL;)
b=b->next;
struct edge *ne=(struct edge *)malloc(sizeof(struct edge));
ne->edata=Vj;
b=ne;
ne->next=NULL;
}
else
printf("Vertx not valid.");
}
void findvertex(struct vertex *G, int Vi)
{
struct vertex *a=G;
for(;a->vlink->vdata!=Vi;)
a=a->vlink;
printf("man");
if(a->vlink==NULL)
printf("Vertex not available.\n");
else
printf("Vertex available.\n");
}
void findedge(struct vertex *G, int Vi, int Vj)
{
struct vertex *a=G;
for(;a->vlink->vdata!=Vi;a=a->vlink);
if(a->vlink==NULL)
printf("Vertex not available.\n");
else
{
struct edge *b=a->elink;
for(;b->next->edata!=Vj;b=b->next);
if(b->next==NULL)
printf("Edge not available.\n");
else
printf("Edge is available.\n");
}
}
void traverse(struct vertex *G, int Vi, int Vj)
{
struct vertex *a=G;
for(;a->vlink!=NULL;a=a->vlink)
{
printf("\n%d: ", a->vdata);
struct edge *b=a->elink;
for(;b->next!=NULL;b=b->next)
printf("%d ", b->edata);
}
}
void main()
{
int x=0, Vi, Vj;
struct vertex *G=NULL;
printf("Hello!\nPress 1: Create an Empty Graph\nPress 2: Add a Vertex\nPress 3: Add an Edge\nPress 4: Find if Vertex Exists\nPress 5: Find if Edge Exists\nPress 6: Graph Traversal\n");
for(;1;)
{
printf("Enter your choice: ");
scanf("%d", &x);
switch(x)
{
case 1:
G= create();
break;
case 2:
printf("Enter new vertex value: ");
scanf("%d", &Vi);
addvertex(G, Vi);
break;
case 3:
printf("Enter parent vertex value: ");
scanf("%d", &Vi);
printf("Enter new edge value: ");
scanf("%d", &Vj);
addedge(G, Vi,Vj);
break;
case 4:
printf("Enter vertex value: ");
scanf("%d", &Vi);
findvertex(G, Vi);
break;
case 5:
printf("Enter parent vertex value: ");
scanf("%d", &Vi);
printf("Enter edge value: ");
scanf("%d", &Vj);
findedge(G, Vi, Vj);
break;
case 6:
printf("Enter from node value: ");
scanf("%d", &Vi);
printf("Enter to node value: ");
scanf("%d", &Vj);
traverse(G, Vi, Vj);
break;
default:
printf("Invalid Input.");
break;
}
}
}