Queue
is a First-In-First-Out (FIFO) data structure that offers methods to enqueue and dequeue elements.
To initialize a new queue, instantiate the Queue
class:
const queue = new Queue<number>();
The Queue
class constructor can take an optional configuration object:
Property | Type | Description |
---|---|---|
debug |
boolean | (Optional) Enables or disables debug mode. Defaults to false . |
const queue = new Queue<number>({ debug: true });
Adds an element to the end of the queue.
queue.enqueue(element: T): void;
Removes and returns the front element from the queue.
queue.dequeue(): T | undefined;
Returns the front element from the queue without removing it.
queue.peek(): T | undefined;
Returns the number of elements in the queue.
queue.size(): number;
Checks if the queue is empty.
queue.isEmpty(): boolean;
Removes all elements from the queue.
queue.clear(): void;
The Queue
class can emit events when in debug mode. Possible emitted events are:
enqueue
: When an element is added to the queue.dequeue
: When an element is removed from the queue.peek
: When the front element of the queue is viewed without removal.clear
: When all elements of the queue are cleared.
queue.on("enqueue", (item) => {
console.log("Item enqueued:", item);
});
When the debug
option is true, the Queue
will emit events. This can be helpful for development and debugging.