-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.ts
More file actions
43 lines (33 loc) · 672 Bytes
/
Queue.ts
File metadata and controls
43 lines (33 loc) · 672 Bytes
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
43
export class Node<T> {
value: T;
next?: Node<T>;
constructor(value: T) {
this.value = value;
}
}
export class Queue<T> {
#head?: Node<T>;
#tail?: Node<T>;
#size = 0;
enqueue(value: T): void {
const item = new Node(value);
if (this.#tail === undefined) {
this.#head = item;
this.#tail = item;
} else {
this.#tail.next = item;
this.#tail = item;
}
this.#size++;
}
dequeue(): T | undefined {
if (this.#head === undefined) return;
const oldHead = this.#head;
this.#size--;
this.#head = oldHead.next;
return oldHead.value;
}
get size(): number {
return this.#size;
}
}