Skip to content

Commit 6d2b300

Browse files
Circular linked lists in C++
1 parent 08e3a9b commit 6d2b300

File tree

8 files changed

+754
-0
lines changed

8 files changed

+754
-0
lines changed
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<iostream>
7+
#include "node.h"
8+
#include "circular.h"
9+
10+
using namespace std;
11+
12+
CircularLinkedList::CircularLinkedList()
13+
{
14+
last = NULL;
15+
}
16+
17+
CircularLinkedList::CircularLinkedList(const CircularLinkedList& L)
18+
{
19+
if( L.last == NULL )
20+
{
21+
last = NULL;
22+
return;
23+
}
24+
Node *p1,*p2;
25+
26+
p1 = L.last->link;
27+
p2 = last = new Node(p1->info);
28+
last->link = last;
29+
p1 = p1->link;
30+
31+
while( p1 != L.last->link )
32+
{
33+
Node *temp = new Node(p1->info);
34+
temp->link = p2->link;
35+
p2->link = temp;
36+
p2 = p2->link;
37+
last = p2;
38+
p1 = p1->link;
39+
}
40+
}
41+
42+
CircularLinkedList::~CircularLinkedList()
43+
{
44+
if( isEmpty() )
45+
return;
46+
if( last->link == last )
47+
{
48+
delete last;
49+
last = NULL;
50+
return;
51+
}
52+
53+
Node *p;
54+
do
55+
{
56+
p = last->link->link;
57+
delete last->link;
58+
last->link = p;
59+
}while( p != last );
60+
delete last;
61+
last = NULL;
62+
}
63+
64+
inline bool CircularLinkedList::isEmpty() const
65+
{
66+
return last == NULL;
67+
}
68+
69+
void CircularLinkedList::displayList() const
70+
{
71+
if( isEmpty() )
72+
{
73+
cout << "List is empty\n";
74+
return;
75+
}
76+
77+
Node *p = last->link;
78+
do
79+
{
80+
cout << p->info<<" ";
81+
p = p->link;
82+
}while( p != last->link );
83+
84+
cout<<"\n";
85+
}
86+
87+
void CircularLinkedList::insertInEmptyList(int data)
88+
{
89+
Node *temp = new Node(data);
90+
last = temp;
91+
last->link = last;
92+
}
93+
94+
void CircularLinkedList::insertAtBeginning(int data)
95+
{
96+
if(isEmpty())
97+
{
98+
insertInEmptyList(data);
99+
return;
100+
}
101+
102+
Node *temp = new Node(data);
103+
temp->link = last->link;
104+
last->link = temp;
105+
}
106+
107+
void CircularLinkedList::insertAtEnd(int data)
108+
{
109+
if(isEmpty())
110+
{
111+
insertInEmptyList(data);
112+
return;
113+
}
114+
115+
Node *temp = new Node(data);
116+
temp->link = last->link;
117+
last->link = temp;
118+
last = temp;
119+
}
120+
121+
void CircularLinkedList::createList()
122+
{
123+
int n,data;
124+
125+
cout << "Enter the number of nodes : ";
126+
cin >> n;
127+
128+
if( n <= 0 )
129+
return;
130+
131+
cout << "Enter the first element to be inserted : ";
132+
cin >> data;
133+
insertInEmptyList(data);
134+
135+
Node *temp;
136+
for(int i=2; i<=n; i++)
137+
{
138+
cout << "Enter the element to be inserted : ";
139+
cin >> data;
140+
temp = new Node(data);
141+
temp->link = last->link;
142+
last->link = temp;
143+
last = temp;
144+
}
145+
}
146+
147+
void CircularLinkedList::insertAfter(int data,int x)
148+
{
149+
Node *p = last->link;
150+
do
151+
{
152+
if( p->info == x )
153+
break;
154+
p = p->link;
155+
}while( p != last->link );
156+
157+
if( p == last->link && p->info != x )
158+
cout << x <<" not present in the list \n";
159+
else
160+
{
161+
Node *temp = new Node(data);
162+
temp->link = p->link;
163+
p->link = temp;
164+
if ( p == last )
165+
last = temp;
166+
}
167+
}
168+
169+
void CircularLinkedList::deleteFirstNode()
170+
{
171+
if(isEmpty())
172+
{
173+
cout<<"List is empty\n";
174+
return;
175+
}
176+
if( last->link == last ) /*List has only one node*/
177+
{
178+
delete last;
179+
last = NULL;
180+
return;
181+
}
182+
Node *temp = last->link;
183+
last->link = temp->link;
184+
delete temp;
185+
}
186+
187+
188+
void CircularLinkedList::deleteLastNode()
189+
{
190+
if(isEmpty())
191+
{
192+
cout<<"List is empty\n";
193+
return;
194+
}
195+
if( last->link == last ) /*List has only one node*/
196+
{
197+
delete last;
198+
last = NULL;
199+
return;
200+
}
201+
202+
Node *p = last->link;
203+
while( p->link != last )
204+
p = p->link;
205+
p->link = last->link;
206+
delete last;
207+
last = p;
208+
}
209+
210+
void CircularLinkedList::deleteNode(int x)
211+
{
212+
if( isEmpty() )
213+
{
214+
cout<<"List is empty\n";
215+
return;
216+
}
217+
218+
/*Deletion of only node*/
219+
if( last->link == last && last->info == x )
220+
{
221+
delete last;
222+
last = NULL;
223+
return;
224+
}
225+
226+
/*Deletion of first node*/
227+
if( last->link->info == x )
228+
{
229+
Node *temp = last->link;
230+
last->link = temp->link;
231+
delete temp;
232+
return;
233+
}
234+
235+
Node *p = last->link;
236+
while( p->link != last->link )
237+
{
238+
if( p->link->info == x )
239+
break;
240+
p = p->link;
241+
}
242+
if( p->link == last->link )
243+
cout << x <<" not found in the list\n";
244+
else
245+
{
246+
Node *temp = p->link;
247+
p->link = temp->link;
248+
if( last->info == x )
249+
last = p;
250+
delete temp;
251+
}
252+
}
253+
254+
255+
void CircularLinkedList::concatenate(CircularLinkedList list)
256+
{
257+
if( last == NULL )
258+
{
259+
last = list.last;
260+
return;
261+
}
262+
263+
if( list.last == NULL )
264+
return;
265+
266+
Node *p = last->link;
267+
last->link = list.last->link;
268+
list.last->link = p;
269+
last = list.last;
270+
}
271+
272+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
#include<iostream>
7+
#include "node.h"
8+
#include "circular.h"
9+
10+
using namespace std;
11+
12+
int main()
13+
{
14+
CircularLinkedList list;
15+
list.createList();
16+
17+
int choice,data,item;
18+
19+
while(1)
20+
{
21+
cout << "1.Display list\n";
22+
cout << "2.Insert in empty list\n";
23+
cout << "3.Insert in the beginning of the list\n";
24+
cout << "4.Insert at the end of the list\n";
25+
cout << "5.Insert a node after a specified node\n";
26+
cout << "6.Delete first node\n";
27+
cout << "7.Delete last node\n";
28+
cout << "8.Delete any node\n";
29+
cout << "9.Quit\n";
30+
31+
cout << "Enter your choice : ";
32+
cin >> choice;
33+
34+
if(choice==9)
35+
break;
36+
37+
switch(choice)
38+
{
39+
case 1:
40+
list.displayList();
41+
break;
42+
case 2:
43+
cout << "Enter the element to be inserted : ";
44+
cin >> data;
45+
list.insertInEmptyList(data);
46+
break;
47+
case 3:
48+
cout << "Enter the element to be inserted : ";
49+
cin >> data;
50+
list.insertAtBeginning(data);
51+
break;
52+
case 4:
53+
cout << "Enter the element to be inserted : ";
54+
cin >> data;
55+
list.insertAtEnd(data);
56+
break;
57+
case 5:
58+
cout << "Enter the element to be inserted : ";
59+
cin >> data;
60+
cout << "Enter the element after which to insert : ";
61+
cin >> item;
62+
list.insertAfter(data,item);
63+
break;
64+
case 6:
65+
list.deleteFirstNode();
66+
break;
67+
case 7:
68+
list.deleteLastNode();
69+
break;
70+
case 8:
71+
cout << "Enter the element to be deleted : ";
72+
cin >> data;
73+
list.deleteNode(data);
74+
break;
75+
default:
76+
cout << "Wrong Choice\n";
77+
break;
78+
}
79+
}
80+
cout << "Exiting \n";
81+
}
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
using namespace std;
7+
8+
class CircularLinkedList
9+
{
10+
Node *last;
11+
bool isEmpty() const;
12+
13+
public:
14+
CircularLinkedList();
15+
CircularLinkedList(const CircularLinkedList& L);
16+
~CircularLinkedList();
17+
18+
void displayList() const;
19+
void insertInEmptyList(int data);
20+
void insertAtBeginning(int data);
21+
void insertAtEnd(int data);
22+
void insertAfter(int data,int x);
23+
void createList();
24+
void deleteNode(int x);
25+
void deleteFirstNode();
26+
void deleteLastNode();
27+
void concatenate(CircularLinkedList list);
28+
};
29+

0 commit comments

Comments
 (0)