-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
251 lines (217 loc) · 6.69 KB
/
main.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
#include<iostream>
using namespace std;
class node{
private:
int data;
node* next;
node* prev;
public:
int getData(){
return data;
}
void setData(int data){
this->data = data;
}
node * getNext(){
return next;
}
void setNext(node * next){
this->next = next;
}
node * getPrev(){
return prev;
}
void setPrev(node * prev){
this->prev = prev;
}
};
class Circular_linked_list{
private:
int size;
node* current;
public:
Circular_linked_list(){
size = 0;
current = nullptr;
}
~Circular_linked_list(){
if(current == nullptr){
return;
}
node * temp= current;
while(temp->getNext() != current){
node * tempForDelete = temp;
temp= temp->getNext();
delete tempForDelete;
}
delete temp;
temp = nullptr;
current = nullptr;
size = 0;
}
int getSize(){
return size;
}
void push(int data){
node * new_node = new node;
// first node
if(current == nullptr){
new_node->setData(data);
new_node->setNext(new_node);
new_node->setPrev(new_node);
current = new_node;
size++;
return;
}
// select last node
node * last = current->getPrev();
new_node->setData(data);
new_node->setNext(current);
new_node->setPrev(last);
last->setNext(new_node);
current->setPrev(new_node);
current = new_node;
size++;
}
void pop(){
// empty list
if(current == nullptr){
return;
}
// one node left
if(size == 1){
delete current;
current = nullptr;
size--;
return;
}
// select the last node
node * last = current;
while(last->getNext() != current){
last = last->getNext();
}
node * first = current;
current = current->getNext();
current->setPrev(last);
delete first;
first = nullptr;
last->setNext(current);
size--;
}
void pop(int data){
// empty list
if(current == nullptr){
return;
}
node * toBeRemoved = current, * prevNode = nullptr;
while(toBeRemoved->getData() != data){
if(toBeRemoved->getNext() == current){
cout << data << " is not in the list!" << endl;
return;
}
prevNode = toBeRemoved;
toBeRemoved = toBeRemoved->getNext();
}
// Check if node is the only node in list
if (toBeRemoved->getNext() == current && prevNode == nullptr){ // size == 1
delete toBeRemoved;
size = 0;
current = nullptr;
return;
}
// deleting first node
if(toBeRemoved == current){
this->pop();
return;
}
// deleting last node
if(toBeRemoved->getNext() == current){ // current->getPrev() == toBeRemoved
prevNode->setNext(current);
current->setPrev(prevNode);
delete toBeRemoved;
size--;
return;
}
// deleting a middle node
node * nextNode = toBeRemoved->getNext();
prevNode->setNext(nextNode);
nextNode->setPrev(prevNode);
delete toBeRemoved;
size--;
}
void push_after(int new_data,int pa_data){
node * new_node = new node();
new_node->setData(new_data);
node * temp = current;
while(temp->getData() != pa_data){
// the case where pa_data does not exist
if(temp->getNext() == current){
cout << pa_data << " is not in the list to push after it!" << endl;
return;
}
temp = temp->getNext();
}
node * nextNode = temp->getNext();
temp->setNext(new_node);
new_node->setPrev(temp);
new_node->setNext(nextNode);
nextNode->setPrev(new_node);
size++;
}
void push_before(int new_data,int pb_data){
node * new_node = new node();
new_node->setData(new_data);
node * temp = current;
while(temp->getData() != pb_data){
// the case where pb_data does not exist
if(temp->getNext() == current){
cout << pb_data << " is not in the list to push after it!" << endl;
return;
}
temp = temp->getNext();
}
// for the first node to correctly set the * current
if(temp == current){
this->push(new_data);
return;
}
node * prevNode = temp->getPrev();
temp->setPrev(new_node);
new_node->setNext(temp);
new_node->setPrev(prevNode);
prevNode->setNext(new_node);
size++;
}
void print(){
if(current == nullptr){
cout << "List is empty" << endl;
return;
}
node * temp = current;
do{
cout << temp->getData() << "\t";
temp = temp->getNext();
}
while (temp != current);
cout << endl;
}
};
int main(){
Circular_linked_list list;
list.push(12);
list.push(22);
list.pop(12);
list.push(32);
list.print();
list.push_after(85, 22);
list.print();
list.push_before(1400, 32);
list.print();
list.pop();
list.push_after(666, 85);
list.print();
list.push_before(99, 666);
list.print();
cout << "Size of list : " << list.getSize() << endl;
return 0;
}