-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_circular_linked_list.cpp
257 lines (216 loc) · 5.27 KB
/
03_circular_linked_list.cpp
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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int value){
data = value;
next = nullptr;
}
};
class CircularLinkedList{
public:
Node* head;
CircularLinkedList(){
head = nullptr;
}
void insertAtHead(int value){
Node* newNode = new Node(value);
if(head == nullptr){
head = newNode;
newNode->next = head;
}
else{
Node* temp = head;
while(temp->next != head){
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
head = newNode;
}
}
Node* traverse(int pos){
Node* temp = head;
for(int i = 1 ; i < pos && temp != nullptr && temp->next != head; i++){
temp = temp->next;
}
return temp;
}
void insertInBetween(int v, int p){
if(p == 0 ){
cout<< "Invalid Position. Try 1 or greater!" <<endl;
return;
}
if(p==1){
insertAtHead(v);
return;
}
Node* temp = traverse(p-1);
Node *newNode = new Node(v);
newNode->data = v;
newNode->next = temp->next;
temp->next = newNode;
}
void deleteAtHead(){
if(head == nullptr){
cout << "List is empty" << endl;
return;
}
if(head->next == head){
delete head;
head = nullptr;
return;
}
Node* temp = head;
Node* last = head;
while(last->next != head){
last = last->next;
}
head = head->next;
last->next = head;
delete temp;
}
void deleteAtPos(int p){
if(p <= 0){
cout << "Invalid Position. Try 1 or greater!" << endl;
return;
}
if(p == 1){
deleteAtHead();
return;
}
Node* prev = head;
Node* curr = head->next;
for (int i = 2; i < p; i++) {
prev = curr;
curr = curr->next;
if (curr == head) {
cout << "Invalid Position. Try a smaller number" << endl;
return;
}
}
if (curr == nullptr || curr == head) {
cout << "Invalid Position. Try a smaller number" << endl;
return;
}
prev->next = curr->next;
delete curr;
}
void display(){
if(head == nullptr){
cout << "List is empty" << endl;
return;
}
Node *temp = head;
do{
cout << temp->data << " -> ";
temp = temp->next;
}while(temp != head);
cout << "(head)" <<endl;
}
Node* search(int n){
if(head == nullptr){
cout << "List is empty" << endl;
return nullptr;
}
Node* temp = head;
do{
if (temp -> data == n){
cout << "Pointer of "<< n << " is: " << temp << endl;
return temp;
}
temp = temp->next;
} while (temp != head);
cout << "No match found" << endl;
return nullptr;
}
void reverse(){
if(head == nullptr || head->next == nullptr){
return;
}
Node* prev = nullptr;
Node *current = head;
Node *next = nullptr;
Node* last = head;
while(last->next != head){
last = last->next;
}
do{
next = current->next;
current->next = prev;
prev = current;
current = next;
} while (current != head);
head->next = prev;
head = prev;
}
void defaultList(){
insertAtHead(5);
insertAtHead(4);
insertAtHead(3);
insertAtHead(2);
insertAtHead(1);
cout<<"Default Circular List: ";
display();
}
void choice(){
int n;
cout<< "Chose on of the Following Option" << endl;
cout<< "1. Insert at Head" << endl;
cout<< "2. Insert at ith position" <<endl;
cout<< "3. Delete at Head" << endl;
cout<< "4. Delete at ith position" <<endl;
cout<< "5. Search for an element x in the singly linked list and return its pointer" << endl;
cout<< "6. Reverse the Linked List" <<endl;
cin>> n;
if (n == 1){
int x;
cout << "Enter the element you want to insert at Head"<< endl;
cin >> x;
insertAtHead(x);
}
if (n == 2){
int x, i;
cout << "Enter the element you want to insert"<< endl;
cin >> x;
cout << "Enter the Position"<< endl;
cin >> i;
insertInBetween(x,i);
}
if (n == 3){
deleteAtHead();
cout << "Element at head deleted"<< endl;
}
if (n == 4){
int i;
cout << "Enter the Position you want to delete"<< endl;
cin >> i;
deleteAtPos(i);
}
if (n == 5){
int x;
cout << "Enter the element you want to search"<< endl;
cin >> x;
search(x);
}
if (n == 6){
reverse();
}
else if(n>6 || n<1){
cout << "Invalid Option" << endl;
}
}
};
int main(){
CircularLinkedList list;
cout << "**********************************************************************************" << endl;
list.defaultList();
cout << "**********************************************************************************" << endl;
list.choice();
cout<< "Circular linked list: ";
list.display();
cout << endl;
return 0;
}