From 31a11c35f4cf5ea201ca55706ddbaf088d83986f Mon Sep 17 00:00:00 2001 From: sanskruti_shahu Date: Tue, 5 Oct 2021 12:16:13 +0530 Subject: [PATCH] Added code for Stack implementation using Queue --- Stack/stack_implementation_using_queue.cpp | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Stack/stack_implementation_using_queue.cpp diff --git a/Stack/stack_implementation_using_queue.cpp b/Stack/stack_implementation_using_queue.cpp new file mode 100644 index 0000000..998e9ea --- /dev/null +++ b/Stack/stack_implementation_using_queue.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; +class Stack +{ + int Size; + queue q1; + queue q2; + +public: + Stack() + { + Size = 0; + } + void push(int value) + { + q2.push(value); + Size++; + while (!q1.empty()) + { + q2.push(q1.front()); + q1.pop(); + } + queue temp = q1; + q1 = q2; + q2 = temp; + } + void pop() + { + q1.pop(); + Size--; + } + bool empty() + { + if (q1.empty()) + { + return true; + } + return false; + } + int top() + { + if (!empty()) + { + return q1.front(); + } + return -1; + } + int size() + { + return Size; + } +}; + +int main() +{ + Stack st; + st.push(4); + st.push(3); + st.push(2); + st.push(1); + cout<<"Top Element : " << st.top() << endl; + cout <<"Size : "<< st.size() << endl; + cout<<"-----After 1st Pop-----"<