put queue in another module instead
for future reusability
This commit is contained in:
parent
586578fe45
commit
f2e9869965
2 changed files with 43 additions and 26 deletions
41
scripts/common.js
Normal file
41
scripts/common.js
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue