Skip to content

Commit e36e8e4

Browse files
Implemented Circular Linked List.
1 parent 364add2 commit e36e8e4

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
//Implementation of Circular Linked List
2+
3+
#include<iostream>
4+
using namespace std;
5+
6+
template<class T>
7+
struct NODE
8+
{
9+
T info; //Information in the node.
10+
NODE<T> *next; //Points to the next node.
11+
};
12+
13+
typedef NODE<char>* NPTR;
14+
15+
template<class T>
16+
class CLL
17+
{
18+
NPTR Head; //Points to the first node.
19+
public:
20+
CLL() //Constructor
21+
{
22+
Head=NULL;
23+
}
24+
~CLL() //Destructor
25+
{
26+
delete Head;
27+
}
28+
NPTR GetNode(); //To get a new node.
29+
NPTR SearchNode(T); //To search a node in the linked list.
30+
void Create(T); //To create a new linked list.
31+
void InsertBefore(T, T); //To insert a new node BEFORE the given node in the linked list.
32+
void InsertAfter(T, T); //To insert a new node AFTER the given node in the linked list.
33+
void DeleteNode(T); //To delete a node in the linked list.
34+
void UpdateNode(T, T); //To update a node of the linked list.
35+
int Size(); //Returns the size of linked list.
36+
void Traverse(); //To display the linked list.
37+
void Reverse(); //To reverse the linked list.
38+
};
39+
40+
template<class T>
41+
NPTR CLL<T>::GetNode()
42+
{
43+
return new NODE<T>;
44+
}
45+
46+
template<class T>
47+
NPTR CLL<T>::SearchNode(T elt)
48+
{
49+
NPTR P=Head;
50+
if (P!=NULL)
51+
{
52+
while (P->next!=Head && P->info!=elt)
53+
P=P->next;
54+
if (P->info==elt)
55+
return P;
56+
}
57+
return NULL;
58+
}
59+
60+
template<class T>
61+
void CLL<T>::Create(T elt)
62+
{
63+
NPTR P=GetNode();
64+
P->info=elt;
65+
P->next=P;
66+
Head=P;
67+
cout<<"\nLinked list successfully created!!!"<<endl<<endl;
68+
}
69+
70+
template<class T>
71+
void CLL<T>::InsertBefore(T new_elt, T before_elt)
72+
{
73+
NPTR P=SearchNode(before_elt);
74+
if (P==NULL)
75+
{
76+
cout<<"\nERROR!!!\nNode before which you want to insert the new node is not found."<<endl<<endl;
77+
}
78+
else
79+
{
80+
NPTR Q=GetNode();
81+
Q->info=new_elt;
82+
Q->next=P;
83+
NPTR R=Head;
84+
while (R->next!=P)
85+
R=R->next;
86+
R->next=Q;
87+
if (P==Head)
88+
Head=Q;
89+
cout<<"\nSUCCESS!!!\nNew node is successfully inserted before the given node."<<endl<<endl;
90+
}
91+
}
92+
93+
template<class T>
94+
void CLL<T>::InsertAfter(T new_elt, T after_elt)
95+
{
96+
NPTR P=SearchNode(after_elt);
97+
if (P==NULL)
98+
{
99+
cout<<"\nERROR!!!\nNode after which you want to insert the new node is not found."<<endl<<endl;
100+
}
101+
else
102+
{
103+
NPTR Q=GetNode();
104+
Q->info=new_elt;
105+
Q->next=P->next;
106+
P->next=Q;
107+
cout<<"\nSUCCESS!!!\nNew node is successfully inserted after the given node."<<endl<<endl;
108+
}
109+
}
110+
111+
template<class T>
112+
void CLL<T>::DeleteNode(T elt)
113+
{
114+
NPTR P=SearchNode(elt);
115+
if (P==NULL)
116+
{
117+
cout<<"\nERROR!!!\nNode that you want to delete is not found!"<<endl<<endl;
118+
}
119+
else
120+
{
121+
if (P->next==P) //If there is only one node (i.e. P=Head).
122+
{
123+
Head=NULL;
124+
}
125+
else
126+
{
127+
if (P==Head)
128+
Head=Head->next;
129+
NPTR Q=Head;
130+
while (Q->next!=P)
131+
Q=Q->next;
132+
Q->next=P->next;
133+
134+
}
135+
delete P;
136+
cout<<"\nSUCCESS!!!\nThe node is successfully deleted."<<endl<<endl;
137+
}
138+
}
139+
140+
template<class T>
141+
void CLL<T>::UpdateNode(T prev_elt, T new_elt)
142+
{
143+
NPTR P=SearchNode(prev_elt);
144+
if (P==NULL)
145+
{
146+
cout<<"\nERROR!!!\nNode that you want to update is not found."<<endl<<endl;
147+
}
148+
else
149+
{
150+
P->info=new_elt;
151+
cout<<"\nSUCCESS!!!\nNode is successfully updated."<<endl<<endl;
152+
}
153+
}
154+
155+
template<class T>
156+
int CLL<T>::Size()
157+
{
158+
int count=0;
159+
if (Head!=NULL)
160+
{
161+
for (NPTR P=Head; P->next!=Head; P=P->next, count++);
162+
++count;
163+
}
164+
return count;
165+
}
166+
167+
template<class T>
168+
void CLL<T>::Traverse()
169+
{
170+
if (Head==NULL)
171+
{
172+
cout<<"\nThe Linked list is empty.";
173+
}
174+
else
175+
{
176+
NPTR P=Head;
177+
178+
do
179+
{
180+
cout<<" --> "<<P->info;
181+
P=P->next;
182+
}
183+
while(P!=Head);
184+
cout<<" --> ";
185+
}
186+
cout<<endl<<endl;
187+
}
188+
189+
template<class T>
190+
void CLL<T>::Reverse()
191+
{
192+
if (Head==NULL)
193+
cout<<"\nThe Linked list is empty.";
194+
else
195+
{
196+
NPTR P=NULL;
197+
NPTR Q;
198+
NPTR R=Head;
199+
while (Head->next!=R)
200+
{
201+
NPTR Q=Head->next;
202+
Head->next=P;
203+
P=Head;
204+
Head=Q;
205+
}
206+
Head->next=P;
207+
R->next=Head;
208+
cout<<"\nSUCCESS!!! The linked list is successfully reversed.";
209+
}
210+
cout<<endl<<endl;
211+
}
212+
213+
int main()
214+
{
215+
cout<<"********************************** CIRCULAR LINKED LIST ********************************** ";
216+
CLL<char> CL_list;
217+
char choice;
218+
int flag=1;
219+
char element1, element2;
220+
while(flag)
221+
{
222+
cout<<"\n1. Create";
223+
cout<<"\n2. Traverse";
224+
cout<<"\n3. Insert Before";
225+
cout<<"\n4. Insert After";
226+
cout<<"\n5. Delete Node";
227+
cout<<"\n6. Update Node";
228+
cout<<"\n7. Size";
229+
cout<<"\n8. Reverse";
230+
cout<<"\n9. Exit";
231+
cout<<"\n\nEnter choice : ";
232+
cin>>choice;
233+
switch(choice)
234+
{
235+
case '1' : cout<<"\nEnter first node : ";
236+
cin>>element1;
237+
CL_list.Create(element1);
238+
break;
239+
case '2' : cout<<endl;
240+
CL_list.Traverse();
241+
break;
242+
case '3' : cout<<"\nEnter the node you want to insert : ";
243+
cin>>element1;
244+
cout<<"\nEnter the node before which you want to insert your node : ";
245+
cin>>element2;
246+
CL_list.InsertBefore(element1,element2);
247+
break;
248+
case '4' : cout<<"\nEnter the node you want to insert : ";
249+
cin>>element1;
250+
cout<<"\nEnter the node after which you want to insert your node : ";
251+
cin>>element2;
252+
CL_list.InsertAfter(element1,element2);
253+
break;
254+
case '5' : cout<<"\nEnter the node that you want to delete : ";
255+
cin>>element1;
256+
CL_list.DeleteNode(element1);
257+
break;
258+
case '6' : cout<<"\nEnter the node you want to update : ";
259+
cin>>element1;
260+
cout<<"\nEnter the new node : ";
261+
cin>>element2;
262+
CL_list.UpdateNode(element1,element2);
263+
break;
264+
case '7' : cout<<"\nThe size of the linked list is : "<<CL_list.Size()<<endl<<endl;
265+
break;
266+
case '8' : CL_list.Reverse();
267+
break;
268+
case '9' : flag=0;
269+
break;
270+
default : cout<<"\n\nERROR!!! You entered a wrong choice."<<endl<<endl;
271+
}
272+
}
273+
cout<<endl;
274+
system("pause");
275+
return 0;
276+
}

0 commit comments

Comments
 (0)