forked from lata-arora/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_singly_linklist.cpp
More file actions
259 lines (207 loc) · 3.75 KB
/
circular_singly_linklist.cpp
File metadata and controls
259 lines (207 loc) · 3.75 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
#include<iostream.h>
#include<conio.h>
#include<process.h>
class node
{
private:
int info;
node *next;
friend class linklist;
public:
node(int el,node *p=0);
};
node::node(int el,node *p)
{
info=el;
next=p;
}
class linklist
{
node *tail;
public:
linklist()
{
tail=0;
}
void insert(int ele);
void display();
void reverse();
void isinlist(int ele);
int t_delete();
void delete_p(int ele);
};
void linklist::insert(int ele)
{
if(tail==0)
{
tail=new node(ele);
tail->next=tail;
}
else
{
tail->next=new node(ele,tail->next);
tail=tail->next;
}
}
void linklist::display()
{
node *temp=tail;
if(temp)
{
cout<<"\n\n\tCONTENTS OF CIRCULAR LINKLIST IS AS FOLLOWS";
while(temp->next!=tail)
{
cout<<" "<<temp->info;
temp=temp->next;
}
cout<<" "<<temp->info;
}
else
{
cout<<"\n\n\tLINKLIST IS EMPTY";
}
}
void linklist::isinlist(int ele)
{
node *temp=tail;
for(temp;temp->next!=tail&&temp->info!=ele;temp=temp->next);
{
if(temp->info==ele)
{
cout<<"\n\n\tFOUND";
}
else
{
cout<<"\n\n\tNOT FOUND";
}
}
}
void linklist::reverse()
{
if(tail)
{
if(tail==tail->next)
{
return;
}
else
{
node *temp,*ptr,*prev,*p;
prev=tail;
temp=tail->next;
p=tail->next;
while(temp!=tail)
{
ptr=temp->next;
temp->next=prev;
prev=temp;
temp=ptr;
}
ptr=temp->next;
temp->next=prev;
prev=temp;
temp=ptr;
tail=p;
}
}
}
void linklist::delete_p(int ele)
{
if(tail)
{
if(tail==tail->next&&tail->info==ele)
{
tail=0;
return;
}
else
{
node *temp=tail;
for(temp;temp->next!=tail&&temp->info!=ele;temp=temp->next);
if(temp->info==ele)
{
if(temp==tail)
{
tail=tail->next;
}
else
{ node *ptr;
for(ptr=tail;ptr->next!=temp;ptr=ptr->next);
ptr->next=temp->next;
}
delete temp;
}
else
{
cout<<"\n\n\tELEMENT NOT FOUND IN THE LIST";
}
}
}
}
int linklist::t_delete()
{
if(tail)
{
node *ptr,*temp;
ptr=tail;
int x=ptr->info;
if(tail==tail->next)
tail=0;
else
{
for(temp=tail;temp->next!=tail;temp=temp->next);
temp->next=tail->next;
tail=tail->next;
}
delete ptr;
return x;
}
else return -1;
}
void main()
{
int choice,ele;
char ch;
linklist l1;
do
{
clrscr();
cout<<"\n\n\tC.I.R.C.U.L.A.R.. L.I.N.K. L.I.S.T";
cout<<"\n\n\t-----------------------------";
cout<<"\n\n\tPRESS 1 TO ADD TO TAIL";
cout<<"\n\n\tPRESS 2 TO SEARCH ANY ELEMENT";
cout<<"\n\n\tPRESS 3 TO DELETE TAIL";
cout<<"\n\n\tPRESS 4 TO REVERSE";
cout<<"\n\n\tPRESS 5 TO DELETE A PARTUICULAR ELEMENT";
cout<<"\n\n\tENTER YOUR CHOICE";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n\n\tENTER THE ELEMENT : ";
cin>>ele;
l1.insert(ele);
l1.display();
break;
case 2:cout<<"\n\n\tENTER THE ELEMENT WANT TO SEARCH FOR : ";
cin>>ele;
l1.isinlist(ele);
l1.display();
break;
case 3:cout<<"\n\n\tDELETED ELEMENT IS "<<l1.t_delete();
break;
case 4:l1.reverse();
cout<<"\n\n\tAFTER REVERSING \n";
l1.display();
break;
case 5:cout<<"\n\n\tENTER ELEMENT DO YO WANT TO DELETE";
cin>>ele;
l1.delete_p(ele);
l1.display();
break;
default:exit(0);
}
cout<<"\n\n\tDO U WANT TO CONTINUE?";
cin>>ch;
}while(ch=='Y'||ch=='y');
getch();
}