Skip to main content

Command Palette

Search for a command to run...

Queue data structure in Javascript

Published
•3 min read•View as Markdown
Queue data structure in Javascript
A

I'm a frontend developer with 2 years of experience working with React and Vue. I love solving problems on DSA

Queue

A queue is a linear data structure and collection of items. In a queue, items are inserted at one end called the rear and deleted at the other end called the front.

A queue is like a stack in many ways, except that it processes data in a different order. In a queue, the first item added will be the first to be removed.

The principle of the queue is First in First out "FIFO". The metaphor for this terminology is the line of people waiting to get served in a Bank, the first person in line is the first to be served, and as more people enter the line, they wait in the back until it's their turn to get served.

Some applications of queue

  1. It's used in breadth search operations in graphs

  2. Job scheduling, CPU scheduling, and Disk scheduling

  3. Priority queues are used in file-downloading operations in a browser

  4. Queues are also the perfect tool for handling asynchronous requests, they ensure that the requests are processed in the order in which they arrive.

Operations on queue

Like stacks, queues are arrays with restrictions, but some of their restrictions are different.

  • Items can be inserted only at the rear.

  • Items can be deleted only at the front.

  • Only the item at the end of a queue can be peeked.

A queue is an abstract data type that allows the following operations.

  • enqueue: inserts items at the rear of the queue

  • dequeue: removes items from the front of the queue

  • size: returns the number of items in the queue

  • isEmpty: returns a boolean value that indicates whether the queue is empty or not

  • peek: returns the front item in the stack

Queue implementation

Queue can be implemented with either an Array, as I'll be using here, or a Linked List.

Let us begin by declaring our Queue class, and inside the constructor method, we create and initialize our queueStore array.

class Queue {
    constructor() {
        this.queueStore = [];
    }
}

the enqueue method inserts items at the rear of the queue.

enqueue(item){
    this.queueStore.push(item);
}

the dequeue method removes items from the front of the queue

dequeue() {
    return this.queueStore.shift();
}

the size method returns the number of items in the queue

size(){
    return this.queueStore.length
}

the isEmpty method checks whether the queue is empty or not

isEmpty(){
    if (this.queueStore.length == 0) {
        return true
    } else {
        return false
    }
}

the peek method returns the front item in the stack

peek(){
    if (this.isEmpty()) {
        return 'No element in queue'
    } else {
        return this.queueStore[0];
    }
}

the print displays all the items in the queue

Bringing everything together

class Queue {
    constructor() {
        this.queueStore = [];
        this.top = 0;
    }
    enqueue(element){
        this.queueStore.push(element);
    }
    dequeue() {
        return this.queueStore.shift();
    }
    size(){
        return this.queueStore.length
    }
    isEmpty(){
        if (this.queueStore.length == 0) {
            return true
        } else {
            return false
        }
    }
    print(){
        let str = '';
        for (let i = 0; i < this.queueStore.length; i++) {
            str += this.queueStore[i]+ ' ';
        }
        return str
    }
    peek(){
        if (this.isEmpty()) {
            return 'No element in queue'
        } else {
            return this.queueStore[0];
        }
    }
}

// Testing our stack implementation
const queue = new Queue();

queue.enqueue('First Item')
queue.enqueue('Second Item')
queue.enqueue('Third Item')

console.log('queue size: ', queue.size())
console.log('front of queue: ', queue.peek())
console.log('Elements in queue: ', queue.print())

queue.dequeue()
console.log('queue size: ', queue.size())
console.log('front of queue: ', queue.peek())
console.log('Elements in queue: ', queue.print())

// Expected outpot
queue size: 3
front of queue: First Item
Elements in queue: First Item Second Item Third Item
// ####
queue size: 2
front of queue: Second Item
Elements in queue: Second Item Third Item