Skip to content

Instantly share code, notes, and snippets.

@intrnl
Last active Dec 21, 2020
Embed
What would you like to do?
export class Queue<V = any> {
head?: Node<V>;
tail?: Node<V>;
size!: number;
constructor () {
this.clear();
}
enqueue (value: V): void {
let node: Node<V> = { value };
if (this.tail) {
this.tail.next = node;
this.tail = node;
} else {
this.head = this.tail = node;
}
this.size++;
}
dequeue (): V | void {
let node = this.head;
if (!node) return;
this.head = this.head.next;
this.size--;
return node.value;
}
peek (): V | void {
if (this.head) return this.head.value;
}
clear (): void {
this.head = this.tail = undefined;
this.size = 0;
}
}
export interface Node<V> {
value: V,
next?: Node<V>,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment