Skip to content

Commit d1aa70f

Browse files
authored
Merge pull request #300 from GuptaTanisha/master
New Algorithm Create stack_using_two_queues.cpp
2 parents 17b6206 + 9183609 commit d1aa70f

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy