summaryrefslogtreecommitdiff
path: root/src/misc/queue.ts
blob: 410878ba8b7bfe726a960dd3be617e72ca25d614 (plain)
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
type Node<T> = { value: T, next: Node<T> };

export default class Queue<T> {
	private top: Node<T> = null;
	private rear: Node<T> = null;
	public length: number = 0;

	public push(value: T): void {
		const node: Node<T> = { value, next: null };
		if (this.top === null) {
			this.top = node;
			this.rear = node;
		} else {
			this.rear.next = node;
			this.rear = node;
		}
		this.length++;
	}

	public pop(): void {
		this.top = this.top.next;
		if (this.top == null) this.rear = null;
		this.length--;
	}

	public toArray(): T[] {
		const arr: T[] = Array<T>(this.length);
		for (let node = this.top, i = 0; node !== null; node = node.next, i++) {
			arr[i] = node.value;
		}
		return arr;
	}
}