Implement enqueue and dequeue

Last Updated : 14 Aug, 2025

We use two stacks to mimic FIFO with LIFO operations. One stack handles additions, and we flip elements to the second stack for removals.

  • Stack: Follows LIFO (Last In, First Out). Like a stack of plates—the top plate is removed first.
  • Queue: Follows FIFO (First In, First Out). Like a line at a ticket counter—the first person leaves first.

Approach 1 :

Use stack1 for enqueue. For dequeue, move elements from stack1 to stack2 (reversing order), then pop from stack2.

  • In this approach we will first initialize two stacks (in the form of two plain arrays).
  • Thereafter we will perform enqueue operation into the first stack with several elements which are given by user itself.
  • Further after performing enqueue operation we will define the dequeue operation for the above inserted elements into the first stack.
  • For dequeue operation we will first have to pop or remove the last element from the first stack.
  • Then after removing or popping out the last element from the first stack we will add that popped element into another stack (which is also in form of array).
  • Then after the adding the element into the new array we will pop or remove that element from the next stack and this way we could perform the dequeue operation.

The above pictorial representation represents the enqueue operation which was being performed with several elements (like a ,b, c and so on) which are inserted into stack (declared in the form of plain array).

Example :

JavaScript
    // Two stacks declared in the form of plain array
    let stack1 = [];
    let stack2 = [];

    // Method that will perform our enqueue operation
    function enqueue(element) {
        stack1.push(element);
        console.log("Stack-1 elements are enqueue: ", stack1);
    }

    // Method that will perform our dequeue operation
    function dequeue() {
        if (stack2.length === 0) {
            if (stack1.length === 0) {
                console.log(
            "Dequeue not possible because queue is empty..");
            }
            while (stack1.length > 0) {
                let x = stack1.pop();
                stack2.push(x);
            }
        }
        console.log("Element after Dequeue: " + stack2.pop());
    }

    enqueue("a");
    enqueue("b");
    enqueue("c");
    dequeue();
    dequeue();

Output
Stack-1 elements are enqueue:  [ 'a' ]
Stack-1 elements are enqueue:  [ 'a', 'b' ]
Stack-1 elements are enqueue:  [ 'a', 'b', 'c' ]
Element after Dequeue: a
Element after Dequeue: b

Approach 2 :

This version handles any order of enqueues/dequeues, like dequeuing before enqueuing or multiple dequeues.

  • This approach works dynamically for multiple enqueue and dequeue operations.
  • Here we will handle multiple cases like if dequeue was called before enqueue, or multiple dequeue's are called, or if enqueue is just called after dequeue or only single operation is just called after dequeue.
  • As similar to first approach here also we are going to make two separate functions or methods for both enqueue and dequeue operations.
  • In these two separate methods we will perform the individual logics of enqueue and dequeue respectively.

The above pictorial representation represents the dequeue operation which was being performed using several elements as well as two stacks by which we will first pop (remove) the last element from the first stack and then we will add that removed element into another stack and further remove or pop the element from the another stack. Basically removing and adding into another stack is done so that this works in reverse to what exactly stack works, or we could say reversing is done here so as to make our logic similar to queue functionality rather than working as stack itself.

Example:

JavaScript
    // Two stacks declared in array form
    let stack1 = [];
    let stack2 = [];

    // Method to implement enqueue operation
    function enqueue(element) {

        // if dequeue was called before actual
        // enqueue operation
        if (stack2.length > 0) {
            let len = stack2.length;
            for (let i = 0; i < len; i++) {
                let p = stack2.pop();
                stack1.push(p);
            }
        }
        stack1.push(element);
        console.log("Elements after Enqueue: ", stack1);
    }

    // Method to implement dequeue operation......
    function dequeue() {

        // If dequeue was called consecutively, all
        // the elements would be in stack2
        if (stack2.length > 0) {
            console.log("Element after dequeue : "
                + stack2.pop());
        }

        // If enqueue was called right before
        // this dequeue, stack2 is empty
        else if (stack2.length === 0) {
            if (stack1.length === 0) {

                // If the first operation is
                // dequeue itself
                console.log("Queue is empty");
            } else if (stack1.length === 1) {

                // If a single operation as
                // enqueue was performed
                console.log(stack1.pop());
            }

            // If enqueue was called before this
            // operation, all the elements are in
            // stack1, so pop them and push the 
            // elements into stack2,  then pop()
            else if (stack1.length > 0) {
                let len = stack1.length;
                for (let i = 0; i < len; i++) {
                    let p = stack1.pop();
                    stack2.push(p);
                }
                console.log("Element after dequeue: "
                    + stack2.pop());
            }
        }
    }
    enqueue("a");
    enqueue("b");
    enqueue("c");
    dequeue();
    enqueue("d");
    enqueue("e");
    dequeue();
    dequeue();
    dequeue();
    enqueue("f");

Output
Elements after Enqueue:  [ 'a' ]
Elements after Enqueue:  [ 'a', 'b' ]
Elements after Enqueue:  [ 'a', 'b', 'c' ]
Element after dequeue: a
Elements after Enqueue:  [ 'b', 'c', 'd' ]
Elements after Enq...

Comment