The Priority Queue in Javascript



In this article, we are going to discuss the priority queue data structure in JavaScript.

A priority queue is an abstract data type (ADT) which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.

Example 1

The following example demonstrates the priority queue class data structure in JavaScript.

Here, we insert the elements into the queue according to their priority. After creating a non-empty queue we can remove the element with the highest priority (by returning the front element using the shift() method).

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>PriorityQueue Data Structure</title>
   </head>
   <body>
      <script type="text/javascript">
         class QueueElement {
            constructor(elem, priNo) {
            this.element = elem;
            this.priority = priNo;
            }
         }
         class PriorityQueue {
            constructor() {
               this.queArr = [];
            }
            enqueue(elem, priNo) {
               let queueElem = new QueueElement(elem, priNo);
               let contain = false;
               for (let i = 0; i < this.queArr.length; i++) {
                  if (this.queArr[i].priority > queueElem.priority) {
                     this.queArr.splice(i, 0, queueElem);
                     contain = true;
                     break;
                  }
               }
               if (!contain) {
                  this.queArr.push(queueElem);
               }
            }
            dequeue() {
               document.write(
                  "</br>The dequeued element in the priority queue is : "
               );
               if (this.isEmpty()) return "Underflow";
               return this.queArr.shift();
            }
            front() {
               if (this.isEmpty()) return "No elements in Queue";
               return this.queArr[0];
            }
            rear() {
               document.write("</br>The rear element of the priority queue is : ");
               if (this.isEmpty()) return "The Queue is Empty..!";
               return this.queArr[this.queArr.length - 1];
            }
            isEmpty() {
               return this.queArr.length == 0;
            }
            display() {
               document.write("The Elements in the priority queue are : </br>");
               let res_Str = "";
               for (let i = 0; i < this.queArr.length; i++)
               res_Str += this.queArr[i].element + " ";
               return res_Str;
            }
         }
         var priorityQueue = new PriorityQueue();
         priorityQueue.enqueue("Alice", 2);
         priorityQueue.enqueue("Cullen", 4);
         priorityQueue.enqueue("Edward", 1);
         priorityQueue.enqueue("Bella", 2);
         priorityQueue.enqueue("Jacob", 3);
         document.write(priorityQueue.display());
      </script>
   </body>
</html>

Example 2

The following example demonstrates the implementation of priority queue class data structure in JavaScript.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>PriorityQueue Data Structure</title>
   </head>
   <body>
      <script type="text/javascript">
         class QueueElement {
            constructor(elem, priNo) {
            this.element = elem;
            this.priority = priNo;
            }
         }
         class PriorityQueue {
            constructor() {
               this.queArr = [];
            }
            enqueue(elem, priNo) {
               let queueElem = new QueueElement(elem, priNo);
               let contain = false;
               for (let i = 0; i < this.queArr.length; i++) {
                  if (this.queArr[i].priority > queueElem.priority) {
                     this.queArr.splice(i, 0, queueElem);
                     contain = true;
                     break;
                  }
               }
               if (!contain) {
                  this.queArr.push(queueElem);
               }
            }
            dequeue() {
               document.write(
                  "</br>The dequeued element in the priority queue is : "
               );
               if (this.isEmpty()) return "Underflow";
               return this.queArr.shift();
            }
            front() {
               document.write("</br>The front element of the priority queue is : ");
               if (this.isEmpty()) return "The Queue is Empty..!";
               return this.queArr[0];
            }
            rear() {
               document.write("</br>The rear element of the priority queue is : ");
               if (this.isEmpty()) return "The Queue is Empty..!";
               return this.queArr[this.queArr.length - 1];
            }
            isEmpty() {
               return this.queArr.length == 0;
            }
            display() {
               document.write("The Elements in the priority queue are : </br>");
               let res_Str = "";
               for (let i = 0; i < this.queArr.length; i++)
               res_Str += this.queArr[i].element + " ";
               return res_Str;
            }
         }
         var priorityQueue = new PriorityQueue();
         priorityQueue.enqueue("Alice", 2);
         priorityQueue.enqueue("Cullen", 4);
         priorityQueue.enqueue("Edward", 1);
         priorityQueue.enqueue("Bella", 2);
         priorityQueue.enqueue("Jacob", 3);
         document.write(priorityQueue.display());
         document.write(priorityQueue.front().element);
         document.write(priorityQueue.rear().element);
      </script>
   </body>
</html>

Advertisements