Skip to content

New Algorithm Create stack_using_two_queues.cpp #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions algorithms/data-structures/stack/stack_using_two_queues.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
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