summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-08-14 07:51:45 +0900
committerGitHub <noreply@github.com>2018-08-14 07:51:45 +0900
commit0702d0974b7608b4e59c3f2af97bc9842e6bd086 (patch)
tree27d3a6ad4bbfdfa69fdc8eab072b4ddfe46df3a2 /src/misc
parentMerge #2182 (diff)
parentResolve #2176 (diff)
downloadsharkey-0702d0974b7608b4e59c3f2af97bc9842e6bd086.tar.gz
sharkey-0702d0974b7608b4e59c3f2af97bc9842e6bd086.tar.bz2
sharkey-0702d0974b7608b4e59c3f2af97bc9842e6bd086.zip
Merge pull request #2199 from syuilo/patch-2176
Resolve #2176
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/queue.ts33
1 files changed, 33 insertions, 0 deletions
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<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;
+ }
+}