27 lines
687 B
JavaScript
27 lines
687 B
JavaScript
|
export class Queue {
|
||
|
#pushStack;
|
||
|
#shiftStack;
|
||
|
constructor() {
|
||
|
this.#pushStack = [];
|
||
|
this.#shiftStack = [];
|
||
|
}
|
||
|
get length() {
|
||
|
return this.#pushStack.length + this.#shiftStack.length;
|
||
|
}
|
||
|
push(elem) {
|
||
|
this.#pushStack.push(elem);
|
||
|
}
|
||
|
shift() {
|
||
|
if (this.#shiftStack.length === 0 && this.#pushStack.length > 0) {
|
||
|
this.#shiftStack = this.#pushStack.reverse();
|
||
|
this.#pushStack = [];
|
||
|
}
|
||
|
return this.#shiftStack.pop();
|
||
|
}
|
||
|
first() {
|
||
|
return this.#shiftStack.length !== 0
|
||
|
? this.#shiftStack[this.#shiftStack.length - 1]
|
||
|
: this.#pushStack[0];
|
||
|
}
|
||
|
}
|