namastey-priority-queue
v1.0.1
Published
Priority Queue data structure implementation in JavaScript for efficient data handling and retrieval.
Downloads
3
Maintainers
Readme
namastey-priority-queue
A package implementing a Priority Queue data structure in JavaScript.
Features
enqueue(value, priority)
: Adds a new item to the queue with a given priority. The queue is sorted so that the highest priority items are dequeued first.dequeue()
: Removes and returns the item with the highest priority. Returnsnull
if the queue is empty.peek()
: Returns the item with the highest priority without removing it from the queue. Returnsnull
if the queue is empty.isEmpty()
: Checks if the queue is empty. Returnstrue
if empty,false
otherwise.size()
: Returns the number of items in the queue.
Installation
To install the package globally, run:
npm install -g namastey-priority-queue
Examples
const PriorityQueue = require('namastey-priority-queue');
const pq = new PriorityQueue();
pq.enqueue('task1', 2);
pq.enqueue('task2', 1);
pq.enqueue('task3', 3);
console.log(pq.peek()); // Output: task3
console.log(pq.dequeue()); // Output: task3
console.log(pq.dequeue()); // Output: task1
console.log(pq.size()); // Output: 1
console.log(pq.isEmpty()); // Output: false