aboutsummaryrefslogtreecommitdiff
path: root/2022/16-proboscidea-volcanium/priority_queue.js
diff options
context:
space:
mode:
authorJulien Dessaux2022-12-26 07:48:55 +0100
committerJulien Dessaux2022-12-27 11:18:43 +0100
commitc9654e1ecc29eb124d5981726f33701cd70d1e1f (patch)
treebe8d526113d0d29ee2a3548176123d2425907f98 /2022/16-proboscidea-volcanium/priority_queue.js
parent2022-15 part 2 in zig (diff)
downloadadvent-of-code-c9654e1ecc29eb124d5981726f33701cd70d1e1f.tar.gz
advent-of-code-c9654e1ecc29eb124d5981726f33701cd70d1e1f.tar.bz2
advent-of-code-c9654e1ecc29eb124d5981726f33701cd70d1e1f.zip
2022-16 in js
Diffstat (limited to '2022/16-proboscidea-volcanium/priority_queue.js')
-rw-r--r--2022/16-proboscidea-volcanium/priority_queue.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/2022/16-proboscidea-volcanium/priority_queue.js b/2022/16-proboscidea-volcanium/priority_queue.js
new file mode 100644
index 0000000..c1a5b65
--- /dev/null
+++ b/2022/16-proboscidea-volcanium/priority_queue.js
@@ -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;
+ }
+}