-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublylinkedlist.c
289 lines (263 loc) · 6.79 KB
/
doublylinkedlist.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head, *tail;
void insert_begin();
void insert_end();
void insert_anywhere();
void delete_begin();
void delete_end();
void delete_random();
void display();
void reverse();
void main()
{
int choice = 0;
while(choice != 10)
{
printf("\nChoose from the menu : \n1.Insert in Begining\n2.Insert at Last\n3.Insert at any Random Location\n4.Delete from Beginning\n5.Delete from Last\n6.Delete Node from Specified Location\n7.Show\n8.Reverse\n9.Exit\n");
printf("\nEnter your Choice -> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_begin();
break;
case 2:
insert_end();
break;
case 3:
insert_anywhere();
break;
case 4:
delete_begin();
break;
case 5:
delete_end();
break;
case 6:
delete_random();
break;
case 7:
display();
break;
case 8:
reverse();
break;
case 9:
printf("\nProgram Ended.\n");
exit(0);
break;
default:
printf("Please Enter A Valid Choice!\n");
}
}
}
void insert_begin()
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
if (newnode == NULL)
{
printf("Memory allocation failed. Overflow!\n");
return;
}
printf("Enter Data - ");
scanf("%d", &newnode -> data);
newnode -> prev = 0;
newnode -> next = 0;
if (head == 0)
{
head = tail = newnode;
printf("Node Inserted.\n");
}
else
{
head -> prev = newnode;
newnode -> next = head;
head = newnode;
printf("\nNode Inserted at the Beginning.\n");
}
}
void insert_end()
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
if (newnode == NULL)
printf("Memory allocation failed. Overflow!\n");
printf("Enter Data - ");
scanf("%d", &newnode -> data);
newnode -> prev = 0;
newnode -> next = 0;
if (head == 0)
{
newnode -> next = NULL;
head = newnode;
printf("\nNode Inserted at the End.\n");
}
else
{
tail -> next = newnode;
newnode -> prev = tail;
tail = newnode;
printf("\nNode Inserted at the End.\n");
}
}
void insert_anywhere()
{
int pos;
struct node *newnode, *temp;
newnode = (struct node *) malloc (sizeof(struct node));
if (newnode == NULL)
printf("Memory allocation failed. Overflow!\n");
else
{
printf("\nEnter the location at which you want to insert - ");
scanf("\n%d", &pos);
temp = head;
int length = 0;
// Calculate the length of the linked list
while(temp != NULL)
{
length++;
temp = temp->next;
}
if (pos < 1 || pos > length)
printf("Invalid Location.\n");
else if (pos == 1)
insert_begin();
else
{
printf("\nEnter Element Value - ");
scanf("%d", &newnode->data);
temp = head;
int i = 1; // Reset i to 1 before traversing the list again
while(i < pos - 1)
{
temp = temp->next;
i++;
}
newnode -> prev = temp;
newnode -> next = temp -> next;
temp -> next = newnode;
newnode -> next -> prev = newnode;
printf("\nNode Inserted at the provided postion.\n");
}
}
}
void delete_begin()
{
struct node *temp;
if (head == NULL)
printf("List is Empty!\n");
else
{
temp = head;
if (head -> next != NULL)
{
head = head -> next;
head -> prev = NULL; // Update prev only if there's a new head
}
else
head = NULL; // Set head to NULL if deleting the last node
free(temp);
printf("Node Deleted from the Beginning!\n");
}
}
void delete_end()
{
struct node *temp;
if (tail == NULL)
printf("List is Empty!\n");
else
{
temp = tail;
if (tail -> prev != NULL)
{
tail -> prev -> next = NULL; // Update next only if there's a new tail
tail = tail -> prev;
}
else
tail = NULL; // Set tail to NULL if deleting the last node
free(temp);
printf("Node Deleted from the End!\n");
}
}
void delete_random()
{
int pos, i = 1;
struct node *temp;
printf("\nEnter the position where you want to delete - ");
scanf("%d", &pos);
temp = head;
int length = 0;
// Calculate the length of the doubly linked list
while (temp != NULL)
{
length++;
temp = temp->next;
}
if (pos < 1 || pos > length)
{
printf("Invalid Location.\n");
}
else if (pos == 1)
{
delete_begin();
}
else
{
temp = head; // Reset temp to head for traversal
while (temp != NULL && i < pos)
{
temp = temp->next;
i++;
}
if (temp == NULL)
{
printf("Invalid position. Can't delete!\n");
return;
}
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
free(temp);
printf("\nNode deleted from the given position.\n");
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
printf("Nothing to Print!\n");
else
{
printf("\nPrinting the Values :- \n");
while (ptr != NULL)
{
printf("\n%d", ptr->data);
ptr = ptr->next;
}
}
}
void reverse()
{
struct node *next_node, *current_node;
current_node = head;
while (current_node != NULL)
{
next_node = current_node -> next;
current_node -> next = current_node -> prev;
current_node -> prev = next_node;
current_node = next_node;
}
current_node = head;
head = tail;
tail = current_node;
printf("Node Reversed.\n");
}