diff --git a/algorithms/data-structures/stack/stack_using_two_queues.cpp b/algorithms/data-structures/stack/stack_using_two_queues.cpp new file mode 100644 index 00000000..30443571 --- /dev/null +++ b/algorithms/data-structures/stack/stack_using_two_queues.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +class QueueStack{ +private: + queue q1; + queue 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<pop()<<" "; + + } + } + cout< q1; + queue 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