How to implement enqueue and dequeue using only two stacks in JavaScript?


In this tutorial, we'll be implementing a queue using only two stacks. This is a pretty common interview question so it's good to know how to do it.

Algorithm

  • STEP 1 − We have two stacks, one for enqueueing and one for dequeueing.

  • STEP 2 − We enqueue by pushing onto the enqueue stack.

  • STEP 3 − We dequeue by popping from the dequeue stack.

  • STEP 4 − If the dequeue stack is empty, we pop everything from the enqueue stack and push it onto the dequeue stack.

  • STEP 5 − This reverses the order so that the oldest item is now at the top of the dequeue stack.

Implementation

Now let's see how this would work in code. We'll create a Queue class with two methods, enqueue and dequeue.

class Queue { constructor() { this.enqueueStack = []; this.dequeueStack = []; } enqueue(item) { this.enqueueStack.push(item); } dequeue() { if (this.dequeueStack.length === 0) { // move everything from enqueueStack to dequeueStack while (this.enqueueStack.length > 0) { const item = this.enqueueStack.pop(); this.dequeueStack.push(item); } } // dequeue from dequeueStack return this.dequeueStack.pop(); } }

In the above code, we have a Queue class with two methods, enqueue and dequeue.

We enqueue by pushing onto the enqueueStack.

We dequeue by popping from the dequeueStack. If the dequeueStack is empty, we pop everything from the enqueueStack and push it onto the dequeueStack. This reverses the order so that the oldest item is now at the top of the dequeueStack.

Example

Below is the full working code to implement enqueue and dequeue using only two stacks.

<!doctype html> <html> <body> <h3> Implementing enqueue and dequeue using two stacks </h3> <div id="result1"></div> <div id="result2"></div> <div id="result3"></div> <script> class Queue { constructor() { this.enqueueStack = []; this.dequeueStack = []; } enqueue(item) { this.enqueueStack.push(item); } dequeue() { if (this.dequeueStack.length === 0) { // move everything from enqueueStack to dequeueStack while (this.enqueueStack.length > 0) { const item = this.enqueueStack.pop(); this.dequeueStack.push(item); } } // dequeue from dequeueStack return this.dequeueStack.pop(); } } // testing const queue = new Queue(); // enqueue three strings queue.enqueue("tutorials"); queue.enqueue("point"); queue.enqueue("JavaScript"); document.getElementById("result1").innerHTML = queue.dequeue(); // 1 document.getElementById("result2").innerHTML = queue.dequeue(); // 2 document.getElementById("result3").innerHTML = queue.dequeue(); // 3 </script> </body> </html>

One benefit of using two stacks is that we can keep the enqueue stack sorted. This way, when we move everything from the enqueue stack to the dequeue stack, the items will already be in the correct order.

There are a few disadvantages to using two stacks.

First, it takes up more memory than a queue implemented with a single stack.

Second, it's less efficient. Each enqueue and dequeue operation requires two stack operations (push and pop).

In this tutorial, we've seen how to implement a queue using only two stacks. This is a pretty common interview question so it's good to know how to do it.

Updated on: 03-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements