namastey-priority-queue
v1.0.1
Published
Priority Queue data structure implementation in JavaScript for efficient data handling and retrieval.
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. Returnsnullif the queue is empty.peek(): Returns the item with the highest priority without removing it from the queue. Returnsnullif the queue is empty.isEmpty(): Checks if the queue is empty. Returnstrueif empty,falseotherwise.size(): Returns the number of items in the queue.
Installation
To install the package globally, run:
npm install -g namastey-priority-queueExamples
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