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-----"<