-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
42 lines (40 loc) · 1008 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// deno-lint-ignore no-explicit-any
class MyQueue<T = any> {
#inStack: Array<T> = [];
#outStack: Array<T> = [];
push(value: T): void {
this.#inStack.push(value);
}
pop(): number | T {
if (!this.#outStack.length) {
if (!this.#inStack.length) {
return -1;
}
this.in2out();
}
return this.#outStack.pop() as T;
}
in2out() {
while (this.#inStack.length) {
this.#outStack.push(this.#inStack.pop() as T);
}
}
peek(): T | number {
if (!this.#outStack.length) {
if (!this.#inStack.length) {
return -1;
}
this.in2out();
}
return this.#outStack[this.#outStack.length - 1] as T;
}
empty(): boolean {
if (!this.#outStack.length) {
if (!this.#inStack.length) {
return true;
}
}
return false;
}
}
export default MyQueue;