| 1234567891011121314151617181920212223242526272829303132 |
- export class Queue<T> {
- private items: T[] = [];
- enqueue(item: T) {
- this.items.push(item);
- }
- dequeue(): T | undefined {
- return this.items.shift();
- }
- peek(): T | undefined {
- return this.items[0];
- }
- isEmpty(): boolean {
- return this.items.length === 0;
- }
- size(): number {
- return this.items.length;
- }
- clear() {
- this.items = [];
- }
- // Add forEach to iterate over the queue's items in order
- forEach(callback: (item: T, index: number, array: T[]) => void) {
- this.items.forEach(callback);
- }
- }
|