Remove elements from a PriorityQueue using Javascript


Dequeuing elements from a PriorityQueue means removing the element of the highest priority. We are storing the elements with the highest priority at the end of the array, we can simply pop it to dequeue it.

Hence, we can implement the dequeue function as follows − 

Example

dequeue() {
   // Check if empty
   if (this.isEmpty()) {
      console.log("Queue Underflow!");
      return;
   }
   return this.container.pop();
}

You can check if this function is working fine using

let q = new PriorityQueue(4);
q.enqueue("Hello", 3);
q.enqueue("World", 2);
q.enqueue("Foo", 8);
console.log(q.dequeue());
q.display();

Output

This will give the output −

{ data: 'Foo', priority: 8 }
[ { data: 'World', priority: 2 },
   { data: 'Hello', priority: 3 }]

Updated on: 15-Jun-2020

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements