Implementation of Priority Queue in Javascript

link 0

  1. Each element of the priority queue has an property associated with it.
  2. Elements are added to the queue as per the priority.
  3. Lowest priority elements are removed first.

We can design a priority queue using two approaches in the first case we can add the queue element at the end of the queue and we can remove the elements of the queue depending on the priority. In the second case we can add elements to the queue according to the priority and remove them from the front of the queeue. In this article we would use the second approach to implement a Priority Queue.
Note: Assuming a Priority queue can grow dynamically we are not considering the overflow condition.
Let’s see an example of a priority queue class:

// User defined class
// to store element and its priority
class QElement {
    constructor(element, priority)
        this.element = element;
        this.priority = priority;
// PriorityQueue class
class PriorityQueue {
    // An array is used to implement priority
        this.items = [];
    // functions to be implemented
    // enqueue(item, priority)
    // dequeue()
    // front()
    // isEmpty()
    // printPQueue()

As you can see in the example above we have defined skeleton of PriorityQueue class. We have use an user defined class QElement having two property element and priority. We have used an array in PriorityQueue class to implement priority queue, this array is a container of QElement.

  1. enqueue() – adds an element to the queue according to its priority.
    // enqueue function to add element
    // to the queue as per priority
    enqueue(element, priority)
        // creating object from queue element
        var qElement = new QElement(element, priority);
        var contain = false;
        // iterating through the entire
        // item array to add element at the
        // correct location of the Queue
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].priority > qElement.priority) {
                // Once the correct location is found it is
                // enqueued
                this.items.splice(i, 0, qElement);
                contain = true;
        // if the element have the highest priority
        // it is added at the end of the queue
        if (!contain) {

    In this method we create a qElement have property element and priority. Then we iterate over the queue to find the correct location of the qElement according to its priority and add it.

  2. dequeue() – Removes an element from the priority queue
    // dequeue method to remove
    // element from the queue
        // return the dequeued element
        // and remove it.
        // if the queue is empty
        // returns Underflow
        if (this.isEmpty())
            return "Underflow";
        return this.items.shift();

    This function removes an element from the front of a queue as the highest priority element is stored at the front of the priority queue. We have used shift method of an array to remove an element from the queue.

  3. front() – returns the front element of the Priority queue
    // front function
        // returns the highest priority element
        // in the Priority queue without removing it.
        if (this.isEmpty())
            return "No elements in Queue";
        return this.items[0];

    This function returns the front element of the Priority queue. We simply return the 0th element of an array to get the front of a Priority queue.

  4. rear() – returns the last element of the Priority queue
    // rear function
        // returns the lowest priorty
        // element of the queue
        if (this.isEmpty())
            return "No elements in Queue";
        return this.items[this.items.length - 1];

    This functions return the last element of the queue or lowest priority element.

Helper Methods:
Let’s declare some helper method which is quite useful while working with the Priority queue queue.

  1. isEmpty() – Returns true if the Priority queue is empty
    // isEmpty function
        // return true if the queue is empty.
        return this.items.length == 0;

    We have used the length property of an array to get the length and if its 0 then priority queue is empty.

  2. printPQueue – It prints the element of the queue as per the priority starting from highest to lowest
    // printQueue function
    // prints all the element of the queue
        var str = "";
        for (var i = 0; i < this.items.length; i++)
            str += this.items[i].element + " ";
        return str;

    In this method we concatenate the element property of each priority queue item into an string.

Note :- Here we consider ” 1 ” as the highest priority element, you can modify this as per the requirement.
Now Let use this Priority Queue class and its different method described above


// creating object for queue classs
var priorityQueue = new PriorityQueue();
// testing isEmpty and front on an empty queue
// return true
// returns "No elements in Queue"
// adding elements to the queue
priorityQueue.enqueue("Sumit", 2);
priorityQueue.enqueue("Gourav", 1);
priorityQueue.enqueue("Piyush", 1);
priorityQueue.enqueue("Sunny", 2);
priorityQueue.enqueue("Sheru", 3);
// prints [Gourav Piyush Sumit Sunny Sheru]
// prints Gourav
// pritns Sheru
// removes Gouurav
// priorityQueue contains
// [Piyush Sumit Sunny Sheru]
// Adding another element to the queue
priorityQueue.enqueue("Sunil", 2);
// prints [Piyush Sumit Sunny Sunil Sheru]

Disclaimer: This content belongs to geeksforgeeks, source:


Leave a Reply

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

Skip to toolbar