Skip to content

Latest commit

 

History

History
107 lines (71 loc) · 2.11 KB

queue.md

File metadata and controls

107 lines (71 loc) · 2.11 KB

Queue Documentation

Queue is a First-In-First-Out (FIFO) data structure that offers methods to enqueue and dequeue elements.

Table of Contents

Initialization

To initialize a new queue, instantiate the Queue class:

const queue = new Queue<number>();

Options

The Queue class constructor can take an optional configuration object:

QueueOptions

Property Type Description
debug boolean (Optional) Enables or disables debug mode. Defaults to false.
const queue = new Queue<number>({ debug: true });

Methods

enqueue

Adds an element to the end of the queue.

queue.enqueue(element: T): void;

dequeue

Removes and returns the front element from the queue.

queue.dequeue(): T | undefined;

peek

Returns the front element from the queue without removing it.

queue.peek(): T | undefined;

size

Returns the number of elements in the queue.

queue.size(): number;

isEmpty

Checks if the queue is empty.

queue.isEmpty(): boolean;

clear

Removes all elements from the queue.

queue.clear(): void;

Events

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);
});

Debug Mode

When the debug option is true, the Queue will emit events. This can be helpful for development and debugging.