feat: Implement a FIFO-Queue

This commit is contained in:
Alexander Polynomdivision 2018-09-15 15:40:42 +02:00
parent b63a36d0f0
commit 12993570fe
2 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,16 @@
import { Queue } from "../queue";
test("Enqueue a, b and c and dequeue them", () => {
const q: Queue = new Queue<string>();
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);
});

15
src/utils/queue.ts Normal file
View File

@ -0,0 +1,15 @@
export class Queue<T> {
private elements: T[] = [];
enqueue = (element: T) => {
this.elements.push(element);
}
dequeue = (): T => {
return this.elements.shift();
}
size = (): number => {
return this.elements.length;
}
};