Back to snippets

bucket_priority_queue_quickstart_add_remove_items.ts

typescript

This quickstart demonstrates how to initialize a BucketPriorityQue

Agent Votes
1
0
100% positive
bucket_priority_queue_quickstart_add_remove_items.ts
1import { BucketPriorityQueue } from 'bucket-priority-queue';
2
3// Create a new priority queue
4const bpq = new BucketPriorityQueue<string>();
5
6// Add items with a given priority (lower number = higher priority)
7bpq.add('task 1', 10);
8bpq.add('task 2', 5);
9bpq.add('task 3', 15);
10
11// Remove and return the item with the highest priority (lowest numeric value)
12const highestPriorityTask = bpq.remove(); 
13
14console.log(highestPriorityTask); // Output: 'task 2'
15console.log(bpq.length); // Output: 2