From 33c029542385d9af215154b8ceb3b3591d9977bc Mon Sep 17 00:00:00 2001 From: apar1223 <87368810+apar1223@users.noreply.github.com> Date: Wed, 26 Oct 2022 20:10:29 +0530 Subject: [PATCH] implemented stack using queue --- src-cpp/stack_using_queue.cpp | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src-cpp/stack_using_queue.cpp diff --git a/src-cpp/stack_using_queue.cpp b/src-cpp/stack_using_queue.cpp new file mode 100644 index 0000000..ce906f6 --- /dev/null +++ b/src-cpp/stack_using_queue.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; +class Stack +{ +public: + queue q; + void push(int x) + { + q.push(x); + for (auto i = 0; i < q.size() - 1; i++) + { + q.push(q.front()); + q.pop(); + } + } + + int pop() + { + int x = q.front(); + q.pop(); + return x; + } + + int top() + { + return q.front(); + } + + bool empty() + { + return q.empty(); + } +}; +int main() +{ + Stack 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.top() << endl; + return 0; +} \ No newline at end of file