CircularQueue

Container of elements in which operations are performed based on FIFO principle and the last position is connected back to the first position to make a circle.

new CircularQueue()

/**
 * Creates an instance of CircularQueue.
 *
 * @param elements List of elements to create the new queue with.
 */
constructor(elements: T[] = [])

length

length: number

Number of elements in the queue. This field is read only.

Examples:

import { CircularQueue } from 'ads-js/queues';

const queue = new CircularQueue();

queue.length === 0; // true
queue.enqueue(1);
queue.length === 1; // true

clear()

/**
 * Clears the queue.
 */
clear()

dequeue()

/**
 * Removes the first element from the front of the queue and returns it.
 * Throws an error if the queue is empty.
 *
 * @returns Removed element.
 */
dequeue(): T;

enqueue()

/**
 * Adds element at the rear of the queue.
 *
 * @param element Element to add.
 */
enqueue(element: T): void;

getFirst()

/**
 * Gets element from the front of the queue without its removal.
 *
 * @returns Queue element.
 */
getFirst(): T;

isEmpty()

isEmpty(): boolean

Running time O(1)

Checks whether the queue is empty or not.

Returns:

TRUE if the queue is empty, FALSE otherwise.

Examples:

import { CircularQueue } from 'ads-js/queues';

const queue = new CircularQueue();

queue.isEmpty(); // true
queue.enqueue(1);
queue.isEmpty(); // false

rotate()

/**
 * Rotates the queue's rear towards its front by the specified number of steps.
 *
 * @param steps Number of steps.
 */
rotate(steps: number = 1): void;

Last updated

Was this helpful?