DEV Community

Swarup Das
Swarup Das

Posted on • Updated on

Data Structures & Algorithms in JavaScript(Queue)

Hello Everyone, Welcome to the 3rd part of the series on JavaScript Data Structures and Algorithms. In the 2nd part, we discussed the Stacks Data Structure, but in this article, we will discuss the Queue Data Structure.

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.

This article will go through a list of following Queue DS,

  • Queue.
  • Deque(Double-ended queue).

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 = {};
    }
}

Enter fullscreen mode Exit fullscreen mode

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 ++;
     }

Enter fullscreen mode Exit fullscreen mode

Dequeue

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

  • If empty, return undefined.
  • 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; 

     }
Enter fullscreen mode Exit fullscreen mode

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];

     }
Enter fullscreen mode Exit fullscreen mode

Size

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


size() {
        return this.count - this.lowestCount;
    }
Enter fullscreen mode Exit fullscreen mode

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",
}

Enter fullscreen mode Exit fullscreen mode

isEmpty

isEmpty will return true if the queue is empty.


 isEmpty() {
         return this.size() === 0;
    }
Enter fullscreen mode Exit fullscreen mode

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;
    }
Enter fullscreen mode Exit fullscreen mode

you get the full source here

Conclusion :

Methods Complexity
equeue O(1)
dequeue O(1)
front O(1)
size O(1)

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

Top comments (2)

Collapse
 
kulpras profile image
Prasanna Kulkarni

Nice post swarup.
One correction, please update this line Implementation of Stack in Javascript

Collapse
 
swarup260 profile image
Swarup Das

😅👍