How to implement queue - JavaScript

Mo Ibra

Mo Ibra

Jan 6, 2023

In this article we will learn how to implementing queue Data Structure in Javascript.

queue is a linear data structure in which addition or removal of element follows a particular order i.e. FIFO(First in First Out).


Create a skeleton of a queue class

Skeleton of a queue class

class Queue {

    // Array is used to implement queue
    constructor() {
        this.items = [];
    }

    // Functions to be implemented:
    // enqueue(item)
    // dequeue()
    // peek()
    // isEmpty()
    // printQueue()
}

Now we have created a skeleton for the class, and when that class is called, we can access the methods and properties of that class, like this:

const queue = new Queue();

But we didn't create any properties or methods for that class, we just did a skeleton, so if we print that class we won't find anything in it.

const queue = new Queue();

console.log(queue);
// Queue { items: [] }

Now let’s see implementation of each method:


Implementation of enqueue method

Now we will implement the enqueue method, and the enqueue method all it do is add an element to the end of the queue.

class Queue {

    // Array is used to implement queue
    constructor() {
        this.items = [];
    }

    // enqueue method
    enqueue(element) {
        // Add element to end of queue
        this.items.push(element);
    }
}

Now that we've done the implementation, let's do a test for the queue

// Create new queue
const queue = new Queue();

// Print before `enqueue`
console.log(queue);

// Add new element to queue
queue.enqueue('John');

// Print after `enqueue`
console.log(queue);

Output

Queue { items: [] }
Queue { items: [ 'John' ] }

As you can see, the element has been added at the end of the queue.

Now let's create the function that deletes the last element of the queue.

Implementation of dequeue method

dequeue method remove an element from the front of the queue.

Implementation

class Queue {

    // Array is used to implement queue
    constructor() {
        this.items = [];
    }

    // enqueue method
    enqueue(element) {
        this.items.push(element);
    }
    
    // dequeue method
    dequeue() {
        if (this.items.length === 0) return "Empty Queue";
        return this.items.shift();
    }
}

Now that we've done the implementation, let's do a test for the queue

// Create new queue
const queue = new Queue();

// Print queue
console.log(queue);

// enqueue method
queue.enqueue('John');
queue.enqueue("Doe");

// Remove last one
console.log(queue.dequeue());

// Print queue
console.log(queue);

// dequeue
console.log(queue.dequeue());

// Print queue
console.log(queue);

// Empty Queue
console.log(queue.dequeue());

Output

Queue { items: [] }
John
Queue { items: [ 'Doe' ] }
Doe
Queue { items: [] }
Empty Queue

Now we can say that we are done, but there are some helpers methods that we must implement like isEmpty and printQueue.


Implementation of isEmpty method

This function returns true or false, all it does is check whether the queue is empty or not.

Implementation

// check whether the queue is empty or not.
isEmpty() {
    return this.items.length === 0;
}

let's do a test for the queue

// Create a queue
const queue = new Queue();

// Check if isEmpty or not
console.log(queue.isEmpty());

// Add elements
queue.enqueue('John');
queue.enqueue("Doe");

// Print queue
console.log(queue);

// Check if isEmpty or not
console.log(queue.isEmpty())

Output

true
Queue { items: [ 'John', 'Doe' ] }
false

Implementation of peek method

peek method return the first element without removing it from the queue.

Implementation

// return the first element from the queue without delete
peek() {
    if (this.isEmpty()) {
        return "No elements in queue";
    }
    return this.items[0];
}

Now that we've done the implementation, let's do a test for the queue

// Create a queue
const queue = new Queue();

// Add elements
queue.enqueue('John');
queue.enqueue("Doe");

// Print queue
console.log(queue);

// Peek method
console.log(queue.peek());

Output

Queue { items: [ 'John', 'Doe' ] }
John

Implementation of printQueue method

This method returns a string in which all the element of an queue is concatenated.

Implementation

// Print queue
printQueue() {
    var str = "";
    for (let i = 0; i < this.items.length; i++) {
        str += this.items[i] + " ";
    }
    return str;
}

Now that we've done the implementation, let's do a test for the queue

// queue
const queue = new Queue();

// Add elements
queue.enqueue('John');
queue.enqueue("Doe");

// Print queue
console.log(queue.printQueue());

Output

John Doe 

Full code:

class Queue {

    // Array is used to implement queue
    constructor() {
        this.items = [];
    }

    // enqueue method
    enqueue(element) {
        this.items.push(element);
    }
    
    // dequeue method
    dequeue() {
        if (this.items.length === 0) return "Empty Queue";
        return this.items.shift();
    }

    // Check if queue is empty or not.
    isEmpty() {
        return this.items.length === 0;
    }

    // return the first element from the queue without delete
    peek() {
        if (this.isEmpty()) {
            return "No elements in queue";
        }
        return this.items[0];
    }

    // Print queue elements
    printQueue() {
        var str = "";
        for (let i = 0; i < this.items.length; i++) {
            str += this.items[i] + " ";
        }
        return str;
    }
}

Thank you for reading

Thank you for reading my blog. 🚀

Suggestions For More Articles: