Remove elements from a PriorityQueue using Javascript

Web DevelopmentFront End TechnologyJavascript

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 }]
raja
Published on 27-Jun-2018 08:43:35
Advertisements