diff --git a/queue_using_stack.cpp b/queue_using_stack.cpp new file mode 100644 index 0000000..76244c0 --- /dev/null +++ b/queue_using_stack.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; +class MyQueue +{ +public: + MyQueue() + { + } + stack s1; + stack s2; + void push(int x) + { + while (!s1.empty()) + { + s2.push(s1.top()); + s1.pop(); + } + s1.push(x); + while (!s2.empty()) + { + s1.push(s2.top()); + s2.pop(); + } + } + + int pop() + { + int x = s1.top(); + s1.pop(); + return x; + } + + int peek() + { + return s1.top(); + } + + bool empty() + { + return s1.empty(); + } +}; +int main() +{ + MyQueue st; + st.push(5); + st.push(15); + st.push(45); + st.push(55); + cout << st.pop() << " Popped from stack" << endl; + cout << "top element of stack " << st.peek() << endl; + + return 0; +} \ No newline at end of file