Initial commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user