Skip to content
On this page

PriorityQueue<T>

A priority queue is an abstract data-type similar to a regular queue or stack data structure in which each element additionally has a priority associated with it. In a priority queue, an element with high priority is served before an element with low priority. In this implementation if two elements have the same priority, ordering of elements with the same priority remains undefined.

The operation of adding an element to the rear of the queue is known as enqueue, and the operation of removing an element from the front is known as dequeue.

Usage

typescript
import { PriorityQueue } from  '@algoasaurujs/tsds';

// instantiate new PriorityQueue

const queue = new PriorityQueue();

Constructors

OverloadDescription
new PriorityQueue(initialValue: T[], comparator: CompareFn<T>): PriorityQueue<T>Creates a PriorityQueue collection.

Properties

NameDescription
lengthGets the number of elements contained in the PriorityQueue<T>.

Methods

NameDescription
clear(): voidRemoves all objects from the PriorityQueue<T>.
dequeue(): TRemoves and returns the object at the beginning of the PriorityQueue<T>.
enqueue(value: T): voidAdds an object to the end of the PriorityQueue<T>.
includes(o: T): booleanThis implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element.
isEmpty(): booleanThis implementation returns length === 0.
iterator(): IterableIterator<T>
peek(): TReturns the object at the beginning of the PriorityQueue<T> without removing it.
toArray(): T[]This implementation returns an array containing all the elements returned by this collection's iterator, in the same order, stored in consecutive elements of the array, starting with index 0. The length of the returned array is equal to the number of elements returned by the iterator, even if the size of this collection changes during iteration, as might happen if the collection permits concurrent modification during iteration. The length property is called only as an optimization hint; the correct result is returned even if the iterator returns a different number of elements.