2022-16 in js
This commit is contained in:
parent
db5e94e884
commit
c9654e1ecc
7 changed files with 337 additions and 0 deletions
55
2022/16-proboscidea-volcanium/priority_queue.js
Normal file
55
2022/16-proboscidea-volcanium/priority_queue.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
export class QElement {
|
||||
constructor(element, priority) {
|
||||
this.element = element;
|
||||
this.priority = priority;
|
||||
}
|
||||
}
|
||||
|
||||
export class PriorityQueue {
|
||||
constructor() {
|
||||
this.items = [];
|
||||
}
|
||||
|
||||
enqueue(element, priority) {
|
||||
var qElement = new QElement(element, priority);
|
||||
var contain = false;
|
||||
|
||||
for (var i = 0; i < this.items.length; i++) {
|
||||
if (this.items[i].priority > qElement.priority) {
|
||||
this.items.splice(i, 0, qElement);
|
||||
contain = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!contain) {
|
||||
this.items.push(qElement);
|
||||
}
|
||||
}
|
||||
dequeue() {
|
||||
if (this.isEmpty()){
|
||||
throw "Attempting to dequeue an empty queue";
|
||||
}
|
||||
return this.items.shift();
|
||||
}
|
||||
front() {
|
||||
if (this.isEmpty()){
|
||||
throw "Attempting to front an empty queue";
|
||||
}
|
||||
return this.items[0];
|
||||
}
|
||||
rear() {
|
||||
if (this.isEmpty()){
|
||||
throw "Attempting to rear an empty queue";
|
||||
}
|
||||
return this.items[this.items.length - 1];
|
||||
}
|
||||
isEmpty() {
|
||||
return this.items.length == 0;
|
||||
}
|
||||
printPQueue() {
|
||||
var str = "";
|
||||
for (var i = 0; i < this.items.length; i++)
|
||||
str += this.items[i].element + " ";
|
||||
return str;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue