-
Notifications
You must be signed in to change notification settings - Fork 126
/
2. Operations_on_Singular_Linked_List.cpp
453 lines (421 loc) · 11.1 KB
/
2. Operations_on_Singular_Linked_List.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *nextptr;
} *stnode;
void create_node_list();
void display_list();
void insert_at_beginning();
void insert_at_end();
void insert_after();
void insert_before();
void delete_at_beginning();
void delete_at_end();
void delete_after();
void delete_before();
void delete_specific();
int main()
{
int ch;
printf("\nCREATION OF SINGULAR LINKED LIST\n");
create_node_list();
printf("\nDISPLAY OF SINGULAR LINKED LIST CREATED\n");
printf("\n Data entered in the list : \n");
display_list();
while(1)
{
printf("\nOPERATIONS TO BE PERFORMED ON A SINGULAR LINKED LIST");
printf("\n1: Insert a new node at the beginning. ");
printf("\n2: Insert a new node at the end. ");
printf("\n3: Insert a new node after a given node with a specific value.");
printf("\n4: Insert a new node before a given node with a specific value.");
printf("\n5: Delete a node at the beginning. ");
printf("\n6: Delete a node at the end.");
printf("\n7: Delete a node after a node with a specific value. ");
printf("\n8: Delete a node before a node with a specific value. ");
printf("\n9: Delete a node with a specific value. ");
printf("\n10: Stop execution and exit.");
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_at_beginning();
break;
case 2: insert_at_end();
break;
case 3: insert_after();
break;
case 4: insert_before();
break;
case 5: delete_at_beginning();
break;
case 6: delete_at_end();
break;
case 7: delete_after();
break;
case 8: delete_before();
break;
case 9: delete_specific();
break;
case 10: exit(0);
break;
default: printf("Wrong choice!");
}
}
return 0;
}
void create_node_list()
{
int n;
printf("\nHow many nodes do you want to enter? ");
scanf("%d", &n);
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf("Input data for node 1 : ");
scanf("%d", &num);
stnode->data = num;
stnode->nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf("Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->data = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void display_list()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" List is empty.");
}
else
{
tmp = stnode;
while(tmp!= NULL)
{
printf(" %d\n", tmp->data);
tmp = tmp->nextptr;
}
}
}
void insert_at_beginning()
{
if(stnode == NULL)
{
printf("Node insertion at the beginning is not possible because the list is empty.");
}
else
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the node data to be inserted: ");
scanf("%d",&temp->data);
temp->nextptr=stnode;
stnode=temp;
printf("\nThe list after insertion of node in the beginning is: \n");
display_list();
}
}
void insert_at_end()
{
if(stnode == NULL)
{
printf("Node insertion at the end is not possible because the list is empty.");
}
else
{
struct node *temp, *ptr;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the node data to be inserted.");
scanf("%d",&temp->data);
ptr=stnode;
while(ptr->nextptr!=NULL)
{
ptr=ptr->nextptr;
}
ptr->nextptr=temp;
temp->nextptr=NULL;
printf("\nThe list after insertion of node in the end is: \n");
display_list();
}
}
void insert_after()
{
if(stnode == NULL)
{
printf("Node insertion after specific node is not possible because the list is empty.");
}
else
{
struct node *temp, *ptr;
int k , val;
printf("\nEnter the node data after which insertion is to be done: ");
scanf("%d",&k);
printf("\nEnter the new node data to be inserted: ");
scanf("%d",&val);
temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
ptr=stnode;
while(ptr->data!=k && ptr->nextptr!=NULL)
{
ptr=ptr->nextptr;
}
if(ptr->nextptr==NULL)
{
if(ptr->data!=k)
{
printf("\nThe specified element %d could not be found in the list.\n",k);
}
else
{
ptr->nextptr=temp;
temp->nextptr=NULL;
printf("\nThe list after insertion of node after the specified element %d is: \n",k);
display_list();
}
}
else
{
temp->nextptr=ptr->nextptr;
ptr->nextptr=temp;
printf("\nThe list after insertion of node after the specified element %d is: \n",k);
display_list();
}
}
}
void insert_before()
{
if(stnode == NULL)
{
printf("Node insertion at before the specified element is not possible because the list is empty.");
}
else
{
struct node *temp , *ptr, *preptr;
int k;
printf("Enter the node data before which insertion is to be done:");
scanf("%d",&k);
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the new node data to be inserted:");
scanf("%d",&temp->data);
ptr=stnode;
if(ptr==stnode)
{
temp->nextptr=stnode;
stnode=temp;
printf("\nThe list after insertion of node before the specified element %d is: \n",k);
display_list();
}
else
{
while(ptr->data!=k && ptr->nextptr!=NULL)
{
preptr=ptr;
ptr=ptr->nextptr;
}
if(ptr->nextptr==NULL && ptr->data!=k)
{
printf("\nThe specified element %d could not be found in the list.\n",k);
}
else
{
preptr->nextptr=temp;
temp->nextptr=ptr;
printf("\nThe list after insertion of node before the specified element %d is: \n",k);
display_list();
}
}
}
}
void delete_at_beginning()
{
if(stnode == NULL)
{
printf("Node deletion at the beginning is not possible because the list is empty.");
}
else
{
struct node *temp;
temp=stnode;
stnode=stnode->nextptr;
free(temp);
printf("\nThe list after deletion of node at the beginning is: \n");
display_list();
}
}
void delete_at_end()
{
if(stnode == NULL)
{
printf("Node insertion at the end is not possible because the list is empty.");
}
else
{
struct node *temp, *ptr, *preptr;
ptr=stnode;
if(ptr->nextptr!=NULL)
{
while(ptr->nextptr!=NULL)
{
preptr=ptr;
ptr=ptr->nextptr;
}
preptr->nextptr=NULL;
}
free(ptr);
printf("\nThe list after deletion of node at the end is: \n");
display_list();
}
}
void delete_after()
{
if(stnode == NULL)
{
printf("Node insertion after the specified element is not possible because the list is empty.");
}
else
{
struct node *temp, *ptr;
int k , val;
printf("\nEnter the node data after which deletion is to be done: ");
scanf("%d",&k);
ptr=stnode;
while(ptr->data!=k && ptr->nextptr!=NULL)
{
ptr=ptr->nextptr;
}
if(ptr->nextptr==NULL)
{
if(ptr->data!=k)
{
printf("\nThe specified element %d could not be found in the list.\n",k);
}
else
{
printf("\nNo element exists after the specified element %d in the list.\n",k);
}
}
else
{
temp=ptr->nextptr;
ptr->nextptr=temp->nextptr;
free(temp);
printf("\nThe list after deletion of node after the specified element %d is: \n",k);
display_list();
}
}
}
void delete_before()
{
if(stnode == NULL)
{
printf("Node insertion before the specified element is not possible because the list is empty.");
}
else
{
struct node *temp , *ptr, *preptr;
int k;
printf("Enter the node data before which deletion is to be done:");
scanf("%d",&k);
ptr=stnode;
if(stnode->data==k)
printf("Deletion before the specified element is not possible because it is the first element of the list.");
else
{
while(ptr->data!=k && ptr->nextptr!=NULL)
{
preptr=ptr;
ptr=ptr->nextptr;
}
if(ptr->nextptr==NULL && ptr->data!=k)
{
printf("\nThe specified element %d could not be found in the list.\n",k);
}
else
{
preptr->data=ptr->data;
preptr->nextptr=ptr->nextptr;
free(ptr);
printf("\nThe list after deletion of node before the specified element %d is: \n",k);
display_list();
}
}
}
}
void delete_specific()
{
if(stnode == NULL)
{
printf("Specific node deletion is not possible because the list is empty.");
}
else
{
struct node *temp, *ptr, *preptr;
int k , val;
printf("\nEnter the node data which is to be deleted: ");
scanf("%d",&k);
ptr=stnode;
if(ptr->data==k)
{
stnode=ptr->nextptr;
free(ptr);
printf("\nThe list after deletion of specific node is: \n");
display_list();
}
else
{
while(ptr->data!=k && ptr->nextptr!=NULL)
{
preptr=ptr;
ptr=ptr->nextptr;
}
if(ptr->nextptr==NULL)
{
if(ptr->data!=k)
{
printf("\nThe specified element %d could not be found in the list.\n",k);
}
else
{
temp=ptr;
preptr->nextptr=NULL;
free(temp);
printf("\nThe list after deletion of specific node is: \n");
display_list();
}
}
else
{
temp=ptr;
preptr->nextptr=ptr->nextptr;
free(temp);
printf("\nThe list after deletion of specific node is: \n");
display_list();
}
}
}
}