-
Notifications
You must be signed in to change notification settings - Fork 5
/
queue_using_stack.cpp
59 lines (52 loc) · 1.13 KB
/
queue_using_stack.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
/*
it takes two stacks to perform the task,
but it's time efficient, only in Linear time
There are also another approach, which is
not time efficient, but use only one stack
*/
#include <iostream>
#include <stack>
using namespace std;
/*
| 3 | | 1 |
| 2 | | 2 |
| 1 | | 3 |
----- -----
enq deq
*/
stack<int> enq, deq;
void enqueue() {
int val;
cin >> val;
enq.push(val);
}
void dequeue() {
if (deq.empty()) { // if deq empty, transfer all enq value to deq in reverse order
int val;
while (!enq.empty()) {
int val = enq.top();
enq.pop();
deq.push(val);
}
}
if (deq.empty()) { // if still empty
printf("Error: queue is empty\n");
exit(EXIT_FAILURE);
}
int val = deq.top();
deq.pop();
printf("dequeued %d\n", val);
}
int main() {
string cmd;
for (int i = 0; i < 10; i++) {
cin >> cmd;
if (tolower(cmd[0]) == 'e') { // cmd == "enqueue"
enqueue();
}
else if (tolower(cmd[0]) == 'd') { // cmd == 'dequeue'
dequeue();
}
}
return 0;
}