使用 JavaScript 从优先级队列中移除元素


从优先级队列中取消排队元素意味着移除优先级最高的元素。我们将优先级最高的元素存储在数组末尾,我们可以简单地弹出它以取消排队。

因此,我们可以按以下方式实现出队函数 - 

示例

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

你可以使用检查此函数是否运行正常

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

输出

它会生成如下输出 -

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

更新于: 15-6月-2020

226 个浏览量

开启你的职业

完成课程获取证书

开始
广告
© . All rights reserved.