-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueWithTwoStacks.cpp
More file actions
51 lines (49 loc) · 1.07 KB
/
QueueWithTwoStacks.cpp
File metadata and controls
51 lines (49 loc) · 1.07 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
51
#include<bits/stdc++.h>
using namespace std;
class Q{
private:
stack<int> st1,st2;
public:
void push(int x) {
st1.push(x);
}
void pop() {
if(!st2.empty()){
st2.pop();
return ;
}
while(!st1.empty()){
st2.push(st1.top());
st1.pop();
}
st2.pop();
}
int front() {
if(!st2.empty()) return st2.top();
int x;
while(!st1.empty()){
st2.push(st1.top());
st1.pop();
}
if(!st2.empty())
x=st2.top();
return x;
}
};
int main() {
Q q;
int t,type,x;
cin>>t; //total number of operations (pop+front+push)
while(t--){
cin>>type;
if(type==1) {
cin>>x;
q.push(x);
}
else if(type==2) {
q.pop();
}
else cout<<q.front()<<endl;
}
return 0;
}