-
Notifications
You must be signed in to change notification settings - Fork 1
/
314.cpp
80 lines (69 loc) · 1.31 KB
/
314.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// { Driver Code Starts
#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;
}
}
// } Driver Code Ends
/* The structure of the class is
class QueueStack{
private:
queue<int> q1;
queue<int> q2;
public:
void push(int);
int pop();
};
*/
//Function to push an element into stack using two queues.
void QueueStack :: push(int x)
{
while(!q1.empty()){
int k=q1.front();
q1.pop();
q2.push(k);
}
q1.push(x);
while(!q2.empty()){
int k=q2.front();
q2.pop();
q1.push(k);
}
}
//Function to pop an element from stack using two queues.
int QueueStack :: pop()
{
if(q1.empty()) return -1;
int x=q1.front();
q1.pop();
return x;
}