-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementing queue using cll.cpp
More file actions
213 lines (198 loc) · 3.5 KB
/
Copy pathimplementing queue using cll.cpp
File metadata and controls
213 lines (198 loc) · 3.5 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
#include<iostream>
using namespace std;
template<class t>
class node
{
public:
t info;
node *next;
node(t x, node *n =NULL)
{
info =x;
next = n;
}
};
template<class t>
class queueimplementbycll
{
node<t>*cursor;
public:
queueimplementbycll()
{
cursor = NULL;
}
void input();
int isempty();
void insert(t x);
t deletion();
void count();
void display();
void clear();
};
template<class t>
void queueimplementbycll<t>::input()
{
int n;
t x;
cout<<"\n Enter number of nodes: ";
cin>>n;
for(int i = 0; i < n; i++)
{
cout<<"\n Enter info part of node: ";
cin>>x;
insert(x);
display();
}
}
template<class t>
int queueimplementbycll<t>::isempty()
{
if(cursor==NULL)
return 1;
else
return 0;
}
template<class t>
void queueimplementbycll<t>::insert(t x)
{
node<t> *temp = new node<t>(x);
if(isempty())
{
cursor = temp;
cursor->next = cursor;
}
else
{
temp->next = cursor->next;
cursor->next = temp;
cursor = temp;
}
}
template<class t>
t queueimplementbycll<t>::deletion()
{
node<t> *temp;
t x;
x = cursor->next->info;
if(cursor->next == cursor)
{
delete cursor;
cursor = NULL;
}
else
{
temp = cursor->next;
cursor->next = temp->next;
delete temp;
}
return x;
}
template<class t>
void queueimplementbycll<t>::count()
{
int count=0;
if(!(isempty()))
{
node<t>*temp;
temp = cursor->next;
cout<<"LIST IS: ";
do
{
cout<<temp->info<<"\t";
temp = temp->next;
count=count+1;
}
while(temp != cursor->next);
cout<<"\n"<<count;
}
}
template<class t>
void queueimplementbycll<t>::display()
{
if(!(isempty()))
{
node<t>*temp;
temp = cursor->next;
cout<<"\nLIST IS: ";
do
{
cout<<temp->info<<"\t";
temp = temp->next;
}
while(temp != cursor->next);
}
}
template<class t>
void queueimplementbycll<t>::clear()
{
t info;
node<t> *next;
info=0;
next=NULL;
}
int main()
{
queueimplementbycll<int> l1;
int choice;
int ele1;
node<int>*temp=NULL;
char ch = 'y';
do
{
cout<<"************MAIN MENU***********"<<endl;
cout<<"\n1. To Insert an element! ";
cout<<"\n2. To Delete an element! ";
cout<<"\n3. To Count number of elements! ";
cout<<"\n4. To Display! ";
cout<<"\n5. To clear! ";
cout<<"\n Enter choice ";
cin>>choice;
switch(choice)
{
case 1:
int b;
cout<<"\n Enter element to be inserted!!: ";
cin>>b;
l1.insert(b);
l1.display();
break;
case 2:
if(l1.isempty())
cout<<"\n List is empty, cannot delete!! ";
else
{
ele1=l1.deletion();
cout<<"\n Deleted element is :"<<ele1<<endl;
l1.display();
}
break;
case 3:
if(l1.isempty())
cout<<"\n List is empty !!";
else
{
cout<<"\n The number of elements in the list are:";
l1.count();
}
break;
case 4:
if(l1.isempty())
cout<<"\n List is empty, nothing to display!! ";
else
{
cout<<"The elements are: ";
l1.display();
}
break;
case 5:
l1.clear();
cout<<"The queue has been cleared!!";
break;
default:
cout<<"\nWRONG CHOICE.";
}
cout<<"\nDO YOU WANT TO CONTINUE?(Y/N): ";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}