Given a Queue data structure that supports standard operations such as enqueue() and dequeue(), the task is to implement a Stack data structure using only queues and the permitted queue operations. A stack is a Last-In-First-Out (LIFO) structure supporting push, pop, top (or peek) and size operations. A queue is a First-In-First-Out (FIFO) structure with enqueue and dequeue.

Keep the most recently pushed element at the front of one queue (q1) so that a pop() simply dequeues from q1. Use a second queue (q2) to reorder elements when pushing a new element.
#include <bits/stdc++.h> using namespace std; class Stack { // Two inbuilt queues queue<int> q1, q2; public: void push(int x) { // Push x first in empty q2 q2.push(x); // Push all the remaining elements in q1 to q2. while (!q1.empty()) { q2.push(q1.front()); q1.pop(); } // swap the names of two queues queue<int> q = q1; q1 = q2; q2 = q; } void pop() { // if no elements are there in q1 if (q1.empty()) return; q1.pop(); } int top() { if (q1.empty()) return -1; return q1.front(); } int size() { return q1.size(); } }; // Driver code int main() { Stack s; s.push(1); s.push(2); s.push(3); cout << "current size: " << s.size() << endl; cout << s.top() << endl; s.pop(); cout << s.top() << endl; s.pop(); cout << s.top() << endl; cout << "current size: " << s.size() << endl; return 0; }
current size: 3 3 2 1 current size: 1
Enqueue new elements into q1. To pop, move all elements except the last from q1 into q2, dequeue the last element (which is the top of the stack), then swap the queues so that q1 again contains all remaining elements in correct order.
// Program to implement a stack // using two queue #include <bits/stdc++.h> using namespace std; class Stack { queue<int> q1, q2; public: void pop() { if (q1.empty()) return; // Leave one element in q1 and push others in q2. while (q1.size() != 1) { q2.push(q1.front()); q1.pop(); } // Pop the only left element from q1 q1.pop(); // swap the names of two queues queue<int> q = q1; q1 = q2; q2 = q; } void push(int x) { q1.push(x); } int top() { if (q1.empty()) return -1; while (q1.size() != 1) { q2.push(q1.front()); q1.pop(); } // last pushed element int temp = q1.front(); // to empty the auxiliary queue after last operation q1.pop(); // push last element to q2 q2.push(temp); // swap the two queues names queue<int> q = q1; q1 = q2; q2 = q; return temp; } int size() { return q1.size(); } }; // Driver code int main() { Stack s; s.push(1); s.push(2); s.push(3); cout << "current size: " << s.size() << endl; cout << s.top() << endl; s.pop(); cout << s.top() << endl; s.pop(); cout << s.top() << endl; cout << "current size: " << s.size() << endl; return 0; }
current size: 3 3 2 1 current size: 1
Use one queue and rotate its elements after every push so that the newly pushed element becomes the front of the queue. This makes push O(N) and pop O(1).
#include <bits/stdc++.h> using namespace std; // Stack Class that acts as a queue class Stack { queue<int> q; public: void push(int data); void pop(); int top(); int size(); bool empty(); }; // Push operation void Stack::push(int data) { // Get previous size of queue int s = q.size(); // Push the current element q.push(data); // Pop all the previous elements and put them after current element for (int i = 0; i < s; i++) { // Add the front element again q.push(q.front()); // Delete front element q.pop(); } } // Removes the top element void Stack::pop() { if (q.empty()) cout << "No elements
"; else q.pop(); } // Returns top of stack int Stack::top() { return (q.empty()) ? -1 : q.front(); } // Returns true if Stack is empty else false bool Stack::empty() { return (q.empty()); } int Stack::size() { return q.size(); } int main() { Stack st; st.push(1); st.push(2); st.push(3); cout << "current size: " << st.size() << "
"; cout << st.top() << "
"; st.pop(); cout << st.top() << "
"; st.pop(); cout << st.top() << "
"; cout << "current size: " << st.size(); return 0; }
current size: 3 3 2 1 current size: 1
It is possible to implement a stack using one or more queues by reordering elements so that the most recently inserted element is accessible at the queue's front. Three standard approaches are: two-queue with push costly, two-queue with pop costly, and a single-queue rotation approach. Choose the approach based on which operation (push or pop) must be optimised in your use case. The provided C++ implementations and complexity analyses give ready patterns for implementation and exam-style understanding.
![]() | Explore Courses for Computer Science Engineering (CSE) exam |
![]() | Get EduRev Notes directly in your Google search |