-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathdoubly_linked_list.c
466 lines (401 loc) · 11 KB
/
doubly_linked_list.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
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
454
455
456
457
458
459
460
461
462
463
464
465
#include <stdio.h>
#include <malloc.h>
/*
Code Description :
Doubly linked list is a type of linked list in which each node
has a data part and two pointer part. The first pointer points
to the previous node in the list and the second pointer points
to the next node in the list.
In the code below we have created the structure of doubly linked list.
We have also provided options to the user to choose among the opearations.
As per the option selected, respective switch-case will execute and function
will be called. Brief description of each function is also provided below.
*/
struct node
{
int info;
struct node *prev,*next;
};
struct node *start = NULL;
//Function Declarations
void insertstart();
void insertlast();
void insertafter(struct node *);
struct node* find ();
void insertat();
void deletefirst();
void deletelast();
void deleteafter();
void viewlist();
int main()
{
int choice;
char ans = 'Y';
while(ans == 'Y' || ans == 'y')
{
printf("\n\n Operations on doubly linked list");
printf("\n----------------------------------");
printf("\n 1- To view the list.");
printf("\n 2- Insertion of element at starting.");
printf("\n 3- Insertion of element at end.");
printf("\n 4- Insertion of element at any position by providing previous element.");
printf("\n 5- Insertion of element at any position by providing index.");
printf("\n 6- Deletion of the first element.");
printf("\n 7- Deletion of the last element.");
printf("\n 8- Delete an element from any position.");
printf("\n 9- To exit.");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\nEnter your choice : ");
scanf("%d" , &choice);
switch(choice)
{
case 1:
viewlist();
break;
case 2:
insertstart();
break;
case 3:
insertlast();
break;
case 4:
insertafter(find());
break;
case 5:
insertat();
break;
case 6:
deletefirst();
break;
case 7:
deletelast();
break;
case 8:
deleteafter();
break;
case 9:
printf("\nExiting :\n");
return 0;
break;
default:
printf("\nIncorrect Choice!\n");
break;
}
printf("\nYOU WANT TO CONTINUE (Y/N) : ");
scanf(" %c", &ans);
}
return 0;
}
/*
To insert a node at the starting of the Doubly linked list,
create a new node and make it point by the start pointer.
*/
void insertstart()
{
int data;
struct node *temp;
temp = (struct node*) malloc (sizeof(struct node));
printf("\nEnter the element : ");
scanf("%d" , &data);
temp->info = data;
temp->prev = NULL;
temp->next = start;
start = temp;
}
/*
To insert a node at the end of the Doubly linked list,
create a new node and make it pointed by the previously last node.
*/
void insertlast()
{
int data;
struct node *temp , *trav;
temp = (struct node*) malloc (sizeof(struct node));
printf("\nEnter the element : ");
scanf("%d" , &data);
temp->info = data;
temp->next = NULL;
trav = start;
if(start == NULL)
{
temp->prev = NULL;
start = temp;
}
else
{
while(trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
/*
To insert a node at a specific position of the Doubly linked list,
create a new node, traverse the list till the desired position,
make the new node pointed by the previous node.
Also make the new node point to the previou's next node.
*/
void insertafter(struct node *ptr)
{
int data;
struct node *temp;
temp = (struct node*) malloc (sizeof(struct node));
if(ptr == NULL)
printf("\nThe list is empty.\n");
else
{
printf("\nEnter the element : ");
scanf("%d" , &data);
temp->info = data;
temp->prev = ptr;
temp->next = ptr->next;
ptr->next->prev = temp;
ptr->next = temp;
}
}
struct node* find ()
{
int data;
struct node *temp;
if(start == NULL)
return NULL;
else
{
printf("\nEnter the element after which you want the insertion : ");
scanf("%d" , &data);
temp = start;
while(temp != NULL)
{
if(temp->info == data)
return temp;
else
temp = temp->next;
}
return NULL;
}
}
void insertat()
{
int data , pos , i=1;
struct node *temp , *newnode;
newnode = malloc(sizeof(struct node));
printf("\nEnter the index/position : ");
scanf("%d" , &pos);
printf("\nEnter the element : ");
scanf("%d" , &data);
newnode->info = data;
temp = start;
if(start == NULL)
{
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}
else
{
while(i < pos-1)
{
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
}
}
/*
To delete the first node, make start ointer point to the next node.
*/
void deletefirst()
{
struct node *temp;
if(start == NULL)
printf("\nThe list is empty.\n");
else
{
temp = start;
start = start->next;
start->prev = NULL;
free(temp);
}
}
/*
To delete the last node, traverse till second last node;
and make it point to NULL.
*/
void deletelast()
{
struct node *temp;
if(start == NULL)
printf("\nThe list is empty.\n");
else
{
temp = start;
while(temp->next != NULL)
temp = temp->next;
if(start->next == NULL)
start = NULL;
else
{
temp->prev->next = NULL;
free(temp);
}
}
}
/*
To delete a specific node, traverse till that node (lets say current),
use an extra pointer to point its previous node,
now make the previous node point to current node's next node.
*/
void deleteafter()
{
int pos , i=1;
struct node *temp , *position;
temp = start;
if(start == NULL)
printf("\nThe list is empty.\n");
else
{
printf("\nEnter the index : ");
scanf("%d",&pos);
while(i < pos-1)
{
temp = temp->next;
i++;
}
position = temp->next;
position->next->prev = temp;
temp->next = position->next;
free(position);
}
}
/*
To view the list, use a pointer and make it point to start,
now print the node's data part and make it point to next node.
Repeat both the steps until the pointer points to NULL.
*/
void viewlist()
{
if(start == NULL)
printf("\nThe list is empty.\n");
else
{
struct node *temp;
temp = start;
printf("\nThe list is : ");
while(temp != NULL)
{
printf("%d " , temp->info);
temp = temp->next;
}
}
}
/*Sample Input/Output of this program-
Operations on doubly linked list
----------------------------------
1- To view the list.
2- Insertion of element at starting.
3- Insertion of element at end.
4- Insertion of element at any position by providing previous element.
5- Insertion of element at any position by providing index.
6- Deletion of the first element.
7- Deletion of the last element.
8- Delete an element from any position.
9- To exit.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 2
Enter the element : 10
YOU WANT TO CONTINUE (Y/N) : y
Operations on doubly linked list
----------------------------------
1- To view the list.
2- Insertion of element at starting.
3- Insertion of element at end.
4- Insertion of element at any position by providing previous element.
5- Insertion of element at any position by providing index.
6- Deletion of the first element.
7- Deletion of the last element.
8- Delete an element from any position.
9- To exit.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 3
Enter the element : 20
YOU WANT TO CONTINUE (Y/N) : y
Operations on doubly linked list
----------------------------------
1- To view the list.
2- Insertion of element at starting.
3- Insertion of element at end.
4- Insertion of element at any position by providing previous element.
5- Insertion of element at any position by providing index.
6- Deletion of the first element.
7- Deletion of the last element.
8- Delete an element from any position.
9- To exit.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 4
Enter the element after which you want the insertion : 10
Enter the element : 15
YOU WANT TO CONTINUE (Y/N) : y
Operations on doubly linked list
----------------------------------
1- To view the list.
2- Insertion of element at starting.
3- Insertion of element at end.
4- Insertion of element at any position by providing previous element.
5- Insertion of element at any position by providing index.
6- Deletion of the first element.
7- Deletion of the last element.
8- Delete an element from any position.
9- To exit.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 1
The list is : 10 15 20
YOU WANT TO CONTINUE (Y/N) : y
Operations on doubly linked list
----------------------------------
1- To view the list.
2- Insertion of element at starting.
3- Insertion of element at end.
4- Insertion of element at any position by providing previous element.
5- Insertion of element at any position by providing index.
6- Deletion of the first element.
7- Deletion of the last element.
8- Delete an element from any position.
9- To exit.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 6
YOU WANT TO CONTINUE (Y/N) : y
Operations on doubly linked list
----------------------------------
1- To view the list.
2- Insertion of element at starting.
3- Insertion of element at end.
4- Insertion of element at any position by providing previous element.
5- Insertion of element at any position by providing index.
6- Deletion of the first element.
7- Deletion of the last element.
8- Delete an element from any position.
9- To exit.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 7
YOU WANT TO CONTINUE (Y/N) : y
Operations on doubly linked list
----------------------------------
1- To view the list.
2- Insertion of element at starting.
3- Insertion of element at end.
4- Insertion of element at any position by providing previous element.
5- Insertion of element at any position by providing index.
6- Deletion of the first element.
7- Deletion of the last element.
8- Delete an element from any position.
9- To exit.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Enter your choice : 1
The list is : 15
YOU WANT TO CONTINUE (Y/N) : n
Process returned 0 (0x0) execution time : 50.165 s
Press any key to continue.
*/