-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_Circular_Linked_List.cpp
More file actions
52 lines (46 loc) · 1.02 KB
/
03_Circular_Linked_List.cpp
File metadata and controls
52 lines (46 loc) · 1.02 KB
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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
Node *next;
}*head;
Node *createNewNode(int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->value = value;
newNode->next = NULL;
return newNode;
}
void pushTail(int value) {
Node *newNode = createNewNode(value);
if(!head) {
head = newNode;
head->next = head;
} else {
Node *curr = head;
while(curr->next != head) {
curr = curr->next;
}
curr->next = newNode;
newNode->next = head;
}
}
void printCircularLL() {
if(!head) {
printf("LL Empty\n");
} else {
Node *curr = head->next;
printf("%d -> ", head->value);
while(curr != head) {
printf("%d -> ", curr->value);
curr = curr->next;
}
printf("%d ", head->value);
}
}
int main() {
pushTail(1);
pushTail(2);
pushTail(3);
printCircularLL();
return 0;
}