Priority queue

A priority queue removes the item with the highest or lowest priority, rather than the item that arrived first. A binary heap is a common implementation.

The stack in Stack handles undo history. The help desk now adds a different rule: a matchmaking outage must be handled before routine reports even if it arrived later.

urgent_items = empty priority queue
add (priority: 1, value: "restart service") to urgent_items
next_item = remove the item with the highest priority

Adding and removing the next item usually costs O(log n), while inspecting the next priority is usually O(1). Use a priority queue for urgent incidents, scheduled tasks, and shortest-path algorithms.

Unlike a sorted list, a heap guarantees quick access to the next priority, not a completely sorted view of every item. Urgency and recency remain separate policies.

See Priority queue examples for runnable examples in supported programming languages.