forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ascending_priority_queue.cpp
223 lines (173 loc) · 3.91 KB
/
Ascending_priority_queue.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
/*
AIM :: TO IMPLEMENT ASCENDING PRIORITY QUEUE
WHAT IS ASCENDING PRIORITY QUEUE ?
In ascending order priority queue, a lower priority number is given as a higher priority in a priority.
For example, we take the numbers from 1 to 5 arranged in an ascending order like 1,2,3,4,5; therefore,
the smallest number, i.e., 1 is given as the highest priority in a priority queue.
*/
#include <iostream>
using namespace std;
//Prototypes
void enq(int *, int &, int &, int); // for insertion
void deq(int *, int &, int &, int); // for deletion
void display(int *, int, int); // for display
int q_full(int, int); // for checking queue is full or not
int q_empty(int, int); // for checking queue is empty or not
int main()
{
int size = 100, choice;
int Q[size];
//initially front and rear will be at -1
int front = -1; // deletion will be done from front end
int rear = -1; // insertion will be done from rear end
cout << "\t\t\nASCENDING PRIORITY QUEUE" << endl;
while (true)
{
cout << "\n1. ENQ\n2. DEQ\n3. DISPLAY\n4. EXIT" << endl;
cout << "Enter your choice :: ";
cin >> choice;
switch (choice)
{
case 1:
enq(Q, front, rear, size);
break;
case 2:
deq(Q, front, rear, size);
break;
case 3:
display(Q, front, rear);
break;
case 4:
cout << "\nexiting..." << endl;
exit(0);
break;
default:
cout << "\nInvalid choice :(" << endl;
break;
}
}
return 0;
}
void enq(int Q[], int &front, int &rear, int size)
{
int data, temp;
if (q_full(front, size))
cout << "QUEUE OVERFLOW" << endl;
else
{
cout << "Enter the data :: ";
cin >> data;
if (front == -1) // means inserting for first time
front++;
//find the appropriate place for the data given by the user
for (temp = rear; temp >= 0 && data < Q[temp]; temp--)
Q[temp + 1] = Q[temp];
Q[temp + 1] = data; // insert data at it's right place
rear++;
display(Q, front, rear);
}
}
void deq(int Q[], int &front, int &rear, int size)
{
if (q_empty(front, rear))
cout << "\nQUEUE UNDERFLOW";
else
{
//storing data which is to be deleted
int data = Q[front];
//if front and rear is equal means there was only one element left
if (front == rear)
front = rear = -1; // reset front and rear
else
front++;
display(Q, front, rear);
}
}
void display(int Q[], int front, int rear)
{
if (q_empty(front, rear))
cout << "\nQUEUE IS EMPTY :(" << endl;
else
{
cout << "\nQUEUE :: ";
for (int i = front; i <= rear; i++)
cout << Q[i] << " ";
cout << '\n';
}
}
int q_full(int rear, int size)
{
if (rear >= size)
return 1;
return 0;
}
int q_empty(int front, int rear)
{
if (front == -1 || front > rear)
return 1;
return 0;
}
/*
TEST CASE
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 1
Enter the data :: 10
QUEUE :: 10
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 1
Enter the data :: 5
QUEUE :: 5 10
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 1
Enter the data :: 15
QUEUE :: 5 10 15
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 1
Enter the data :: 1
QUEUE :: 1 5 10 15
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 2
QUEUE :: 5 10 15
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 2
QUEUE :: 10 15
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 2
QUEUE :: 15
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 2
QUEUE IS EMPTY :(
1. ENQ
2. DEQ
3. DISPLAY
4. EXIT
Enter your choice :: 4
exiting...
TIME COMPLEXITY
INSERTION O(n)
DELETION O(1)
*/