-
Notifications
You must be signed in to change notification settings - Fork 0
/
node insertion and deletion in graph.cpp
272 lines (253 loc) · 4.84 KB
/
node insertion and deletion in graph.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <malloc.h>
struct edge;
struct node
{
struct node *next;
char name;
struct edge *adj;
}*start=NULL;
struct edge
{
char dest;
struct edge *link;
};
struct node *find(char item);
void insert_node(char node_name)
{
struct node *tmp,*ptr;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->name=node_name;
tmp->next=NULL;
tmp->adj=NULL;
if(start==NULL)
{
start=tmp;
return;
}
ptr=start;
while( ptr->next!=NULL)
ptr=ptr->next;
ptr->next=tmp;
}/*End of insert_node()*/
void delete_node(char u)
{
struct node *tmp,*q;
if(start->name == u)
{
tmp=start;
start=start->next; /* first element deleted */
free(tmp);
return;
}
q=start;
while(q->next->next != NULL)
{
if(q->next->name==u) /* element deleted in between */
{
tmp=q->next;
q->next=tmp->next;
free(tmp);
return;
}
q=q->next;
}/*End of while*/
if(q->next->name==u) /* last element deleted */
{
tmp=q->next;
free(tmp);
q->next=NULL;
}
}/*End of delete_node()*/
void delnode_edge(char u)
{
struct node *ptr;
struct edge *q,*start_edge,*tmp;
ptr=start;
while(ptr!=NULL)
{
/* ptr->adj points to first node of edge linked list */
if(ptr->adj->dest == u)
{
tmp=ptr->adj;
ptr->adj=ptr->adj->link; /* first element deleted */
free(tmp);
continue; /* continue searching in another edge lists */
}
q=ptr->adj;
while(q->link->link != NULL)
{
if(q->link->dest==u) /* element deleted in between */
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
continue;
}
q=q->link;
}/*End of while*/
if(q->link->dest==u) /* last element deleted */
{
tmp=q->link;
free(tmp);
q->link=NULL;
}
ptr=ptr->next;
}/*End of while*/
}/*End of delnode_edge()*/
void insert_edge(char u,char v)
{
struct node *locu,*locv;
struct edge *ptr,*tmp;
locu=find(u);
locv=find(v);
if(locu==NULL )
{
printf("Source node not present ,first insert node %c\n",u);
return;
}
if(locv==NULL )
{
printf("Destination node not present ,first insert node %c\n",v);
return;
}
tmp=(struct edge *)malloc(sizeof(struct edge));
tmp->dest=v;
tmp->link=NULL;
if(locu->adj==NULL) /* item added at the begining */
{
locu->adj=tmp;
return;
}
ptr=locu->adj;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=tmp;
}/*End of insert_edge()*/
struct node *find(char item)
{
struct node *ptr,*loc;
ptr=start;
while(ptr!=NULL)
{
if(item==ptr->name)
{
loc=ptr;
return loc;
}
else
ptr=ptr->next;
}
loc=NULL;
return loc;
}/*End of find()*/
void del_edge(char u,char v)
{
struct node *locu,*locv;
struct edge *ptr,*tmp,*q;
locu=find(u);
if(locu==NULL )
{
printf("Source node not present\n");
return;
}
if(locu->adj->dest == v)
{
tmp=locu->adj;
locu->adj=locu->adj->link; /* first element deleted */
free(tmp);
return;
}
q=locu->adj;
while(q->link->link != NULL)
{
if(q->link->dest==v) /* element deleted in between */
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
return;
}
q=q->link;
}/*End of while*/
if(q->link->dest==v) /* last element deleted */
{
tmp=q->link;
free(tmp);
q->link=NULL;
return;
}
printf("This edge not present in the graph\n");
}/*End of del_edge()*/
void display()
{
struct node *ptr;
struct edge *q;
ptr=start;
while(ptr!=NULL)
{
printf("%c ->",ptr->name);
q=ptr->adj;
while(q!=NULL)
{
printf(" %c",q->dest);
q=q->link;
}
printf("\n");
ptr=ptr->next;
}
}/*End of display()*/
main()
{
int choice;
char node,origin,destin;
while(1)
{
printf("1.Insert a node\n");
printf("2.Insert an edge\n");
printf("3.Delete a node\n");
printf("4.Delete an edge\n");
printf("5.Display\n");
printf("6.Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter a node to be inserted : ");
fflush(stdin);
scanf("%c",&node);
insert_node(node);
break;
case 2:
printf("Enter an edge to be inserted : ");
fflush(stdin);
scanf("%c %c",&origin,&destin);
insert_edge(origin,destin);
break;
case 3:
printf("Enter a node to be deleted : ");
fflush(stdin);
scanf("%c",&node);
/*This fn deletes the node from header node list*/
delete_node(node);
/* This fn deletes all edges coming to this node */
delnode_edge(node);
break;
case 4:
printf("Enter an edge to be deleted : ");
fflush(stdin);
scanf("%c %c",&origin,&destin);
del_edge(origin,destin);
break;
case 5:
display();
break;
case 6:
break;
default:
printf("Wrong choice\n");
break;
}/*End of switch*/
}/*End of while*/
}/*End of main()*/