put queue in another module instead

for future reusability
This commit is contained in:
buzz-lightsnack-2007 2024-04-13 23:24:08 +08:00
parent 586578fe45
commit f2e9869965
2 changed files with 43 additions and 26 deletions

41
scripts/common.js Normal file
View file

@ -0,0 +1,41 @@
// common.js provides the advanced data structures.
export class Queue {
#elements;
constructor() {
this.#elements = [];
}
enqueue(element) {
this.#elements.push(element);
}
dequeue() {
return this.#elements.shift();
}
isEmpty() {
return this.#elements.length <= 0;
}
}
export class Stack {
#elements;
constructor() {
this.#elements = [];
}
push(element) {
this.#elements.push(element);
}
pop() {
return this.#elements.pop();
}
isEmpty() {
return this.#elements.length <= 0;
}
}