From 638b503f3046fcc34a720ac4405831f08c5c1034 Mon Sep 17 00:00:00 2001 From: n-ryu Date: Wed, 7 Dec 2022 00:41:24 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B5=9C=EC=A0=81=ED=99=94=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=B4=20Queue=20=EC=A7=81=EC=A0=91=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/util/queue.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 client/src/util/queue.ts diff --git a/client/src/util/queue.ts b/client/src/util/queue.ts new file mode 100644 index 0000000..c6e88db --- /dev/null +++ b/client/src/util/queue.ts @@ -0,0 +1,40 @@ +interface QueueObj { + prev: QueueObj | undefined + next: QueueObj | undefined + value: T | undefined; +} + +class Queue { + private head: QueueObj; + private tail: QueueObj; + #length: number; + constructor() { + this.head = {prev: undefined, next: undefined, value: undefined}; + this.tail = {prev: this.head, next: undefined, value: undefined}; + this.head.next = this.tail; + this.#length = 0; + } + length() : number { + return this.#length; + } + isEmpty() : boolean { + if(this.length() === 0) return true; + return false; + } + push(input: T): void { + const newQueueObj = {value: input, prev: this.tail.prev, next: this.tail}; + if(this.tail.prev) this.tail.prev.next = newQueueObj; + this.tail.prev = newQueueObj; + this.#length ++; + } + pop(): T{ + const target = this.head.next; + if(this.isEmpty() || target === undefined) throw new Error("Error: You can't pop from empty queue"); + this.head.next = target.next; + if(target.next) target.next.prev = this.head; + this.#length --; + return target.value as T; + } +} + +export default Queue; \ No newline at end of file