Implement a stack using single queue
Last Updated : 23 Jul, 2025
We are given a queue data structure, the task is to implement a stack using a single queue.
Also Read: Stack using two queues
The idea is to keep the newly inserted element always at the front of the queue, preserving the order of previous elements by appending the new element at the back and rotating the queue by size n so that the new item is at the front.
// Push an element x in the stack s
push(s, x)
1) Let size of q be s.
1) Enqueue x to q
2) One by one Dequeue s items from queue and enqueue them.
// pop an item from stack s
pop(s)
1) Dequeue an item from q
C++ #include<bits/stdc++.h> using namespace std; // User defined stack that uses a queue class Stack { queue<int>q; public: void push(int val); void pop(); int top(); bool empty(); }; // Push operation void Stack::push(int val) { // Get previous size of queue int s = q.size(); // Push current element q.push(val); // Pop (or Dequeue) all previous // elements and put them after current // element for (int i=0; i<s; i++) { // this will add front element into // rear of queue q.push(q.front()); // this will delete front element q.pop(); } } // Removes the top element void Stack::pop() { if (q.empty()) cout << "No elements\n"; 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()); } // Driver code int main() { Stack s; s.push(10); s.push(20); cout << s.top() << endl; s.pop(); s.push(30); s.pop(); cout << s.top() << endl; return 0; }
Java import java.util.LinkedList; import java.util.Queue; public class stack { Queue<Integer> q = new LinkedList<Integer>(); // Push operation void push(int val) { // get previous size of queue int size = q.size(); // Add current element q.add(val); // Pop (or Dequeue) all previous // elements and put them after current // element for (int i = 0; i < size; i++) { // this will add front element into // rear of queue int x = q.remove(); q.add(x); } } // Removes the top element int pop() { if (q.isEmpty()) { System.out.println("No elements"); return -1; } int x = q.remove(); return x; } // Returns top of stack int top() { if (q.isEmpty()) return -1; return q.peek(); } // Returns true if Stack is empty else false boolean isEmpty() { return q.isEmpty(); } // Driver program to test above methods public static void main(String[] args) { stack s = new stack(); s.push(10); s.push(20); System.out.println(s.top()); s.pop(); s.push(30); s.pop(); System.out.println(s.top()); } }
Python q = [] # append operation def append(val): # get previous size of queue size = len(q) # Add current element q.append(val); # Pop (or Dequeue) all previous # elements and put them after current # element for i in range(size): # this will add front element into # rear of queue x = q.pop(0); q.append(x); # Removes the top element def pop(): if (len(q) == 0): print("No elements"); return -1; x = q.pop(0); return x; # Returns top of stack def top(): if(len(q) == 0): return -1; return q[-1] # Returns true if Stack is empty else false def isEmpty(): return len(q)==0; # Driver program to test above methods if __name__=='__main__': s = [] s.append(10); s.append(20); print(str(s[-1])); s.pop(); s.append(30); s.pop(); print(str(s[-1]));
C# using System; using System.Collections.Generic; public class stack { Queue<int> q = new Queue<int>(); // Push operation void push(int val) { // get previous size of queue int size = q.Count; // Add current element q.Enqueue(val); // Pop (or Dequeue) all previous // elements and put them after current // element for (int i = 0; i < size; i++) { // this will add front element into // rear of queue int x = q.Dequeue(); q.Enqueue(x); } } // Removes the top element int pop() { if (q.Count == 0) { Console.WriteLine("No elements"); return -1; } int x = q.Dequeue(); return x; } // Returns top of stack int top() { if (q.Count == 0) return -1; return q.Peek(); } // Returns true if Stack is empty else false bool isEmpty() { if(q.Count == 0) return true; return false; } // Driver program to test above methods public static void Main(String[] args) { stack s = new stack(); s.push(10); s.push(20); Console.WriteLine(s.top()); s.pop(); s.push(30); s.pop(); Console.WriteLine(s.top()); } }
JavaScript let q = []; // Push operation function Push(val) { // get previous size of queue let Size = q.length; // Add current element q.push(val); // Pop (or Dequeue) all previous // elements and put them after current // element for (let i = 0; i < Size; i++) { // this will add front element into // rear of queue let x = q[0]; q.shift(); q.push(x); } } // Removes the top element function Pop() { if (isEmpty()) { console.log("No elements" + "</br>"); return -1; } let x = q[0]; q.shift(); return x; } // Returns top of stack function Top() { if (isEmpty()) return -1; return q[0]; } // Returns true if Stack is empty else false function isEmpty() { if(q.length == 0) return true; return false; } Push(10); Push(20); console.log(Top()); Pop(); Push(30); Pop(); console.log(Top());
Time complexity
For Push Operations: O(n) as we need to rotate the queue to bring the newly added element to the front. ( where n is the size of the queue )
For Pop Operations: O(1)
Auxiliary Space: O(n)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem
My Profile