JavaScript implementation of Priority Queue
Min Heap or Max Heap
Install
yarn add javascript-priority-queue
or npm install javascript-priority-queue
Usage
// Include using "import";const minQueue = 'min'; // Lowest priority firstconst maxQueue = 'max'; // Highest priority first // Include using "require"const PriorityQueue = ;const minQueue = 'min'; // Lowest priority firstconst maxQueue = 'max'; // Highest priority first // Functionsqueue; // add to the queue. e.g. queue.enqueue("a", 1)queue; // returns the current first item and remove it from the queue.queue; // returns the current first itemqueuesize; // returns the number of items in the queuequeue; // NOT IN SORTED ORDER! prints the all items
Example
const PriorityQueue = ; const queue = 'min'; queue;queue;queue;queue;queue; console;/* output: (NOT IN SORTED ORDER)Value: c Priority: -0.156Value: a Priority: 2Value: b Priority: 1Value: d Priority: 351680468468489Value: e Priority: 50*/ console;// output: c console;// output: b console;// output: 4