max-priority-queue-typed
v2.6.0
Published
Max Priority Queue
Maintainers
Keywords
Readme
What
Brief
This is a standalone Max Priority Queue data structure from the data-structure-typed collection. If you wish to access more data structures or advanced features, you can transition to directly installing the complete data-structure-typed package
How
install
npm
npm i max-priority-queue --saveyarn
yarn add max-priority-queuesnippet
Job scheduling by priority
const jobs = new MaxPriorityQueue<number>();
jobs.add(3); // low priority
jobs.add(7); // high priority
jobs.add(5); // medium priority
jobs.add(10); // critical
// Highest priority job first
console.log(jobs.poll()); // 10;
console.log(jobs.poll()); // 7;
console.log(jobs.poll()); // 5;
console.log(jobs.poll()); // 3;Auction system with highest bid tracking
interface Bid {
bidder: string;
amount: number;
}
const auction = new MaxPriorityQueue<Bid>([], {
comparator: (a, b) => b.amount - a.amount
});
auction.add({ bidder: 'Alice', amount: 100 });
auction.add({ bidder: 'Bob', amount: 250 });
auction.add({ bidder: 'Charlie', amount: 175 });
// Current highest bid
console.log(auction.peek()?.bidder); // 'Bob';
console.log(auction.peek()?.amount); // 250;
// Process winning bid
const winner = auction.poll()!;
console.log(winner.bidder); // 'Bob';
console.log(auction.peek()?.bidder); // 'Charlie';CPU process scheduling
const cpuQueue = new MaxPriorityQueue<[number, string]>([], {
comparator: (a, b) => b[0] - a[0]
});
cpuQueue.add([5, 'System process']);
cpuQueue.add([1, 'Background task']);
cpuQueue.add([8, 'User interaction']);
cpuQueue.add([3, 'Network sync']);
const order = [];
while (cpuQueue.size > 0) {
order.push(cpuQueue.poll()![1]);
}
console.log(order); // [
// 'User interaction',
// 'System process',
// 'Network sync',
// 'Background task'
// ];API docs & Examples
Examples Repository
