forked from THUNDERANKUSH/HACKERRANK-CODES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_using_Two_Stacks.cpp
More file actions
50 lines (46 loc) · 1.12 KB
/
Queue_using_Two_Stacks.cpp
File metadata and controls
50 lines (46 loc) · 1.12 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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
stack<int> s1, s2;
int num_operations;
cin>>num_operations;
int Q_operation, x;
for(int i=0; i<num_operations; i++){
cin>>Q_operation;
if(Q_operation == 1){
cin>>x;
s1.push(x);
}
if(Q_operation == 2){
if(!s2.empty()){
s2.pop();
}
else{
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
s2.pop();
}
}
if(Q_operation == 3){
if(!s2.empty()){
cout<<s2.top()<<endl;
}
else{
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
cout<<s2.top()<<endl;
}
}
}
return 0;
}