-
Notifications
You must be signed in to change notification settings - Fork 0
/
3DoublyLinkedList.c
377 lines (317 loc) · 7.09 KB
/
3DoublyLinkedList.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *prev;
struct node *next;
}node;
int count(node* head)
{
int c = 0;
node* ptr = head;
while(ptr != NULL)
{
c++;
ptr = ptr->next;
}
return c;
}
node* AddToEmpty(node *head, int val)
{
head = (node*)malloc(sizeof(node));
head->prev = NULL;
head->data = val;
head->next = NULL;
return head;
}
node* InsertAtBeginning(node *head, int val)
{
node* ptr = (node*)malloc(sizeof(node));
ptr->prev = NULL;
ptr->data = val;
ptr->next = head;
if(head != NULL)
{
head->prev = ptr;
}
head = ptr;
return head;
}
node* InsertAtEnd(node *head, int val)
{
node *temp = (node*)malloc(sizeof(node));
temp->prev = NULL;
temp->data = val;
temp->next = NULL;
node* ptr = head;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
temp->prev = ptr;
return head;
}
// METHON FOR INSERTION IN BETWEEN (USING ONE POINTER)
node* InsertInBetween(node* head, int pos, int val)
{
int c = count(head);
node *temp = (node*)malloc(sizeof(node));
temp->prev = NULL;
temp->data = val;
temp->next = NULL;
node* ptr = head;
if(pos == 1)
{
head = InsertAtBeginning(head, val);
}
else if(pos == c+1)
{
head = InsertAtEnd(head, val);
}
else
{
pos--;
while(pos != 1)
{
ptr = ptr->next;
pos--;
}
temp->next = ptr->next;
ptr->next->prev = temp;
ptr->next = temp;
temp->prev = ptr;
}
return head;
}
/*ALTERNATIVE METHOD FOR INSERTION IN BETWEEN (USING TWO POINTERS)
node* InsertInBetween(node* head, int pos, int val)
{
node* ptr = NULL;
node* temp1 = head;
node* temp2 = NULL;
ptr = AddToEmpty(ptr, val);
while(pos != 1)
{
temp1 = temp1->next;
pos--;
}
if(temp1->next == NULL)
{
temp1->next = ptr;
ptr->prev = temp1;
}
else if(pos == 1)
{
head = InsertAtBeginning(head, val);
}
else
{
temp2 = temp1->next;
temp1->next = ptr;
temp2->prev = ptr;
ptr->next = temp2;
ptr->prev = temp1;
}
return head;
}
*/
node* DeleteFromBeginning(node* head)
{
if(head == NULL)
{
printf("Linked list is empty!\n");
}
else
{
node* ptr = head;
head = head->next;
free(ptr);
ptr = NULL;
head->prev = NULL;
}
return head;
}
node* DeleteFromEnd(node* head)
{
if(head == NULL)
{
printf("Linked list is already empty!");
}
else
{
node* ptr = head;
while(ptr->next->next != NULL)
{
ptr = ptr->next;
}
free(ptr->next);
ptr->next = NULL;
}
return head;
}
node* DeletionInBetween(node* head, int pos)
{
int c = count(head);
if(pos == 1)
{
head = DeleteFromBeginning(head);
}
else if(pos == c)
{
head = DeleteFromEnd(head);
}
else
{
node* ptr1 = head;
node* ptr2 = NULL;
while(pos != 1)
{
ptr2 = ptr1;
ptr1 = ptr1->next;
pos--;
}
ptr2->next = ptr1->next;
free(ptr1);
ptr1 = NULL;
ptr2->next->prev = ptr2;
}
return head;
}
node* ReversalOfList(node* head)
{
node* ptr = head;
node* temp = NULL;
while(ptr != NULL)
{
temp = ptr->next;
ptr->next = ptr->prev;
ptr->prev = temp;
head = ptr;
ptr = ptr->prev;
}
return head;
}
void display(node* head)
{
node* ptr = head;
while(ptr != NULL)
{
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
node* CreateList(node* head)
{
int n, val, i;
printf("Enter the number of nodes you want to enter: ");
scanf("%d", &n);
if(n == 0)
{
return head;
}
else
{
printf("Enter the element for node 1: ");
scanf("%d", &val);
head = AddToEmpty(head, val);
for(i=1; i<n; i++)
{
printf("Enter the element for node %d: ", i+1);
scanf("%d", &val);
head = InsertAtEnd(head, val);
}
}
return head;
}
int main()
{
node* head = (node*)malloc(sizeof(node));
head = NULL;
head = CreateList(head);
int op, val, c, pos;
c = count(head);
while(1)
{
printf("Enter 1 to Insert a node at beginning\n");
printf("Enter 2 to Insert a node at the end\n");
printf("Enter 3 to Insert a node in between two nodes\n");
printf("Enter 4 to delete the node from beginning\n");
printf("Enter 5 to delete the node from the end\n");
printf("Enter 6 to Delete the node in between\n");
printf("Enter 7 to reverse the linked list\n");
printf("Enter 8 to display the list\n");
printf("Enter 9 to count the total nubmer of nodes in the list\n");
printf("Enter 10 to exit\n");
printf("Enter your option- ");
scanf("%d", &op);
switch(op)
{
case 1:
printf("Enter the element you want to enter at the beginning: ");
scanf("%d", &val);
head = InsertAtBeginning(head, val);
break;
case 2:
if(head == NULL)
{
printf("List is empty!\n");
}
else
{
printf("Enter the element you want to enter at the end: ");
scanf("%d", &val);
head = InsertAtEnd(head, val);
}
break;
case 3:
printf("Enter the position at which you want to enter the element: ");
scanf("%d", &pos);
if(pos < 1 || pos > c)
{
printf("Invalid position!\n");
}
else
{
printf("Enter the element you want to enter: ");
scanf("%d", &val);
head = InsertInBetween(head, pos, val);
}
break;
case 4:
head = DeleteFromBeginning(head);
break;
case 5:
head = DeleteFromEnd(head);
break;
case 6:
printf("Enter the position from which you want to delete the node: ");
scanf("%d", &pos);
if(pos > c || pos < 1)
{
printf("Invalid position\n");
}
else
{
head = DeletionInBetween(head, pos);
}
break;
case 7:
head = ReversalOfList(head);
break;
case 8:
display(head);
break;
case 9:
c = count(head);
printf("Total number of nodes in the list are: %d", c);
break;
case 10:
exit(1);
default:
printf("Wrong input\n");
}
}
return 0;
}