File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed
algorithms/data-structures/stack Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class QueueStack {
5
+ private:
6
+ queue<int > q1;
7
+ queue<int > q2;
8
+ public:
9
+ void push (int );
10
+ int pop ();
11
+ };
12
+
13
+
14
+ int main ()
15
+ {
16
+ int T;
17
+ cin>>T;
18
+ while (T--)
19
+ {
20
+ QueueStack *qs = new QueueStack ();
21
+
22
+ int Q;
23
+ cin>>Q;
24
+ while (Q--){
25
+ int QueryType=0 ;
26
+ cin>>QueryType;
27
+ if (QueryType==1 )
28
+ {
29
+ int a;
30
+ cin>>a;
31
+ qs->push (a);
32
+ }else if (QueryType==2 ){
33
+ cout<<qs->pop ()<<" " ;
34
+
35
+ }
36
+ }
37
+ cout<<endl;
38
+ }
39
+ }
40
+
41
+ class QueueStack {
42
+ private:
43
+ queue<int > q1;
44
+ queue<int > q2;
45
+ public:
46
+ void push (int );
47
+ int pop ();
48
+ };
49
+ void QueueStack :: push(int x)
50
+ {
51
+ q2.push (x);
52
+ while (!q1.empty ()){
53
+ q2.push (q1.front ());
54
+ q1.pop ();
55
+ }
56
+ swap (q1,q2);
57
+ }
58
+ int QueueStack :: pop()
59
+ {
60
+ if (q1.empty ()) return -1 ;
61
+ else {
62
+ int x = q1.front ();
63
+ q1.pop ();
64
+ return x;
65
+ }
66
+ }
You can’t perform that action at this time.
0 commit comments