feat: Implement a FIFO-Queue
This commit is contained in:
parent
b63a36d0f0
commit
12993570fe
16
src/utils/__test__/queue.test.ts
Normal file
16
src/utils/__test__/queue.test.ts
Normal 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
15
src/utils/queue.ts
Normal 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;
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user