-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue using Two Stacks.cpp
55 lines (46 loc) · 1.22 KB
/
Queue using Two Stacks.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
/*
Problem Title: Queue using Two Stacks
Problem URL: https://www.hackerrank.com/challenges/one-month-preparation-kit-queue-using-two-stacks/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=preparation-kits&playlist_slugs%5B%5D%5B%5D=one-month-preparation-kit&playlist_slugs%5B%5D%5B%5D=one-month-week-three
Max Score: 100
Score: 100
Language: C++
Category: One Month Preparation
*/
#include <iostream>
#include <stack>
using namespace std;
int main() {
int q, type, x; cin >> q;
stack<int> val1, val2;
stack<char> op1, op2;
while (q--) {
cin >> type;
switch (type) {
case 1:
cin >> x;
val1.push(x);
break;
case 2:
op1.push('D'); // Dequeue
break;
default:
op1.push('P'); // print
}
}
while(!val1.empty()) {
val2.push(val1.top());
val1.pop();
}
while(!op1.empty()) {
op2.push(op1.top());
op1.pop();
}
while(!op2.empty()) {
if (op2.top() == 'D') // Dequeue
val2.pop();
else // Print
cout << val2.top() << '\n';
op2.pop();
}
return 0;
}