From f443d36dbb6e819626d3feac59d3afd570f0e52f Mon Sep 17 00:00:00 2001 From: Aya Morisawa Date: Tue, 14 Aug 2018 07:49:59 +0900 Subject: Resolve #2176 --- src/misc/queue.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/misc/queue.ts (limited to 'src/misc') diff --git a/src/misc/queue.ts b/src/misc/queue.ts new file mode 100644 index 0000000000..410878ba8b --- /dev/null +++ b/src/misc/queue.ts @@ -0,0 +1,33 @@ +type Node = { value: T, next: Node }; + +export default class Queue { + private top: Node = null; + private rear: Node = null; + public length: number = 0; + + public push(value: T): void { + const node: Node = { 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(this.length); + for (let node = this.top, i = 0; node !== null; node = node.next, i++) { + arr[i] = node.value; + } + return arr; + } +} -- cgit v1.2.3-freya