Implementation of commonly used queue's in TypeScript
A Priority queue implemented via a Binary heap. Generic implementation where T
is the type of the items and U
is the type of the priorities. The type U
must be comparable to itself (i.e. number
, string
...)
Constructor parameters:
mapper
: function that will return the priority mapping of typeU
for a given element of typeT
. Defaults to the identity function.items
(rest parameter): the items that should be initally put into the queue. Defaults to an empty array.
Usage with defaults:
const queue = new PriorityQueue();
const small = 1;
const middle = 5;
const big = 9;
queue.enqueue(middle);
queue.enqueue(big);
queue.enqueue(small);
console.log(queue.dequeue()); // 1
console.log(queue.dequeue()); // 5
console.log(queue.dequeue()); // 9
console.log(queue.dequeue()); // Underflow error
Usage with set parameters:
interface WithValue {value: number;}
const withSmallUnits = { value: 101 };
const withMiddleUnits = { value: 95 };
const withBigUnits = { value: 909 };
const mapping = ({value}:WithValue) => value % 10; // return the digit in the units place
const queue = new PriorityQueue(mapping, withMiddleUnits, withBigUnits, withSmallUnits);
console.log(queue.dequeue()); // {value: 101}
console.log(queue.dequeue()); // {value: 95}
console.log(queue.dequeue()); // {value: 909}
A simple FIFO Queue, implemented as a simple abstraction over the built-in array type. Generic implementation where T
is the type of the items.
Constructor parameters:
items
(rest parameter): the items that should be initally put into the queue. Defaults to an empty array.
Usage with defaults:
const small = 1;
const middle = 5;
const big = 9;
const queue = new Queue(middle, big);
queue.enqueue(small);
console.log(queue.dequeue()); // 5
console.log(queue.dequeue()); // 9
console.log(queue.dequeue()); // 1
console.log(queue.dequeue()); // Underflow error
A simple LIFO Stack, implemented as a simple abstraction over the built-in array type. Generic implementation where T
is the type of the items.
Constructor parameters:
items
(rest parameter): the items that should be initally put into the queue. Defaults to an empty array.
Usage with defaults:
const small = 1;
const middle = 5;
const big = 9;
const stack = new Stack(middle, big);
stack.enqueue(small);
console.log(stack.pop()); // 1
console.log(stack.pop()); // 9
console.log(stack.pop()); // 5
console.log(stack.pop()); // Underflow error