File

src/queue/queue.ts

Index

Properties
Methods
Accessors

Properties

store
Type : T[]
Default value : []

Methods

dequeue
dequeue()

To get the head element of the queue and remove the head

Returns : any

T

enqueue
enqueue(item: T)

To add item to the queue

Parameters :
Name Type Optional
item T No
Returns : void
peek
peek()

Get head element of the queue

Returns : T

T

Accessors

isEmpty
getisEmpty()

Get if the queue is empty or not

size
getsize()

Get the size of queue

import { Errors } from '../errors.enum';

export default class Queue<T> {
    store: T[] = [];

    /**
     * To add item to the queue
     * @param item
     */
    enqueue(item: T) {
        this.store.push(item);
    }

    /**
     * To get the head element of the queue and remove the head
     * @returns T
     */
    dequeue() {
        if (this.isEmpty) {
            throw new Error(Errors.QUEUE_EMPTY);
        }
        return this.store.shift();
    }

    /**
     * Get head element of the queue
     * @returns T
     */
    peek(): T {
        if (this.isEmpty) {
            throw new Error(Errors.QUEUE_EMPTY);
        }
        return this.store[0];
    }

    /**
     * Get if the queue is empty or not
     * @returns bool
     */
    get isEmpty() {
        return this.size === 0;
    }

    /**
     * Get the size of queue
     * @returns length
     */
    get size() {
        return this.store.length;
    }
}

results matching ""

    No results matching ""