-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularDoublyLinkedList.c
527 lines (479 loc) · 11.6 KB
/
CircularDoublyLinkedList.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/**
*
* @file CircularDoublyLinkedList.c
* @author Max Base ([email protected])
* @brief Circular Doubly Linked List (CDLL) is a variation of Doubly Linked List (DLL). In CDLL, the last node points to the first node and the first node points to the last node. It is a circular list and hence the name. It is also called a Ring List. It can be a singly circular linked list or doubly circular linked list.
* @version 0.1
* @date 2022-12-19
* @copyright Copyright (c) 2022
*
*/
#include <stdio.h> // for printf, scanf, NULL
#include <stdlib.h> // for malloc, free
#include <stdbool.h> // for bool type
typedef struct Node {
int data;
struct Node *prev;
struct Node *next;
} Node;
typedef struct {
Node *head;
Node *tail;
int size;
} CircularDoublyLinkedList;
/**
* @brief Create a new Circular Doubly Linked List object
*
* @return CircularDoublyLinkedList*
*/
CircularDoublyLinkedList* createNewCircularDoublyLinkedList() {
CircularDoublyLinkedList *list = (CircularDoublyLinkedList*) malloc(sizeof(CircularDoublyLinkedList));
list->head = NULL;
list->tail = NULL;
list->size = 0;
return list;
}
/**
* @brief Create a new Node object
*
* @param data
* @return Node*
*/
Node* createNewNode(int data) {
Node *node = (Node*) malloc(sizeof(Node));
node->data = data;
node->prev = NULL;
node->next = NULL;
return node;
}
/**
* @brief Insert a new node at the end of the list
*
* @param list
* @param data
*/
void insertAtEnd(CircularDoublyLinkedList *list, int data) {
Node *node = createNewNode(data);
if(list->head == NULL) {
list->head = node;
list->tail = node;
node->next = node;
node->prev = node;
} else {
list->tail->next = node;
node->prev = list->tail;
list->tail = node;
list->tail->next = list->head;
list->head->prev = list->tail;
}
list->size++;
}
/**
* @brief Insert a new node at the beginning of the list
*
* @param list
* @param data
*/
void insertAtBeginning(CircularDoublyLinkedList *list, int data) {
Node *node = createNewNode(data);
if(list->head == NULL) {
list->head = node;
list->tail = node;
node->next = node;
node->prev = node;
} else {
list->head->prev = node;
node->next = list->head;
list->head = node;
list->tail->next = list->head;
list->head->prev = list->tail;
}
list->size++;
}
/**
* @brief Insert a new node at the given index
*
* @param list
* @param data
* @param index
*/
void insertAtIndex(CircularDoublyLinkedList *list, int data, int index) {
if(index < 0 || index > list->size) {
printf("Index out of range!\n");
return;
}
if(index == 0) {
insertAtBeginning(list, data);
} else if(index == list->size) {
insertAtEnd(list, data);
} else {
Node *node = createNewNode(data);
Node *temp = list->head;
for(int i = 0; i < index - 1; i++) {
temp = temp->next;
}
node->next = temp->next;
temp->next->prev = node;
temp->next = node;
node->prev = temp;
list->size++;
}
}
/**
* @brief Delete the first node of the list
*
* @param list
*/
void deleteFirstNode(CircularDoublyLinkedList *list) {
if(list->head == NULL) {
printf("List is empty!\n");
return;
}
if(list->head == list->tail) {
free(list->head);
list->head = NULL;
list->tail = NULL;
} else {
Node *temp = list->head;
list->head = list->head->next;
list->head->prev = list->tail;
list->tail->next = list->head;
free(temp);
}
list->size--;
}
/**
* @brief Delete the last node of the list
*
* @param list
*/
void deleteLastNode(CircularDoublyLinkedList *list) {
if(list->head == NULL) {
printf("List is empty!\n");
return;
}
if(list->head == list->tail) {
free(list->head);
list->head = NULL;
list->tail = NULL;
} else {
Node *temp = list->tail;
list->tail = list->tail->prev;
list->tail->next = list->head;
list->head->prev = list->tail;
free(temp);
}
list->size--;
}
/**
* @brief Delete the node at the given index
*
* @param list
* @param index
*/
void deleteNodeAtIndex(CircularDoublyLinkedList *list, int index) {
if(index < 0 || index >= list->size) {
printf("Index out of range!\n");
return;
}
if(index == 0) {
deleteFirstNode(list);
} else if(index == list->size - 1) {
deleteLastNode(list);
} else {
Node *temp = list->head;
for(int i = 0; i < index; i++) {
temp = temp->next;
}
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
free(temp);
list->size--;
}
}
/**
* @brief Search for a node in the list
*
* @param list
* @param data
* @return true
* @return false
*/
bool search(CircularDoublyLinkedList *list, int data) {
Node *temp = list->head;
for(int i = 0; i < list->size; i++) {
if(temp->data == data) {
return true;
}
temp = temp->next;
}
return false;
}
/**
* @brief Print the list
*
* @param list
*/
void printList(CircularDoublyLinkedList *list) {
if(list->head == NULL) {
printf("List is empty!\n");
return;
}
Node *temp = list->head;
for(int i = 0; i < list->size; i++) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
/**
* @brief Print the list in reverse order
*
* @param list
*/
void printListReverse(CircularDoublyLinkedList *list) {
if(list->head == NULL) {
printf("List is empty!\n");
return;
}
Node *temp = list->tail;
for(int i = 0; i < list->size; i++) {
printf("%d ", temp->data);
temp = temp->prev;
}
printf("\n");
}
/**
* @brief Helper function for printListReverseRecursive
*
* @param node
*/
void printListReverseRecursiveHelper(Node *node) {
if(node->next == NULL) {
printf("%d ", node->data);
return;
}
printListReverseRecursiveHelper(node->next);
printf("%d ", node->data);
}
/**
* @brief Print the list in reverse order using recursion
*
* @param list
*/
void printListReverseRecursive(CircularDoublyLinkedList *list) {
if(list->head == NULL) {
printf("List is empty!\n");
return;
}
printListReverseRecursiveHelper(list->head);
printf("\n");
}
/**
* @brief Reverse the list
*
* @param list
*/
void reverseList(CircularDoublyLinkedList *list) {
if(list->head == NULL) {
printf("List is empty!\n");
return;
}
Node *temp = list->head;
Node *temp2 = list->tail;
for(int i = 0; i < list->size / 2; i++) {
int tempData = temp->data;
temp->data = temp2->data;
temp2->data = tempData;
temp = temp->next;
temp2 = temp2->prev;
}
}
/**
* @brief Get the size of the list
*
* @param list
* @return int
*/
int getSize(CircularDoublyLinkedList *list) {
return list->size;
}
/**
* @brief Get the data at the given index
*
* @param list
* @param index
* @return int
*/
int getDataAtIndex(CircularDoublyLinkedList *list, int index) {
if(index < 0 || index >= list->size) {
printf("Index out of range!\n");
return -1;
}
Node *temp = list->head;
for(int i = 0; i < index; i++) {
temp = temp->next;
}
return temp->data;
}
/**
* @brief Get the index of the given data
*
* @param list
* @param data
* @return int
*/
int getIndex(CircularDoublyLinkedList *list, int data) {
Node *temp = list->head;
for(int i = 0; i < list->size; i++) {
if(temp->data == data) {
return i;
}
temp = temp->next;
}
return -1;
}
/**
* @brief Get the first node
*
* @param list
* @return Node*
*/
Node *getFirstNode(CircularDoublyLinkedList *list) {
return list->head;
}
/**
* @brief Get the last node
*
* @param list
* @return Node*
*/
Node *getLastNode(CircularDoublyLinkedList *list) {
return list->tail;
}
/**
* @brief Get the node at the given index
*
* @param list
* @param index
* @return Node*
*/
Node *getNodeAtIndex(CircularDoublyLinkedList *list, int index) {
if(index < 0 || index >= list->size) {
printf("Index out of range!\n");
return NULL;
}
Node *temp = list->head;
for(int i = 0; i < index; i++) {
temp = temp->next;
}
return temp;
}
/**
* @brief Get the next node
*
* @param node
* @return Node*
*/
Node *getNextNode(Node *node) {
return node->next;
}
/**
* @brief Get the previous node
*
* @param node
* @return Node*
*/
Node *getPrevNode(Node *node) {
return node->prev;
}
/**
* @brief Get the data of the given node
*
* @param node
* @return int
*/
int getData(Node *node) {
if (node == NULL) return -1;
return node->data;
}
/**
* @brief Set the data of the given node
*
* @param node
* @param data
*/
void setData(Node *node, int data) {
node->data = data;
}
/**
* @brief Delete the list
*
* @param list
*/
void deleteList(CircularDoublyLinkedList *list) {
if(list->head == NULL) {
printf("List is empty!\n");
return;
}
Node *temp = list->head;
for(int i = 0; i < list->size; i++) {
Node *temp2 = temp->next;
free(temp);
temp = temp2;
}
free(list);
}
/**
* @brief Entry point
*
* @return int
*/
int main(int argc, char** argv)
{
// Create a new list
CircularDoublyLinkedList *list = createNewCircularDoublyLinkedList();
// Add some data
insertAtBeginning(list, 1);
insertAtBeginning(list, 2);
insertAtBeginning(list, 3);
insertAtBeginning(list, 4);
// Print the list
printList(list);
// Print the list in reverse order
printListReverse(list);
// Reverse the list
reverseList(list);
// Print the list
printList(list);
// Print the list
printList(list);
// Get the size of the list
printf("Size: %d\n", getSize(list));
// Get the data at the given index
printf("Data at index 2: %d\n", getDataAtIndex(list, 2));
// Get the index of the given data
printf("Index of data 3: %d\n", getIndex(list, 3));
// Get the first node
Node *firstNode = getFirstNode(list);
// Get the last node
Node *lastNode = getLastNode(list);
// Get the node at the given index
Node *nodeAtIndex = getNodeAtIndex(list, 2);
printf("Data of node at index 2: %d\n", getData(nodeAtIndex));
// Get the next node
Node *nextNode = getNextNode(firstNode);
printf("Data of next node: %d\n", getData(nextNode));
// Get the previous node
Node *prevNode = getPrevNode(lastNode);
printf("Data of previous node: %d\n", getData(prevNode));
// Get the data of the given node
printf("Data of first node: %d\n", getData(firstNode));
// Set the data of the given node
setData(firstNode, 5);
// Print the list
printList(list);
// Delete the list
deleteList(list);
return 0;
}