Data Structures & Algorithms in JavaScript(Queue)

Data Structures & Algorithms in JavaScript(Queue)

Hello Everyone, This is part 3 in the series of blogs about data structure and algorithms in JavaScript. Previously I have explained Stack. In this blog, I will cover Queue. 

What is the Queue?

A Queue is a linear structure which follows a particular order in which the operations are performed. The order is FIFO(First In First Out)
-geeksforgeeks.org

A real-world example of a queue can be people standing at the bus stop where the first standing in the line will be the first person to get out of the line i.e. first in first out. If you compared it to a stack, the last person will be the first to leave.

List Of Operations Available

  • Enqueue: Insert an element at the end of the queue.
  • Dequeue: Remove an element from the front of the queue.
  • Front: Return the first element of the queue.
  • Size: Return Size of the queue.
  • isEmpty: Check if the queue is empty if empty return true else false.
  • Clear: Reset the queue.

Implementation of Queue in Javascript

let’s define ES6 class name Queue, with properties : 

count:  To track the number of elements.

 items: A Javascript object which will hold all the elements.

lowestCount : since we will be removing an element from the front of the queue, we also need a variable to help us track the first element.


class Queue {
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.items = {};
    }
}

Enqueue

Inserting an element in the queue is similar to Stack’s push method and Array’s push method, which add the elements at the end.


 enqueue(element){
         this.items[this.count] = element;
         this.count ++;
     }

Dequeue

Removing an element from the queue, we have two scenarios;

  1. If empty, return undefined.
  2. Else store the lowestCount property element in a variable, To return an element after deletion, Delete the lowestCount item & increment the count by one. The dequeue method is similar to Array’s shift method.

   dequeue(){
         if (this.isEmpty()) {
             return undefined;
         }
         let result = this.items[this.lowestCount]; 
         delete this.items[this.lowestCount]; 
         this.lowestCount ++; 
         return result; 

     }

Front

This method returns the first element. To get the first element, we can return the lowestCount element


   front(){
         if (this.isEmpty()) {
             return undefined;
         }
         return this.items[this.lowestCount];

     }

Size

This method will return the size of the queue which is count minus the lowestCount.


size() {
        return this.count - this.lowestCount;
    }

Example:-In the below queue items object, If the zeroth element was removed from the front, the lowest count will be one. The total count of the element will be two, therefore, the size will count-lowest count


let queue = {
   1: "1",
   2: "2",
}

isEmpty

isEmpty will return true if the queue is empty.


 isEmpty() {
         return this.size() === 0;
    }

Clear

To clear all the elements from the queue, we can evoke the dequeue method until it returns undefined or we can simply reset the value of the Queue class properties to the same values as declared in its constructor method.

 clear() {
    this.items = {}
    this.count = 0;
    this.lowestCount = 0;
    return this.items;
    }

you can get the full source here

Conclusion :

A Queue is a data structure that uses the FIFO (First In First Out ) principle. We can insert an element at the end and remove an element from the front.

MethodsComplexity
equeueO(1)
dequeueO(1)
frontO(1)
sizeO(1)
Complexity of the Queue

So, stay tuned for the next blog, in which cover another Deque.

Swarup Das

Swarup Das

Web Developer, Tech Enthusiast. Passionate about new technology and programming languages.

Leave a Reply

Your email address will not be published. Required fields are marked *