-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete1.c
374 lines (319 loc) · 5.07 KB
/
delete1.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int info;
struct node *link;
}Node;
void disp(Node *pf);
Node *insertfront(Node *pf);
Node *createnode(Node *nw);
Node *delete1(Node *pf);
Node *delete2(Node *pf);
Node *delete3(Node *pf);
Node *searchneg(Node *pf);
Node *insertrightk(Node *pf,int k);
Node *insertleftk(Node *pf,int k);
Node *destroy(Node *pf);
Node *searchkey(Node *pf,int k);
Node *swap(Node *pf,Node *N1,Node *N2);
int main()
{
Node *root = NULL;
int ch;
int k;
Node *n1,*n2;
int k1,k2;
while(1)
{
printf("1.Insertfront\n");
printf("2.Delete neg\n");
printf("3.Delete 2\n");
printf("4.Delete 3\n");
printf("5.Disp\n");
printf("6.Insert right of k\n");
printf("7.Insert left of k\n");
printf("8.Destroy\n");
printf("9.Swap 2 nodes\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
root = insertfront(root);
break;
case 2:
root = delete1(root);
break;
case 3:
root = delete2(root);
break;
case 4:
root = delete3(root);
break;
case 5:
disp(root);
break;
case 6:
printf("Enter the value of k\n");
scanf("%d",&k);
root = insertrightk(root,k);
break;
case 7:
printf("Enter the value of k\n");
scanf("%d",&k);
root = insertleftk(root,k);
break;
case 8:
root = destroy(root);
break;
case 9:
printf("Enter the value of first node key\n");
scanf("%d",&k1);
n1 = searchkey(root,k1);
printf("Enter the value of second node key\n");
scanf("%d",&k2);
n2 = searchkey(root,k2);
if(n1!=NULL && n2!=NULL)
root = swap(root,n1,n2);
disp(root);
break;
default:
exit(1);
}
}
return 0;
}
Node *createnode(Node *nw)
{
nw = (Node *)malloc(sizeof(Node));
nw->link = NULL;
return nw;
}
Node *insertfront(Node *pf)
{
Node *nw;
nw = createnode(nw);
printf("Enter the value\n");
scanf("%d",&nw->info);
if(pf==NULL)
pf = nw;
else
{
nw->link = pf;
pf = nw;
}
return pf;
}
void disp(Node *pf)
{
if(pf==NULL)
{
printf("Empty");
return;
}
while(pf != NULL)
{
printf("%d ",pf->info);
pf = pf->link;
}
printf("\n");
}
Node *delete2(Node *pf)//delete node next to neg
{
Node *tn,*pn;
tn = pf;
pn = tn;
tn = tn->link;
while(tn!=NULL)
{
if(pn->link != NULL && pn->info < 0 )
{
pn->link = tn->link;
free(tn);
tn = pn->link;
}
if(tn!=NULL)
{
pn = tn;
tn = tn->link;
}
}
return pf;
}
Node *swap(Node *pf,Node *N1,Node *N2)//swap 2 nodes
{
Node *p1,*p2,*T;
p1 = pf;
while(p1 != N1 && p1->link != N1)
p1 = p1->link;
p2 = N1->link;
while(p2 != N2 && p2->link != N2)
p2 = p2->link;
T = N2->link;
N2->link = N1->link;
p1->link = N2;
p2->link = N1;
N1->link = T;
printf("N2 = %d N1 = %d\n",N2->info,N1->info);
if(N1==pf)
{
pf = N2;
printf("NULL\n");
}
//return N2;
return pf;
}
Node *delete3(Node *pf)//delete the node previous to node having a negetive key value
{
Node *tn,*pn,*qn;
qn=pn=NULL;
tn = pf;
while(tn!=NULL)
{
if(tn->info >= 0)
{
qn = pn;
pn = tn;
tn = tn->link;
}
else if(pn==NULL)
{
if(pn==NULL)
{
qn = pn;
pn = tn;
tn = tn->link;
}
else
{
pf = tn;
free(pn);
pn=NULL;
}
}
else
{
qn->link = tn;
free(pn);
pn = tn;
tn = tn->link;
}
}
return pf;
}
Node *delete1(Node *pf)//delete all nodes containing a negative value
{
Node *temp,*pos,*prev=NULL;
temp = pf;
while(temp!=NULL)
{
pos = searchneg(temp);
if(pos != NULL)
{
if(prev == NULL)
{
pf = pf->link;
free(temp);
temp = pf;
}
else
{
while(temp!=pos)
{
prev = temp;
temp = temp->link;
}
prev->link = temp->link;
free(temp);
temp = pf;
prev = NULL;
}
}
else
{
prev = temp;
temp = temp->link;
}
}
return pf;
}
Node *searchneg(Node *pf)
{
if(pf->info<0)
return pf;
else
return NULL;
}
Node *insertrightk(Node *pf,int k)//insert element to the right of kth node
{
Node *temp,*nw,*prev=NULL;
int count=1;
temp = pf;
while(temp != NULL && count!=k)
{
prev = temp;
temp = temp->link;
count++;
}
if(temp == NULL)
{
printf("Entered position is invalid\n");
return pf;
}
nw = createnode(nw);
printf("Enter the info\n");
scanf("%d",&nw->info);
nw->link = temp->link;
temp->link = nw;
printf("check\n");
return pf;
}
Node *insertleftk(Node *pf,int k)//insert node to the left of kth node
{
Node *temp,*nw,*prev=NULL;
int count=1;
temp = pf;
while(temp != NULL && count != k)
{
prev = temp;
temp = temp->link;
count++;
}
if(temp == NULL)
{
printf("Entered position is invalid\n");
return pf;
}
nw = createnode(nw);
printf("Enter the info\n");
scanf("%d",&nw->info);
if(prev == NULL)
pf = nw;
else
prev->link = nw;
nw->link = temp;
printf("check\n");
return pf;
}
Node *destroy(Node *pf)//destroy all nodes
{
Node *temp;
temp = pf;
while(pf!=NULL)
{
pf = pf->link;
free(temp);
temp = pf;
}
return NULL;
}
Node *searchkey(Node *pf,int k)
{
Node *temp = pf;
while(temp!=NULL)
{
if(temp->info == k)
return temp;
temp = temp->link;
}
return NULL;
}