type Node = { value: T; next?: Node; }; export default class Queue { public length: number; private head?: Node; private tail?: Node; constructor() { this.head = undefined; this.tail = undefined; this.length = 0; } enqueue(item: T): void { const node = { value: item } as Node; this.length++; if (!this.tail) { this.head = node; this.tail = node; return; } this.tail.next = node; this.tail = node; } deque(): T | undefined { if (!this.head) { return undefined; } this.length--; const head = this.head; this.head = this.head.next; head.next = undefined; if (this.length === 0) { this.tail = undefined; } return head.value; } peek(): T | undefined { return this.head?.value; } }