forked from HarshwardhanPatil07/HactoberFest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularDoublyLinkedList
191 lines (179 loc) · 5.03 KB
/
CircularDoublyLinkedList
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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int x) {
data = x;
next = this;
prev = this;
}
};
class CircularDoublyLinkedList {
public:
Node* head = nullptr;
void insertBegin(int x) {
Node* newNode = new Node(x);
if (head == nullptr) {
head = newNode;
} else {
newNode->next = head;
newNode->prev = head->prev;
head->prev->next = newNode;
head->prev = newNode;
head = newNode;
}
}
void insertEnd(int x) {
Node* newNode = new Node(x);
if (head == nullptr) {
head = newNode;
} else {
newNode->next = head;
newNode->prev = head->prev;
head->prev->next = newNode;
head->prev = newNode;
}
}
void insertAtPosition(int x, int pos) {
if (pos < 1) {
cout << "Invalid position." << endl;
return;
}
Node* newNode = new Node(x);
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
int count = 1;
while (count < pos - 1) {
current = current->next;
count++;
}
newNode->next = current->next;
newNode->prev = current;
current->next->prev = newNode;
current->next = newNode;
}
}
void deleteBegin() {
if (head == nullptr) {
cout << "Empty list" << endl;
} else {
Node* temp = head;
if (head->next == head) {
head = nullptr;
} else {
head->prev->next = head->next;
head->next->prev = head->prev;
head = head->next;
}
delete temp;
}
}
void deleteEnd() {
if (head == nullptr) {
cout << "Empty list" << endl;
} else {
Node* temp = head->prev;
if (head->next == head) {
head = nullptr;
} else {
head->prev = temp->prev;
temp->prev->next = head;
}
delete temp;
}
}
void deleteAtPosition(int pos) {
if (head == nullptr) {
cout << "Empty list" << endl;
} else if (pos < 1) {
cout << "Invalid position." << endl;
} else {
Node* current = head;
int count = 1;
while (count < pos && current->next != head) {
current = current->next;
count++;
}
if (count == pos) {
current->prev->next = current->next;
current->next->prev = current->prev;
delete current;
} else {
cout << "Position not found." << endl;
}
}
}
void display() {
if (head == nullptr) {
cout << "Empty list" << endl;
} else {
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
}
};
int main() {
CircularDoublyLinkedList myList;
int choice, n, pos;
do {
cout << "1. Insert at the beginning" << endl;
cout << "2. Insert at the end" << endl;
cout << "3. Insert at a specific position" << endl;
cout << "4. Delete from the beginning" << endl;
cout << "5. Delete from the end" << endl;
cout << "6. Delete from a specific position" << endl;
cout << "7. Display the list" << endl;
cout << "8. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Input Element to insert: ";
cin >> n;
myList.insertBegin(n);
break;
case 2:
cout << "Input element to insert: ";
cin >> n;
myList.insertEnd(n);
break;
case 3:
cout << "Input element to insert: ";
cin >> n;
cout << "Enter position: ";
cin >> pos;
myList.insertAtPosition(n, pos);
break;
case 4:
myList.deleteBegin();
break;
case 5:
myList.deleteEnd();
break;
case 6:
cout << "Enter position to be deleted: ";
cin >> pos;
myList.deleteAtPosition(pos);
break;
case 7:
myList.display();
break;
case 8:
cout << "Exiting the program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
} while (choice != 8);
return 0;
}