JavaScript program to implement queue using stack

A queue is a First In First Out (FIFO) data structure, in which the first element added to the queue is the first one to be removed. The different operations associated with Queue include Enqueue, Dequeue etc. A stack is a Last In, First Out (LIFO) data structure, in which the last element added to the stack is the first one to be removed. The different operations associated with stack include push, pop, peek etc.

Approach

We need two Stack to implement the queue because the Queue is First In First Out (FIFO) data structure and the Stack is Last In, First Out (FIFO) i.e. the first element added to the queue is the first one to be removed and the last element added to the stack is the first one to be removed.

  • Define a class named QueueUsingStack.
  • Inside the constructor, initialize two empty arrays stack1 and stack2. These arrays will serve as the two stacks required for implementing the queue.
  • Implement the enqueue(item) method to add elements to the queue. Simply push the item onto stack1.
  • Implement the dequeue() method to remove and return the oldest element from the queue. If stack2 is empty, pop all elements from stack1 and push them onto stack2. Then, pop and return the top element from stack2.
  • Implement the peek() method to return the oldest element from the queue without removing it. Similar to dequeue, if stack2 is empty, first move all elements from stack1 to stack2. Then, return the top element of stack2.
  • Implement the isEmpty() method to check if the queue is empty. The queue is empty when both stack1 and stack2 are empty.

Example: To demonstrate implementation of queue using stack.

JavaScript
class QueueUsingStack {
    constructor() {
        this.stack1 = [];
        this.stack2 = [];
    }

    // Enqueue operation
    enqueue(item) {
        // Push the item into stack1
        this.stack1.push(item);
    }

    // Dequeue operation
    dequeue() {
        // If stack2 is empty, pop all elements
        // from stack1 and push into stack2
        if (this.stack2.length === 0) {
            while (this.stack1.length > 0) {
                this.stack2.push(this.stack1.pop());
            }
        }
        // Pop the top element from stack2 
        // (which is the oldest element in the queue)
        return this.stack2.pop();
    }

    // Peek operation
    peek() {
        // If stack2 is empty, pop all elements
        // from stack1 and push into stack2
        if (this.stack2.length === 0) {
            while (this.stack1.length > 0) {
                this.stack2.push(this.stack1.pop());
            }
        }
        // Peek at the top element 
        // of stack2 without removing it
        return this.stack2[this.stack2.length - 1];
    }

    // Check if the queue is empty
    isEmpty() {
        // Both stack1 and stack2 should be 
        // empty for the queue to be empty
        return this.stack1.length === 0 && 
               this.stack2.length === 0;
    }
}

const queue = new QueueUsingStack();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

console.log(queue.dequeue()); 
console.log(queue.peek());    
console.log(queue.isEmpty());

Output
1
2
false

Time complexity: O(1)

Space complexity: O(n)



Contact Us