This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp18_2910.c
308 lines (270 loc) · 5.33 KB
/
p18_2910.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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 18/2910
// CDLL Circular Doubly Linked List
// Dynamic Memory Allocation
#include<stdio.h>
#include<stdlib.h>
void ins_b();
void ins_e();
void ins_p();
void del_b();
void del_e();
void del_p();
void display_h();
void display_t();
void exit_free();
struct node{
int data;
struct node *prev;
struct node *next;
};
struct node *head = NULL;
struct node *tail = NULL;
struct node *temp;
void main(){
int ch;
while(1){
printf("\n\n *** CIRCULAR DOUBLY LINKED LIST [C - DLL] ***");
printf("\nInsert at >> 1. Beginning 2. End 3. Position");
printf("\nDelete at >> 4. Beginning 5. End 6. Position");
printf("\nDisplay >> 7. From Head 8. From Tail");
printf("\n >> 9. Exit");
printf("\nEnter choice: ");
scanf("%d", &ch);
switch(ch){
case 1: ins_b(); break;
case 2: ins_e(); break;
case 3: ins_p(); break;
case 4: del_b(); break;
case 5: del_e(); break;
case 6: del_p(); break;
case 7: display_h(); break;
case 8: display_t(); break;
default: exit_free(); return;
}
}
}
// Insertion at Start
// Choice 1
void ins_b(){
struct node *a = malloc(sizeof(struct node));
printf("\tEnter data: ");
scanf("%d", &(a->data));
if(head == NULL && tail == NULL){
a->prev = a; // change
a->next = a; // change
head = a;
tail = a;
}
else{
a->next = head;
a->prev = tail; // change
head->prev = a;
tail->next = a;
head = a;
}
printf("\tSuccessfully inserted %d at beginning", a->data);
}
// Insertion at end
// Choice 2
void ins_e(){
struct node *a = malloc(sizeof(struct node));
printf("\tEnter data: ");
scanf("%d", &(a->data));
if(head == NULL && tail == NULL){
a->prev = a; // change
a->next = a; // change
head = a;
tail = a;
}
else{
tail->next = a;
a->prev = tail;
a->next = head;
head->prev = a;
tail = a;
}
printf("\tSuccessfully inserted %d at end", a->data);
}
// Insertion at specific position
// Choice 3
void ins_p(){
int pos, i = 1;
printf("\tEnter position: ");
scanf("%d", &pos);
if(pos <= 0){
printf("\tError in position! Do not enter index");
return;
}
if(pos == 1){
ins_b();
return;
}
temp = head;
do{
temp = temp->next;
i++;
}while(temp != head && i < pos - 1);
/* for(temp = head; temp != NULL && i < (pos - 1); i++){*/
/* temp = temp->next;*/
/* }*/
// error with do while when number of elements is 1
// and pos = 2. Fix
if(i == pos - 1){
struct node *a = malloc(sizeof(struct node));
struct node *loc;
loc = temp->next;
printf("\tEnter data: ");
scanf("%d", &(a->data));
a->prev = temp;
a->next = loc;
temp->next = a;
if(loc == head){ // change ***
// Insertion at end
tail = a;
head->prev = tail;
}
else{
loc->prev = a;
}
printf("\tSuccessfully inserted %d at position %d", a->data, pos);
}
else{
printf("\tError in position!");
}
}
// Deletion of beginning node
// Choice 4
void del_b(){
if(head == NULL && tail == NULL){
printf("\n\tCLL is Empty");
return;
}
temp = head;
printf("\n\tSuccessfully deleted %d from the beginning", temp->data);
if(head != tail){
head = head->next;
head->prev = NULL;
}
else{
head = NULL;
tail = NULL;
}
free(temp);
}
// Deletion of node from end
// Choice 5
void del_e(){
if(head == NULL && tail == NULL){
printf("\n\tCLL is Empty");
return;
}
temp = tail;
printf("\n\tSuccessfully deleted %d from the end", temp->data);
if(head != tail){
tail = tail->prev;
tail->next = head; // change
}
else{
head = NULL;
tail = NULL;
}
free(temp);
}
// Deletion of node at specific position
// Choice 6
void del_p(){
if(head == NULL && tail == NULL){
printf("\n\tCLL is Empty");
return;
}
int pos, i = 1;
printf("\tEnter position: ");
scanf("%d", &pos);
if(pos <= 0){
printf("\tError in position! Do not enter index");
return;
}
if(pos == 1){
del_b();
return;
}
temp = head;
do{
temp = temp->next;
i++;
}while(temp != head && i < pos - 1);
/* for(temp = head; temp != NULL && i < (pos - 1); i++){*/
/* temp = temp->next;*/
/* }*/
struct node *loc;
loc = temp->next;
if(temp != head){
if(loc == tail){
del_e();
return;
}
else{
temp->next = loc->next;
loc->next->prev = temp;
printf("\tSuccessfully deleted %d from position %d", loc->data, pos);
free(loc);
}
}
else{
printf("\tError in position!");
}
}
// Display DLL - Head to Tail
// Choice 7
void display_h(){
if(head == NULL && tail == NULL){
printf("\n\tCLL is empty");
return;
}
printf("\n\tCuurent CLL >>>");
temp = head;
do{
printf(" %d <->", temp->data);
temp = temp->next;
}while(temp != head);
/* for(temp = head; temp != NULL; temp = temp->next){*/
/* printf(" %d <->", temp->data);*/
/* }*/
}
// Display DLL - Tail to Head - Reverse
// Choice 8
void display_t(){
if(head == NULL && tail == NULL){
printf("\n\tCLL is empty");
return;
}
printf("\n\tCuurent CLL >>>");
temp = tail;
do{
printf(" %d <->", temp->data);
temp = temp->prev;
}while(temp != tail);
/* for(temp = tail; temp != NULL; temp = temp->prev){*/
/* printf(" %d <->", temp->data);*/
/* }*/
}
// Final de-allocation of DLL
// de-allocation from head
// Choice 9
void exit_free(){
struct node *loc;
if(head != NULL){
tail->next = NULL;
temp = head;
loc = temp->next; // Next node
while(loc != NULL){
free(temp);
temp = loc;
loc = loc->next;
}
free(temp);
}
}