src/queue/queue.ts
Properties |
Methods |
Accessors |
store |
Type : T[]
|
Default value : []
|
Defined in src/queue/queue.ts:4
|
dequeue |
dequeue()
|
Defined in src/queue/queue.ts:18
|
To get the head element of the queue and remove the head
Returns :
any
T |
enqueue | ||||||
enqueue(item: T)
|
||||||
Defined in src/queue/queue.ts:10
|
||||||
To add item to the queue
Parameters :
Returns :
void
|
peek |
peek()
|
Defined in src/queue/queue.ts:29
|
Get head element of the queue
Returns :
T
T |
isEmpty |
getisEmpty()
|
Defined in src/queue/queue.ts:40
|
Get if the queue is empty or not |
size |
getsize()
|
Defined in src/queue/queue.ts:48
|
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;
}
}