-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublecircular
305 lines (296 loc) · 7.25 KB
/
doublecircular
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
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev,*next;
int data;
};
struct node *head=NULL,*new_node,*ptr,*preptr,*temp,*del;
struct node *createnode(int item){
new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=item;
return new_node;
}
void display(){
if(head==NULL){
printf("Empty list");
return;
}
temp=head;
while(temp->next!=head){
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
void insert_beg(int item){
new_node=createnode(item);
if(head==NULL){
head=new_node;
new_node->prev=new_node;
new_node->next=head;
}
else{
new_node->next=head;
head->prev->next=new_node;
new_node->prev=head->prev;
head->prev=new_node;
head=new_node;
}
printf("The new list after insertion is: ");
display();
}
void insert_end(int item){
new_node=createnode(item);
if(head==NULL){
head=new_node;
new_node->prev=new_node;
new_node->next=head;
}
else{
head->prev->next=new_node;
new_node->next=head;
head->prev=new_node;
printf("The new list after insertion is: ");
display();
}
}
void insert_before(int val, int item){
new_node = createnode(item);
if(head == NULL){
printf("Empty list");
return;
}
ptr = head;
if(head->data == val){
insert_beg(item);
return;
}
do{
ptr = ptr->next;
} while(ptr != head && ptr->data != val);
if(ptr == head){
printf("Node doesn't exist");
return;
}
new_node->next = ptr;
new_node->prev = ptr->prev;
ptr->prev->next = new_node;
ptr->prev = new_node;
printf("The new list after insertion is: ");
display();
}
void insert_after(int val, int item){
new_node = createnode(item);
if(head == NULL){
printf("Empty list");
return;
}
ptr = head;
do{
ptr = ptr->next;
} while(ptr != head && ptr->data != val);
if(ptr == head){
printf("Node doesn't exist");
return;
}
new_node->prev = ptr;
new_node->next = ptr->next;
ptr->next = new_node;
printf("The new list after insertion is: ");
display();
}
void delete_beg(){
if(head==NULL){
printf("Empty list");
return;
}
if (head->next == head) {
free(head);
head = NULL;
}
else{
(head->prev)->next=head->next;
(head->next)->prev=head->prev;
del=head;
head=head->next;
free(del);
printf("The new list after deletion is: ");
}
display();
}
void delete_last() {
if (head == NULL) {
printf("Empty list");
return;
}
if (head->next == head) {
free(head);
head = NULL;
} else {
temp = head->prev;
head->prev = temp->prev;
temp->prev->next = head;
free(temp);
}
printf("The new list after deletion is: ");
display();
}
void delete_node(int val){
if(head==NULL){
printf("Empty list");
return;
}
ptr=head;
if(ptr->data==val){
delete_beg();
return;
}
while(ptr->data!=val){
ptr=ptr->next;
if(ptr==head){
printf("Node doesn't exist");
return;
}
}
ptr->prev->next=ptr->next;
ptr->next->prev=ptr->prev;
free(ptr);
printf("The new list after deletion is: ");
display();
}
void delete_before(int item) {
if (head == NULL) {
printf("Empty list\n");
return;
}
ptr = head;
preptr = head->prev;
// Find the node containing 'item'
while (ptr->next != head && ptr->next->data != item) {
preptr = ptr;
ptr = ptr->next;
}
// Check if the node with 'item' was found
if (ptr->next == head || ptr->next->data != item) {
printf("Item not found in the list.\n");
return;
}
// Check if 'ptr' is the first or second node
if (ptr == head || ptr->next == head) {
printf("No node before the second node.\n");
return;
}
// Update pointers to delete the node before 'item'
preptr->prev->next = ptr;
ptr->prev = preptr->prev;
free(preptr);
printf("The new list after deletion is: ");
display();
}
void delete_after(int item) {
if (head == NULL) {
printf("List is empty, nothing to delete.\n");
return;
}
ptr = head;
preptr = ptr;
if (ptr->data == item) {
while (preptr->next != head) {
preptr = preptr->next;
}
preptr->next = ptr->next;
ptr->next->prev = preptr;
if (ptr == head) {
head = preptr->next;
}
if (head == ptr) {
head = NULL;
}
free(ptr);
display();
return;
}
while (ptr->data != item && ptr->next != head) {
preptr = ptr;
ptr = ptr->next;
}
if (ptr->data != item) {
printf("Item not found in the list.\n");
return;
}
preptr = ptr->prev;
preptr->next = ptr->next;
ptr->next->prev = preptr;
free(ptr);
display();
}
int main(){
int option, item, val;
do {
printf("\n\n*****MAIN MENU*****");
printf("\n 1: Display list");
printf("\n 2: Insert a node at the beginning");
printf("\n 3: Insert a node at the end");
printf("\n 4: Add a node before a given node");
printf("\n 5: Add a node after a given node");
printf("\n 6: Delete a node from the beginning");
printf("\n 7: Delete a node at the end");
printf("\n 8: Delete a given node");
printf("\n 9: Delete before a given node");
printf("\n 10: Delete after a given node");
printf("\n 11: Exit");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option) {
case 1:
display();
break;
case 2:
printf("Enter the item to be inserted: ");
scanf("%d", &item);
insert_beg(item);
break;
case 3:
printf("Enter the item to be inserted: ");
scanf("%d", &item);
insert_end(item);
break;
case 4:
printf("Enter the item: ");
scanf("%d", &item);
printf("Enter the value before which you want to insert: ");
scanf("%d", &val);
insert_before(val, item);
break;
case 5:
printf("Enter the item to be inserted: ");
scanf("%d", &item);
printf("Enter the value after which you want to insert: ");
scanf("%d", &val);
insert_after(val, item);
break;
case 6:
delete_beg();
break;
case 7:
delete_last();
break;
case 8:
printf("Enter the item to be deleted: ");
scanf("%d", &item);
delete_node(item);
break;
case 9:
printf("Enter the value before which you want to delete: ");
scanf("%d", &val);
delete_before(val);
break;
case 10:
printf("Enter the value after which you want to delete: ");
scanf("%d", &val);
delete_after(val);
break;
}
} while(option != 11);
return 0;
}