forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_using_circular_list.cpp
231 lines (188 loc) · 3.95 KB
/
queue_using_circular_list.cpp
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
/*
Queue is a data structure in which insertion is done at one end and deletion is done at the other end as
it follows the principle of First In First Out(FIFO). We are implementing queue using circular singly-linked list,
so we will craete class and then perform various function like: enqueue, dequeue, peek, display, isEmpty, isFull on it.
*/
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
struct Node *link;
};
Node *rear;
//This function will check if the Queue is empty or not.
int isEmpty()
{
if(rear == NULL)
return 1;
else
return 0;
}
//This function will insert elements into the Queue.
void insertQueue(int item)
{
Node *temp = new Node;
temp->data = item;
if(temp == NULL)
{
cout<<"\nMemory problem."<<endl;
return;
}
if(isEmpty()) //If queue is empty.
{
rear = temp;
temp->link = rear;
}
else
{
temp->link = rear->link;
rear->link = temp;
rear = temp;
}
}
//This function will delete the front element of Queue.
int deleteElement()
{
int item;
Node *temp;
if(isEmpty())
{
cout<<"\nQueue underflow"<<endl;
return -1;
}
if(rear->link == rear) //If only one element is present
{
temp = rear;
rear = NULL;
}
else
{
temp = rear->link;
rear->link = rear->link->link;
}
item = temp->data;
delete temp;
return item;
}
//This function will return the front element
int peek()
{
if(isEmpty())
{
cout<<"\nQueue underflow."<<endl;
return -1;
}
return rear->link->data;
}
//This function will display elements of Queue
void display()
{
Node *temp;
if(isEmpty())
{
cout<<"\nQueue is empty"<<endl;
return;
}
cout<<"\nQueue formed : ";
temp = rear->link;
do
{
cout<<temp->data<<" ";
temp = temp->link;
}while(temp != rear->link);
cout<<endl;
}
int main()
{
int choice,item;
while(1)
{
cout<<endl;
cout<<"1. INSERT"<<endl;
cout<<"2. DELETE"<<endl;
cout<<"3. PEEK"<<endl;
cout<<"4. DISPLAY"<<endl;
cout<<"5. EXIT"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter the element for insertion : ";
cin>>item;
insertQueue(item);
break;
case 2:
if(deleteElement() == -1)
cout<<endl;
else
cout<<"\nDeleted element : "<<deleteElement()<<endl;
break;
case 3:
if(peek() == -1)
cout<<"\nNot found"<<endl;
else
cout<<"\nItem at the front of queue is : "<<peek()<<endl;
break;
case 4:
display();
break;
case 5:
exit(0);
default:
cout<<"IVALID CHOICE!";
}
}
}
/*
Time Complexity:
Insertion - O(1)
Deletion - O(n)
Peek - O(1)
isEmpty - O(1)
Space Complexity: O(n), as we are using linked list of n-nodes.
Sample Input/Output:
1. INSERT
2. DELETE
3. PEEK
4. DISPLAY
5. EXIT
Enter your choice : 1
Enter the element for insertion : 10
1. INSERT
2. DELETE
3. PEEK
4. DISPLAY
5. EXIT
Enter your choice : 1
Enter the element for insertion : 20
1. INSERT
2. DELETE
3. PEEK
4. DISPLAY
5. EXIT
Enter your choice : 1
Enter the element for insertion : 30
1. INSERT
2. DELETE
3. PEEK
4. DISPLAY
5. EXIT
Enter your choice : 3
Item at the front of queue is : 10
1. INSERT
2. DELETE
3. PEEK
4. DISPLAY
5. EXIT
Enter your choice : 4
Queue formed : 10 20 30
1. INSERT
2. DELETE
3. PEEK
4. DISPLAY
5. EXIT
Enter your choice : 5
*/