-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48_Circular_LinkedList.cpp
More file actions
109 lines (84 loc) · 1.85 KB
/
48_Circular_LinkedList.cpp
File metadata and controls
109 lines (84 loc) · 1.85 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
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
#include<iostream>
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
next = nullptr;
}
};
void insertAtTail(Node*& head, int data) {
Node* newNode = new Node(data);
if (head == nullptr) {
head = newNode;
newNode->next = head;
return;
}
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
void deleteAtHead(Node*& head) {
if (head == nullptr) return;
Node* toDelete = head;
if (head->next == head) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
head = head->next;
temp->next = head;
delete toDelete;
}
void deleteNodeCLL(Node*& head, int key) {
if (head == nullptr) return;
if (head->data == key) {
deleteAtHead(head);
return;
};
Node* temp = head;
while (temp->next != nullptr && temp->next->data != key) {
temp = temp->next;
}
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
}
void createCLL(Node*& head, int n) {
for (int i = 1; i <= n; i++) {
insertAtTail(head, i);
}
}
void printCLL(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
do {
std::cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
}
void printCLLR(Node* head, Node*& temp) {
static int flag = 0;
if (temp != head || flag == 0) {
flag = 1;
std::cout << temp->data << " ";
printCLLR(head, temp->next);
}
flag = 0;
}
int main() {
Node* CLL = nullptr;
createCLL(CLL, 5);
deleteNodeCLL(CLL, 1);
deleteNodeCLL(CLL, 3);
printCLLR(CLL, CLL);
return 0;
}