-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCircular_DLL.c
385 lines (304 loc) · 11.9 KB
/
Circular_DLL.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
//To implement insertion and deletion operations in a circular doubly linked list using menu driven flow of control
//02.03.19
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#include<conio.h>
//Macro for easy use of malloc for memory allocation
#define Malloc(a) (a*)(malloc(sizeof(a)))
//Self referential structure for node creation in linked list
typedef struct nodeStructure{
int data;//data storage
struct nodeStructure *next;//points to next node in list
struct nodeStructure *previous;//points to previous node in list
};
//Declaration of first and last node pointers
struct nodeStructure *firstNode;
struct nodeStructure *lastNode;
//Function prototype and declaration for operations
//For inserting the nodes
void insertInList();
void putNodeAtStart();
void putNodeAtEnd();
void putNodeAtPosition();
//For deleting the nodes
void deleteFromList();
void removeNodeFromStart();
void removeNodeFromEnd();
void removeNodeAtPosition();
//Get values to store in the node
int getNodeValue();
//To display the nodes in the list
void getNodes();
//Searching for a user input data value in nodes of the linked list
struct nodeStructure* searchInNodes();
//main() opens
void main(){
int answer;
//For continuous display of switch-case menu
while(1){
system("cls"); //Clears the screen before each run of menu
printf("\nCircular Double Linked List Operations\n");
printf("01. Insertion in linked list\n");
printf("02. Deletion from linked list\n");
printf("03. Display the linked list\n");
printf("04. Exit\n");
scanf("%d", &answer);
//Switch-case for the user choice
switch (answer){
case 1: //For insertion in the linked list
insertInList();
break;
case 2: //For deletion from the linked list
deleteFromList();
break;
case 3: //For displaying the nodes in list
getNodes();
break;
default: //Default case for exiting the program
exit(0);
break;
}
}
getch();
}//main() closes
//Function definition for operations
struct nodeStructure* searchInNodes(){
struct nodeStructure *searchNode;
int valueFound = 0, searchValue;
//Get value to search from user
printf("Enter value to search \n");
scanf("%d", &searchValue);
//Declare new node for traversing the list
struct nodeStructure *scanNode;
scanNode = firstNode;
//Traverse the list till the lastNode is encountered (which points to the first node)
searchNode = NULL;
if (scanNode->data == searchValue){ //If value is found in the first node
valueFound = 1;
searchNode = firstNode;
}
scanNode = scanNode->next; //Moves the pointer forward to the next node
//Loop is entered through the second node and stops when scanNode takes the address of the
//first node through the last node
while(scanNode != firstNode){
if(scanNode->data == searchValue){ //If value is found in a node
valueFound = 1;
searchNode = scanNode;
}
scanNode = scanNode->next;
}
//Check if value is found or not
if (valueFound){
printf("Value is found \n");
}
else{
printf("Value not found\n");
}
return searchNode;
}
int getNodeValue(){ //Gets data to be stored in node from the user
int value;
printf("Enter value in node\n");
scanf("%d", &value);
return value;
}
void insertInList(){
struct nodeStructure *newNode;
int insertAnswer;
//Allocate memory for a node and pass it's address to newNode
newNode = Malloc(struct nodeStructure);
if (!firstNode){ //If the list doesn't exist
//Get value for node
newNode->data = getNodeValue();
//Initialize newNode as the first and last nodes
firstNode = newNode;
lastNode = firstNode;
//Initialize the node pointers to create a self referential node
newNode->next = lastNode;
newNode->previous = firstNode;
}
else{ //If the list exists
//Menu to display for insertion
system("cls");
printf("Insertion In Linked List\n");
printf("01. At the beginning\n");
printf("02. At the end\n");
printf("03. At a position\n");
scanf("%d", &insertAnswer);
//Switch case for user choice to insert node
switch (insertAnswer){
case 1: //Inserts a node at the beginning of the list
putNodeAtStart();
break;
case 2: //Inserts a node at the end of the list
putNodeAtEnd();
break;
case 3: //Inserts a node at a position given by the user
putNodeAtPosition();
break;
default:
break;
}
}
getch();
}
void putNodeAtStart(){
struct nodeStructure *newNode;
//Allocate memory for a node and pass it's address to newNode
newNode = Malloc(struct nodeStructure);
//Get value for node
newNode->data = getNodeValue();
//Placing the node in the circular doubly linked list at first position
newNode->next = firstNode;
firstNode->previous = newNode;
newNode->previous = lastNode;
lastNode->next = newNode;
firstNode = newNode;
}
void putNodeAtEnd(){
struct nodeStructure *newNode;
//Allocate memory for a node and pass it's address to newNode
newNode = Malloc(struct nodeStructure);
//Get value for node
newNode->data = getNodeValue();
//Placing the node in the doubly linked list at the end position
lastNode->next = newNode;
newNode->previous = lastNode;
newNode->next = firstNode;
firstNode->previous = newNode;
lastNode = newNode;
}
void putNodeAtPosition(){
struct nodeStructure *newNode, *searchNode;
//Get the node with the given search value
searchNode = searchInNodes();
if(searchNode != NULL){ //if search value is found
if(searchNode == lastNode){ //If the searched value is found in the last node
putNodeAtEnd();
}
else if(searchNode ==firstNode){ //If the searched value is found in the first node
putNodeAtStart();
}
else{
//Allocate memory for a node and pass it's address to newNode
newNode = Malloc(struct nodeStructure);
//Get value for node
newNode->data = getNodeValue();
//Attaches the new node after the search node
newNode->next = searchNode->next;
searchNode->next = newNode;
newNode->previous = searchNode;
//Traverse both the nodes forward to create reverse linkage to the newNode by the node after the searched node
searchNode = searchNode->next;
newNode = newNode->next;
newNode->previous = searchNode;
}
}
else{ //if search value isn't found exit the current function
return;
}
}
void deleteFromList(){
int insertAnswer;
if (!firstNode){ //If the list doesn't exist
printf("No nodes exist underflow\n");
}
else{ //If the list exists
//Menu to display for insertion
system("cls");
printf("Deletion In Linked List\n");
printf("01. At the beginning\n");
printf("02. At the end\n");
printf("03. At a position\n");
scanf("%d", &insertAnswer);
//Switch case for user choice to insert node
switch (insertAnswer){
case 1: //Remove the first node in linked list
removeNodeFromStart();
break;
case 2: //Removes last node from linked list
removeNodeFromEnd();
break;
case 3: //Removes node at a position given by the user
removeNodeAtPosition();
break;
default:
break;
}
}
getch();
}
void removeNodeFromStart(){
//initialize a new node to store address of node to be deleted
struct nodeStructure *deleteNode;
deleteNode = firstNode;
//Make the second node the first node and delete the original first node
firstNode = firstNode->next;
firstNode->previous = lastNode;
lastNode->next = firstNode; //Attaches the last node to the new first node
free(deleteNode); //frees the memory space
printf("Node deleted successfully\n");
}
void removeNodeFromEnd(){
//initialize a new node to store address of node to be deleted
struct nodeStructure *deleteNode;
//Makes the second last node as the last node and deletes the original last node
deleteNode = lastNode;
lastNode = lastNode->previous;
lastNode->next = firstNode;
firstNode->previous = lastNode; //Attaches the first node to the new last node
free(deleteNode); //frees the memory space
printf("Node deleted successfully\n");
}
void removeNodeAtPosition(){
//initialize a new node to store address of node to be deleted, the node before it and after it
struct nodeStructure *deleteNode, *previousNode, *nextNode;
//Get the node with the search value
deleteNode = searchInNodes();
if(deleteNode != NULL){ //If the search value is found
if(deleteNode == lastNode){
removeNodeFromEnd();
return;
}
else if(deleteNode == firstNode){
removeNodeFromStart();
return;
}
else{
previousNode = deleteNode->previous; //initialize the previous node
//Break connections from the node to be deleted
previousNode->next = deleteNode->next;
nextNode = deleteNode->next;
nextNode->previous = previousNode;
free(deleteNode); //frees the memory space
printf("Node deleted successfully\n");
}
}
else{
return;
}
}
void getNodes(){ //Displays the list using double arrow connections
struct nodeStructure *scanNode;
scanNode = firstNode;
if(firstNode){
printf("The linked list is \n");
printf("HEAD");
printf(" < -- > %d", firstNode->data);
scanNode = firstNode->next;
//Loop is entered through the second node and stops when scanNode takes the address of the
//first node through the last node
while (scanNode != firstNode){
printf(" < -- > %d",scanNode->data);
scanNode = scanNode->next;
}
printf(" < -- > HEAD"); //Last node points to NULL
}
else{
printf("List is empty, No nodes found\n");
}
getch();
}
//Written in ANSI C for the GNU-GCC compiler
//Elit Altum