VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implement-stack-using-queue/

⇱ Implement Stack using Queues - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implement Stack using Queues

Last Updated : 18 Sep, 2025

Implement a stack using queues. We are allowed to use only queue data structure.

👁 Dequeue-Operation-in-Queue-2

The stack should support the following operations:

  • push(x): Insert element x onto the top of the stack.
  • pop(): Remove the element from the top of the stack. If the stack is empty, do nothing.
  • top(): Return the element at the top of the stack. If the stack is empty, return -1.
  • size(): Return the number of elements in the stack.

[Approach 1] Using Two Queue - Push in O(n) and Pop() in O(1)

We will be using two queues (q1 and q2) to implement the stack operations.

The main idea is to always keep the newly inserted element at the front of q1, so that both pop() and top() can directly access it. Queue q2 acts as a helper to rearrange elements during push().

push(x):

  • Enqueue the new element x into q2.
  • Move all elements from q1 into q2, one by one.
  • Swap the names of q1 and q2. After the swap, q1 contains the updated stack order with the newest element at the front.

pop():

  • If q1 is empty, the stack is empty (underflow condition).
  • Otherwise, dequeue from the front of q1, which represents the top of the stack.

top():

  • If q1 is empty, return -1.
  • Otherwise, return the front element of q1, since it represents the current top of the stack.

size():

  • Simply return the number of elements in q1, which tracks the current stack size.

Output
3
2
1
1

Time Complexity:

  • push operation: O(n), as every new element must be inserted first into q2, then all elements from q1 are moved to q2.
  • pop operation: O(1), since the top element is always at the front of q1.
  • top operation: O(1), because the top can be accessed directly from the front of q1.
  • size operation: O(1), as size is tracked by the queue.

Auxiliary Space: O(n)

[Approach 2] Using Two Queue - Push in O(1) and Pop() in O(n)

We are implementing a stack using two queues (q1 and q2).

The idea is to make the push(x) operation simple, and adjust the order during pop() and top() so that the stack behavior (Last-In-First-Out) is preserved.

push(x):

  • The element x is simply enqueued into q1. This keeps push() efficient.

pop():

  • If q1 is empty, the stack is empty (underflow condition).
  • Otherwise, move elements from q1 to q2 until only one element is left in q1.
  • Remove this last element from q1 (which represents the top of the stack).
  • Swap the names of q1 and q2 so that q1 again holds the current stack elements.

top():

  • If q1 is empty, return -1.
  • Otherwise, move elements from q1 to q2 until only one element is left.
  • Store this last element (the top of the stack), then move it to q2 instead of discarding it.
  • Swap q1 and q2 to restore order.
  • Return the stored element.

size():

  • Return the number of elements currently in q1, which represents the size of the stack.

Output
3
2
1
1

Time Complexity:

  • push operation: O(1), as the element is directly added to q1.
  • pop operation: O(n), since all elements except the last need to be moved to q2 before removal.
  • top operation: O(n), as we need to move elements to access the last inserted one.
  • size operation: O(1), because queue maintains its size.

Auxiliary Space: O(n)

[Approach 3] Using Single Queue - Push in O(n) and Pop() in O(1)

In this approach, we implement a stack using only one queue.

The main idea is to always keep the most recently pushed element at the front of the queue, so that top() and pop() work in O(1) time.

push(x):

  • Insert the new element at the back of the queue.
  • Rotate the queue by repeatedly removing the front element and pushing it back until the new element comes to the front.
  • After rotation, the newest element will be at the front, acting as the stack top.

size():

  • Return the size of the queue.

top():

  • Return the element at the front of the queue, since it represents the top of the stack.

pop():

  • Simply dequeue the front element of the queue.

Output
3
2
1
1

Time Complexity:

  • push operation: O(n), since after inserting the element, all previous elements must be rotated behind it.
  • pop operation: O(1), as the top element is always at the front of the queue.
  • top operation: O(1), because the most recent element is always at the front.
  • size operation: O(1), queue keeps track of size.

Auxiliary Space: O(n)

Comment