-
Notifications
You must be signed in to change notification settings - Fork 445
/
Doubly_linked_list.c
344 lines (344 loc) · 8.54 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
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node* next;
struct node* prev;
};
void insert_at_beginning(struct node** head,int value)
{
struct node*temp=(struct node*)malloc(1*sizeof(struct node));
//checking if memory allocation failed or not
if(!temp)
{
printf("Memory allocation failed");
return;
}
temp->data=value;
//if the list is empty
if(!(*head))
{
temp->next=(*head);
temp->prev=(*head);
*head=temp;
return;
}
temp->next=(*head);//connecting temporary node to the first node
(*head)->prev=temp;//now connecting first node to the temporary node
temp->prev=NULL;//as now temporary node has become the first node so the its prev should hold NULL
*head=temp;
return;
}
void insert_at_end(struct node** head,int value)
{
struct node*temp=(struct node*)malloc(1*sizeof(struct node));
//if memory allocation fails
if(!temp)
{
printf("Memory allocation failed");
return;
}
temp->data=value;
//when no linked list is empty
if(!(*head))
{
temp->next=(*head);
temp->prev=(*head);
*head=temp;
return;
}
struct node *itr = *head;
//traversing to the last node
while(itr->next)
itr=itr->next;
temp->next=itr->next;
temp->prev=itr;
itr->next=temp;
return;
}
void insert_before_k(struct node** head,int value,int k)
{
struct node*temp=(struct node*)malloc(1*sizeof(struct node));
if(!temp)
{
printf("Memory allocation failed");
return;
}
temp->data=value;
//when no linked list is empty
if(!(*head))
{
temp->next=(*head);
temp->prev=(*head);
*head=temp;
return;
}
if((*head)->data==k)
{
temp->next=*head;
temp->prev=NULL;
(*head)->prev=temp;
*head=temp;
return;
}
struct node*itr=*head;
while(itr->next)//traversing till the last node
{
if(itr->next->data == k)
{
temp->next=itr->next;
temp->prev=itr;
temp->next->prev=temp;
itr->next=temp;
return;
}
itr=itr->next;
}
//inserting at the end when node not found
printf("\n%d is not found so %d is inserted in end",k,value);
temp->next=itr->next;
temp->prev=itr;
itr->next=temp;
}
void insert_after_k(struct node** head,int value, int k)
{
struct node*temp=(struct node*)malloc(1*sizeof(struct node));
//when memory allocation fails
if(!temp)
{
printf("Memory allocation failed");
return;
}
temp->data=value;
if(!(*head))
{
temp->next=(*head);
temp->prev=(*head);
*head=temp;
return;
}
struct node*itr=*head;
do
{
if(itr->data==k)
{
temp->next=itr->next;
temp->prev=itr;
if (itr->next)
{
itr->next->prev = temp;
}
itr->next=temp;
return;
}
itr=itr->next;
}while(itr);
//inserting at the end when node not found
printf("%d is not found so %d is inserted at the end\n",k,value);
temp->next=itr->next;
temp->prev=itr;
itr->next=temp;
}
void delete_by_value(struct node** head,int k)
{
//when list is empty
if(!(*head))
{
printf("\nEmpty list");
return;
}
struct node*itr=*head;
if((*head)->data==k)//when the node to delete is head
{
*head=(*head)->next;
(*head)->prev=itr->prev;
free(itr);
return;
}
while(itr)
{
if(itr->data==k)
{
if (itr->prev)
{
itr->prev->next = itr->next;
}
if (itr->next)
{
itr->next->prev = itr->prev;
}
//If the node to delete is the head
if (*head == itr)
{
*head = itr->next;
}
free(itr);
return;
}
itr=itr->next;
}
printf("Node with value %d not found\n",k );
}
void delete_head(struct node** head)
{
//checking whether list is empty or not
if(!(*head))
{
printf("\nEmpty list\n");
return;
}
if(!((*head)->next))//if there is only one node in the linkedlist
{
*head=(*head)->next;
free(*head);
return;
}
*head=(*head)->next;//changing the pointer of head to point at second node in linkedlist
free((*head)->prev);//freeing the first node
(*head)->prev=NULL;//freeing the first node
}
void delete_tail(struct node** head)
{
//checking whether list is empty or not
if(!(*head))//or you can write it as *head==NULL
{
printf("\nEmpty list\n");
return;
}
//if there is only one node
if(!((*head)->next))
{
*head=(*head)->next;
free(*head);
return;
}
struct node*itr=*head;
//iterating till the tail
while(itr->next->next)
{
itr=itr->next;
}
itr->next = NULL;//assigning last node NULL
free(itr->next);
}
void display_head(struct node** head)
{
//checking whether list is empty or not
if(!(*head))
{
printf("\nEmpty list\n");
return;
}
//printing while traversing till last node
struct node *itr=*head;
while(itr)
{
printf("%d ----> ", itr->data);
itr=itr->next;
}
printf("NULL\n");//to indicate the address stored by last node
}
void display_tail(struct node** head)
{
//checking whether list it empty or not
if(!(*head))
{
return;
}
//recurrively calling the function
display_tail(&((*head)->next));
//printing the node
printf("%d --->", (*head)->data);//address of first node will be printed as data in this case
}
int main()
{
struct node * head=NULL;
char ch;
int choice,data,value;
do
{
printf("Enter your choice:\n\n1-Insert at beginning\n2-insert at end\n3-insert before a given value\n4-insert after a given value\n5-delete a node by value\n6-delete first node\n7-delete last node\n8-display node from head to tail\n9-display node form tail to head: \n");
scanf("%d",&choice);
switch (choice)
{
case 1:
{
printf("\nEnter the data to be inserted at the beginning: ");
scanf("%d",&data);
insert_at_beginning(&head,data);
break;
}
case 2:
{
printf("\nEnter the data to be inserted at the end: ");
scanf("%d",&data);
insert_at_end(&head,data);
break;
}
case 3:
{
printf("\nEnter the data to be inserted: ");
scanf("%d",&data );
printf("\nInsert before which value? ");//inserting before that value
scanf("%d",&value);
insert_before_k(&head,data,value);
break;
}
case 4:
{
//inserting after k
printf("\nEnter the data to be inserted : ");
scanf("%d", &data);
printf("\nAfter what number you want to enter this element? ");
scanf("%d",&value);
insert_after_k(&head,data,value);
break;
}
case 5:
{
//deleting a node by value
printf("\nEnter the value of the node you want to delete: ");
scanf("%d",&value);
delete_by_value(&head,value);
break;
}
case 6 :
{
//delete first node
delete_head(&head);
break;
}
case 7:
{
//delete last node
delete_tail(&head);
break;
}
case 8:
{
//display node from head to tail
display_head(&head);
break;
}
case 9:
{
//display node from tail to head
display_tail(&head);
break;
}
default:
{
printf("\nInvalid argument\n");
}
}
printf("\nDo you want to continue: Y-yes or N-no: ");
scanf(" %c",&ch);
}while(ch=='y'||ch=='Y');
while (head)
{
struct node* temp = head;
head = head->next;
free(temp);
}
return 0;
}