Skip to content

Commit

Permalink
feat: 최적화를 위해 Queue 직접 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
n-ryu committed Dec 6, 2022
1 parent 744e570 commit 638b503
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions client/src/util/queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
interface QueueObj<T> {
prev: QueueObj<T> | undefined
next: QueueObj<T> | undefined
value: T | undefined;
}

class Queue<T> {
private head: QueueObj<T>;
private tail: QueueObj<T>;
#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;

0 comments on commit 638b503

Please sign in to comment.