-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublyll.c
390 lines (345 loc) · 9.32 KB
/
doublyll.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
// C program for the all operations in
// the Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
// Linked List Node
struct node {
int info;
struct node *prev, *next;
};
struct node* start = NULL;
//prototypes
void traverse();
void insertAtFront();
void insertAtEnd();
void insertAtPosition();
void deleteFirst();
void deleteEnd();
void deletePosition();
void reverseList();
void printBack();
int main()
{
int choice;
while (1) {
printf("\n\t1 To see list\n");
printf("\t2 For insertion at starting\n");
printf("\t3 For insertion at end\n");
printf("\t4 For insertion at any position\n");
printf("\t5 For deletion of first element\n");
printf("\t6 For deletion of last element\n");
printf("\t7 For deletion of element at any position\n");
printf("\t8 To reverse the list\n");
printf("\t9 To Display from Back\n");
printf("\t10 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
reverseList();
break;
case 9:
printBack();
break;
case 10:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}
// Function to traverse the linked list
void traverse()
{
struct node* temp;
// List is empty
if (start == NULL)
{
printf("\nList is empty\n");
}
// Else print the DLL
else
{
temp = start;
while (temp != NULL) {
printf(" %d ->",temp->info);
temp = temp->next;
}
}
}
// Function to insert at the front
// of the linked list
void insertAtFront()
{
int data;
struct node* temp;
// temp=start;
temp = (struct node*)malloc(sizeof(struct node));// temp=*prev<-NULL->next*
//NULL<-temp->NULL
printf("\nEnter number to be inserted: ");
scanf("%d", &data); //taking data from client
temp->info = data; // temp=NULL<-data->NULL
temp->prev = NULL; // NULL <- data //temp= NULL<-data->next*
// Pointer of temp will be
// assigned to start
if(start==NULL)
{
start=temp;
}
else
{
start->prev=temp;
temp->next=start;
start=temp;
}
traverse();
main();
}
// Function to insert at the end of
// the linked list
void insertAtEnd()
{
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;
// If start is NULL
if (start == NULL) {
printf("The Linklist is empty so your data will be inserted in the First Position\n");
start = temp;
traverse();
}
// Changes Links
else {
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
traverse();
}
}
// Function to insert at any specified
// position in the linked list
void insertAtPosition()
{
int pos,i=1;
int length=0;
struct node *ptr1,*newp;
ptr1=start;
newp=(struct node*)malloc(sizeof(struct node));
printf("Enter the position you want to insert: ");
scanf("%d",&pos);
while(ptr1!=NULL)
{
ptr1=ptr1->next;
length++;
}
if(start==NULL)
{
if(pos==0)
{
printf("Invalid Input\n");
main();
}
if(pos==1)
{
insertAtFront();
}
printf("The Linklist is empty, So your data will be inserted in th first position\n");
insertAtFront();
}
else if(pos<=0)
{
printf("Invalid Position\n");
main();
}
else if(pos==1)
{
insertAtFront();
}
else if(pos==length+1)
{
insertAtEnd();
}
else if(pos>=length+2)
{
printf("We have ony %d nodes in the List, please choose between 1 and %d to insert",length,length+1);
}
else
{
ptr1=start;
printf("Enter the data :");
scanf("%d",&newp->info);
while(i<pos-1)
{
ptr1=ptr1->next;
i++;
}
newp->prev=ptr1;
newp->next=ptr1->next;
ptr1->next=newp;
newp->next->prev=newp;
traverse();
// main();
}
}
// Function to delete from the front
// of the linked list
void deleteFirst()
{
struct node* temp;
if (start == NULL)
{
traverse();
}
else {
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
traverse();
}
}
// Function to delete from the end
// of the linked list
void deleteEnd()
{
struct node* temp;
if (start == NULL)
{
traverse();
}
else
{
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
}
traverse();
}
}
// Function to delete from any specified
// position from the linked list
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;
int count=0;
while(temp != NULL)
{
temp=temp->next;
count++;
}
// If DLL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
// Position to be deleted
printf("\nEnter position : ");
scanf("%d", &pos);
position=malloc(sizeof(struct node));
temp=start;
if(pos>count)
{
printf("We have only %d nodes in the list, please choose between 1 to %d\n",count,count);
main();
}
else if(pos==0)
{
printf("Invalid Input\n");
}
else if(pos ==1)
{
deleteFirst();
}
else{
// Traverse till position
while (i < pos - 1) {
temp = temp->next;
i++;
}
// Change Links
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
// Free memory
free(position);
traverse();
}
}}
void reverseList()
{
struct node *temp = NULL;
struct node *current = start;
/* swap next and prev for all nodes of
doubly linked list */
while (current != NULL)
{
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
/* Before changing head, check for the cases like empty
list and list with only one node */
if(temp != NULL )
start = temp->prev;
traverse();
}
void printBack()
{
struct node *tail=start;
if(start==NULL)
{
printf("List is empty\n");
main();
}
while(tail->next != NULL)
{
tail=tail->next;
}
while(tail!=start)
{
printf(" %d ",tail->info);
tail=tail->prev;
}
printf(" %d ",tail->info);
}