Implement Queue using Stacks

Updated on 02 June, 2025
Implement Queue using Stacks header image

Problem Statement

You need to construct a First In First Out (FIFO) queue utilizing only two stacks. This custom queue class, named MyQueue, implements all traditional queue operations such as enqueueing (pushing) elements to the queue, previewing the front element (peek), dequeueing (popping) the front element, and checking if the queue is empty. To meet the challenge:

  • push(int x) adds an element x at the end of the queue.
  • pop() removes and returns the element at the front of the queue.
  • peek() returns the front element without removing it.
  • empty() checks if the queue is empty and returns a boolean accordingly.

The implementation must use only basic stack operations: push to top, pop from top, peek top, size, and empty check. If the language lacks native stacks, simulate them using lists or deques while adhering strictly to the allowed operations.

Examples

Example 1

Input:

["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]

Output:

[null, null, null, 1, 1, false]

Explanation:

MyQueue myQueue = new MyQueue();
myQueue.push(1);      // queue is: [1]
myQueue.push(2);      // queue is: [1, 2]
myQueue.peek();       // returns 1
myQueue.pop();        // returns 1, queue becomes [2]
myQueue.empty();      // returns false

Constraints

  • 1 <= x <= 9
  • At most 100 calls will be made to push, pop, peek, and empty
  • All calls to pop and peek are valid (the queue will not be empty)

Approach and Intuition

A queue operates on a FIFO (First In First Out) principle, whereas a stack works in a LIFO (Last In First Out) manner. To implement a queue using only stacks, we use two stacks:

  1. stack1 for enqueueing elements (push operation).
  2. stack2 for dequeueing or peeking the front element.

Operations:

  • push(x) – Always push onto stack1.
  • pop() – If stack2 is empty, transfer all elements from stack1 to stack2 (reversing their order), then pop from stack2. If stack2 is not empty, just pop.
  • peek() – Same logic as pop() but return the top of stack2 without removing it.
  • empty() – Return true if both stack1 and stack2 are empty.

This dual-stack approach ensures that enqueue is efficient and dequeue honors the queue’s FIFO behavior by reversing order when needed. It's a classic exercise in structural transformation using simple building blocks.

Solutions

  • Java
java
public int getFront() {
    if (!s2.isEmpty()) {
        return s2.peek();
    }
    return front;
}

The solution involves implementing a queue using two stacks in Java, specifically focusing on the getFront() method. This method efficiently retrieves the front element of the queue.

  • Ensure that two stacks, s1 and s2, are utilized, where s1 is for enqueue operations and s2 is for dequeue operations.
  • In the getFront() method:
    • First, check if s2 is not empty. If s2 has elements, the next element to be dequeued (the front of the queue) is the top element of s2. Use s2.peek() to retrieve it.
    • If s2 is empty, retrieve the front element from the previously saved front variable. This condition is useful when all elements are transferred from s1 to s2 and s2 is subsequently emptied.

This method ensures constant time complexity for retrieving the front element of the queue, providing an efficient way to mimic queue operations using stack-based mechanisms.

Comments

No comments yet.