Queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. This Queue use an array as its underlying container, providing a specific set of member functions to access its elements.
You can import the Queue as follows:
import { Queue } from "data-structures-typescript";
const Q = new Queue<Type>()
where Type
is the type of your queue.
The queue currently supports the methods below:
Method | Description | Usage |
---|---|---|
constructor |
creates an empty queue | here |
front |
retuns the element in the front of the queue or undefined if the Queue is empty |
here |
push |
insert a new element into the queue at the end | here |
pop |
pops the first element in the queue and throws an Exception if the Queue is empty |
here |
isEmpty |
returns true if the Queue is empty false otherwise |
here |
The constructor method will initialize the Queue to an empty array so when you call:
const Q = new Queue<number>()
this will create a Queue with the elements
array to an empty array []
;
The front
method returns the element at the front of the Queue
const Q = new Queue<number>()
Q.push(23);
console.log(Q.front()); // 23
Q.pop();
console.log(Q.front()); // undefined
const Q = new Queue<number>();
Q.push(1);
Q.pop();
console.log(Q.front()); // undefined
Q.pop() // The queue is empty there is nothing to pop.
const Q = new Queue<number>();
console.log(Q.isEmpty()) // true
Q.push(2);
console.log(Q.isEmpty()) // false