Files
2026-08-24 22:20:36 +02:00

31 lines
652 B
JavaScript

class PriorityQueue {
constructor() {
this.items = [];
};
enqueue(item, priority) {
const element = { item, priority };
let added = false;
for (let i = 0; i < this.items.length; i++) {
if (priority < this.items[i].priority) {
this.items.splice(i, 0, element);
added = true;
break;
};
};
if (!added) this.items.push(element);
}
dequeue() {
return this.isEmpty() ? null : this.items.shift().item;
}
isEmpty() {
return this.items.length === 0;
}
};
module.exports = PriorityQueue;