diff --git a/src/utils/__test__/queue.test.ts b/src/utils/__test__/queue.test.ts new file mode 100644 index 0000000..d06e69c --- /dev/null +++ b/src/utils/__test__/queue.test.ts @@ -0,0 +1,16 @@ +import { Queue } from "../queue"; + +test("Enqueue a, b and c and dequeue them", () => { + const q: Queue = new Queue(); + q.enqueue("a"); + q.enqueue("b"); + q.enqueue("c"); + + expect(q.size()).toBe(3); + + expect(q.dequeue()).toEqual("a"); + expect(q.dequeue()).toEqual("b"); + expect(q.dequeue()).toEqual("c"); + + expect(q.size()).toBe(0); +}); diff --git a/src/utils/queue.ts b/src/utils/queue.ts new file mode 100644 index 0000000..7265ea3 --- /dev/null +++ b/src/utils/queue.ts @@ -0,0 +1,15 @@ +export class Queue { + private elements: T[] = []; + + enqueue = (element: T) => { + this.elements.push(element); + } + + dequeue = (): T => { + return this.elements.shift(); + } + + size = (): number => { + return this.elements.length; + } +};