|
| 1 | +import { Queue } from "./queue"; |
| 2 | + |
| 3 | +type Node<T> = { |
| 4 | + value: T, |
| 5 | + next?: Node<T>, |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * This is a LinkedList-like implementation of a Queue, |
| 10 | + * allowing the operations to be implemented in constant time. |
| 11 | + * A Queue is a data structure that follows the FIFO (First-In First-Out) principle: |
| 12 | + * The first element that was added to the queue will be the first one to be removed. |
| 13 | + */ |
| 14 | +export class LinkedQueue<T> implements Queue<T> { |
| 15 | + |
| 16 | + public size: number; |
| 17 | + public head?: Node<T>; |
| 18 | + private tail?: Node<T>; |
| 19 | + |
| 20 | + constructor() { |
| 21 | + this.head = this.tail = undefined; |
| 22 | + this.size = 0; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Adds an item to the queue. |
| 27 | + * |
| 28 | + * @param item The item being added to the queue. |
| 29 | + */ |
| 30 | + enqueue(item: T): void { |
| 31 | + const node = { value: item } as Node<T>; // Creates a new node |
| 32 | + this.size++ // Increase the length of the Queue |
| 33 | + |
| 34 | + |
| 35 | + if (!this.tail) { |
| 36 | + this.tail = this.head = node; |
| 37 | + return; |
| 38 | + } |
| 39 | + this.tail.next = node; // Updates the next tail to the node created |
| 40 | + this.tail = node; // The tail of the Queue then becomes the node created!! |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Removes an item from the queue and returns it. |
| 47 | + * |
| 48 | + * @throws Queue Underflow if the queue is empty. |
| 49 | + * @returns The item that was removed from the queue. |
| 50 | + */ |
| 51 | + dequeue(): T | undefined { |
| 52 | + |
| 53 | + if (!this.head) { |
| 54 | + throw new Error("Queue Underflow"); |
| 55 | + } |
| 56 | + |
| 57 | + this.size--; |
| 58 | + let head = this.head; // We store the head in order not to lose track of it |
| 59 | + this.head = this.head.next; // Update the the head to the next node |
| 60 | + return head.value; // Return the value of the head |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the item at the front of the queue. |
| 66 | + * |
| 67 | + * @returns The item at the front of the queue or null if the queue is empty. |
| 68 | + */ |
| 69 | + peek(): T | undefined | null { |
| 70 | + |
| 71 | + if (this.isEmpty()) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + return this.head?.value; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Checks if the queue is empty. |
| 79 | + * |
| 80 | + * @returns {boolean} Whether the queue is empty or not. |
| 81 | + */ |
| 82 | + isEmpty(): boolean { |
| 83 | + return this.size === 0 |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns the number of items in the queue. |
| 88 | + * |
| 89 | + * @returns {number} The number of items in the queue. |
| 90 | + */ |
| 91 | + length(): number { |
| 92 | + return this.size; |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments