-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedList.cpp
More file actions
366 lines (285 loc) · 6.31 KB
/
Copy pathCircularLinkedList.cpp
File metadata and controls
366 lines (285 loc) · 6.31 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Name : Annie Bhalla
Roll No : 19HCS4009
Course : Bsc (H) Computer Science
Semester : 3
Subject : Data Structures
Title : Implementation of Circular Linked List
*/
#include<iostream>
using namespace std;
typedef int Elem;
class Cnode
{
public:
Cnode* next;
Elem elem;
friend class CircularLinkedList;
};
class CLinkedList
{
Cnode* cursor;
public:
CLinkedList(); // 1
~CLinkedList(); // 2
bool empty(); // 3
const Elem& front() const ; // 4
const Elem& back() const ; // 5
void advance(); // 6
void addFront(const Elem& x); // 7
void addBack(const Elem& x); // 8
void addAfter(const Elem& x, const Elem& y); // 9
void remove(); // 10
void removeFront(); // 11
void removeBack(); // 12
void removeElement(const Elem& x); // 13
void display(); // 14
Cnode* search(const Elem& x); // 15
void concatenate(CLinkedList& C); // 16
int count(); // 17
};
// (1) Constructor
CLinkedList::CLinkedList() : cursor(NULL) { }
// (2) Destructor
CLinkedList::~CLinkedList()
{
while( !empty() )
remove();
}
// (3) To check whether the list is empty or not
bool CLinkedList::empty()
{
return ( cursor == NULL);
}
// (4) fucntion to return the element following cursor
const Elem& CLinkedList::front() const
{
return ( cursor->next->elem);
}
// (5) to return the elemnt at cursor
const Elem& CLinkedList::back() const
{
return (cursor->elem);
}
// (6) to advance the cursor in the circular list
void CLinkedList::advance()
{
cursor=cursor->next;
}
// (7) to add element x in front of the list
void CLinkedList::addFront(const Elem& x)
{
Cnode* p= new Cnode;
p->elem= x;
if(empty())
{
p->next = p;
cursor=p;
}
else
{
p->next = cursor->next;
cursor->next= p;
}
}
// (8) to add element at the back of the list
void CLinkedList::addBack(const Elem& x)
{
Cnode* newnode = new Cnode;
newnode->elem= x;
if( empty())
{
newnode->next= newnode;
cursor= newnode;
}
else
{
newnode->next= cursor->next;
cursor->next = newnode;
cursor= newnode;
}
}
// (9) add an element y after an element x in the list
void CLinkedList::addAfter(const Elem& x, const Elem& y)
{
if ( empty()) throw " Function addAfter() : Empty List ";
bool flag=0;
Cnode* ptr = cursor->next;
do
{
if ( ptr->elem == x)
{
flag=1;
Cnode* newnode = new Cnode;
newnode->elem= y;
newnode->next= ptr->next;
ptr->next= newnode;
}
}while(ptr!=cursor->next);
if(!flag) throw " Function addAfter() : Element you are trying to find does not exist in the list ";
}
// (10) to remove the element following cursor
void CLinkedList::remove()
{
Cnode* old= cursor->next;
if( old== cursor)
cursor= NULL;
else
cursor->next= old->next;
delete old;
}
// (11) to remove an element from front
void CLinkedList::removeFront()
{
if(empty()) throw " Function removeFront() : Empty List " ;
Cnode* old = cursor->next ;
if( old == cursor)
cursor= NULL;
else
cursor->next = old->next;
delete old;
}
// (12) to remove a node from back
void CLinkedList::removeBack()
{
if( empty()) throw " Function removeBack() : Empty List ";
Cnode* old = cursor->next;
if(old==cursor)
cursor= NULL;
else
{
while(old->next!=cursor)
old=old->next;
cursor= old;
old= old->next;
cursor->next = old->next;
delete old;
}
}
// (13) to remove element x from list
void CLinkedList::removeElement(const Elem& x)
{
if( empty()) throw " Function removeElement() : Empty List ";
bool flag=0;
Cnode* ptr = cursor->next;
Cnode* q= cursor;
do
{
if(ptr->elem == x)
{
flag=1;
q->next = ptr->next;
if(ptr==cursor) cursor=q;
delete ptr;
}
ptr=ptr->next;
q=q->next;
}while( ptr!= cursor->next);
if(!flag)
throw " Function removeElement(): element not found ";
}
// (14) to display the list
void CLinkedList::display()
{
if(empty()) throw " Function display(): Empty List ";
cout<<endl;
Cnode* ptr= cursor->next;
do
{
cout<<ptr->elem<<" -> ";
ptr=ptr->next;
} while ( ptr != cursor->next);
}
// (15) to search an element in CLL
Cnode* CLinkedList::search(const Elem& x)
{
if(empty()) throw " Function search() : Empty List ";
Cnode* ptr= cursor->next;
do
{
if( ptr->elem == x)
return ptr;
}while( ptr != cursor->next);
return NULL;
}
// (16) to concatenate two CLL
void CLinkedList::concatenate(CLinkedList& C)
{
if( empty() && C.empty())
throw " Function concatenate() : Both lists are empty ";
if( empty())
{
C.display();
return;
}
if( C.empty())
{
display();
return;
}
Cnode* first= cursor;
Cnode* second = C.cursor->next;
C.cursor->next = cursor->next;
cursor= C.cursor;
first->next=second;
C.cursor= NULL;
display();
}
// (17) to count the number of nodes in CLL
int CLinkedList::count()
{
int count=0;
if ( empty())
return 0;
else
{
Cnode* ptr= cursor->next;
do
{
count++;
ptr=ptr->next;
} while( ptr!=cursor->next);
}
return count;
}
// driver code
int main()
{
try
{
CLinkedList C1, C2,C3,C4;
C1.addFront(5);
C1.addFront(4);
C1.addFront(2);
C1.addBack(6);
C1.addAfter(2,3);
C1.display();
cout<<endl;
C2.addBack(11);
C2.addFront(10);
C2.addFront(9);
C2.addFront(7);
C2.addAfter(7,8);
C2.display();
cout<<"\n ************** CONCATENATED LIST ****************";
C1.concatenate(C2);
int x= C1.count();
cout<<"\n Number of nodes in Concatenated List : "<<x;
cout<<endl;
cout<<"\n Removing elements from concatenated string "<<endl;
C1.removeFront();
C1.display();
cout<<endl;
C1.removeBack();
C1.display();
cout<<endl;
C1.removeElement(6);
C1.display();
cout<<endl;
C3.concatenate(C4);
}
catch(const char* str)
{
cout<<"\n Exception Occured : "<<str;
}
return 0;
}