1.0.1 • Published 10 months ago

namastey-priority-queue v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

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. Returns null if the queue is empty.
  • peek(): Returns the item with the highest priority without removing it from the queue. Returns null if the queue is empty.
  • isEmpty(): Checks if the queue is empty. Returns true 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
1.0.1

10 months ago

1.0.0

11 months ago