forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_using_two_queues.cpp
More file actions
66 lines (60 loc) · 1018 Bytes
/
stack_using_two_queues.cpp
File metadata and controls
66 lines (60 loc) · 1018 Bytes
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
#include<bits/stdc++.h>
using namespace std;
class QueueStack{
private:
queue<int> q1;
queue<int> q2;
public:
void push(int);
int pop();
};
int main()
{
int T;
cin>>T;
while(T--)
{
QueueStack *qs = new QueueStack();
int Q;
cin>>Q;
while(Q--){
int QueryType=0;
cin>>QueryType;
if(QueryType==1)
{
int a;
cin>>a;
qs->push(a);
}else if(QueryType==2){
cout<<qs->pop()<<" ";
}
}
cout<<endl;
}
}
class QueueStack{
private:
queue<int> q1;
queue<int> q2;
public:
void push(int);
int pop();
};
void QueueStack :: push(int x)
{
q2.push(x);
while(!q1.empty()){
q2.push(q1.front());
q1.pop();
}
swap(q1,q2);
}
int QueueStack :: pop()
{
if(q1.empty()) return -1;
else{
int x = q1.front();
q1.pop();
return x;
}
}