Data Structures - Priority Queue
A priority queue is an extended form of a queue. Unlike a normal queue's "first in, first out", each element in a priority queue is assigned a priority, and the dequeue operation always removes the element with the highest priority currently in the queue.
Concept and Characteristics of Priority Queues
It can be likened to the triage system in a hospital emergency room - the more critical a patient's condition, the higher priority they get, even if they arrived later.
The core operations of a priority queue differ from those of a normal queue:
| Operation | Normal Queue | Priority queue |
|---|---|---|
| Enqueue | Add an element at the tail | Insert an element together with its priority |
| Dequeue | Return the front element (the earliest enqueued) | Return the element with the highest priority |
Comparison of Three Implementation Approaches
Priority Queue - Efficiency Comparison of Three Implementation Approaches
Unordered Array
insert
O(1)
Dequeue
O(n)
Space
Data only
Suitable for
Frequent insertion
Ordered Linked List
insert
O(n)
Dequeue
O(1)
Space
Data + pointers
Suitable for
Frequent dequeue
Heap (Heap) - recommended
insert
O(log n)
Dequeue
O(log n)
Space
Compact array
Suitable for
Frequent insertion and deletion
Heap implementation is optimal: it balances insertion and deletion efficiency and is the core dependency of algorithms such as Dijkstra's shortest path and Huffman coding.
The figure above compares the time-complexity differences among the three implementations; the heap implementation is the most balanced choice.
Implementation Based on Unordered Array
Example
#include
#define MAX 100
/* Priority queue based on unordered array (max-priority queue: larger value means higher priority) */
struct
PriorityQueue
{
int
items
[
MAX
]
;
int
size
;
/* current number of elements */
}
;
void
initPQ
(
struct
PriorityQueue
pq
)
{
pq
->
size
=
0
;
}
/* Enqueue: append directly to the end, O(1) */
void
enqueue
(
struct
PriorityQueue
pq
,
int
value
)
{
if
(
pq
->
size
=
MAX
)
return
;
pq
->
items
[
pq
->
size
++
]
=
value
;
}
/* Dequeue: scan to find the maximum value, O(n)
Then move the last element to the deleted position, and decrement size */
int
dequeue
(
struct
PriorityQueue
pq
)
{
if
(
pq
->
size
==
0
)
return
1
;
/* Find the index of the maximum value */
int
maxIdx
=
0
;
for
(
int
i
=
1
;
i
<
pq
->
size;
i
++
)
{
if
(
pq
->
items
[
i
]
pq
->
items
[
maxIdx
]
)
{
maxIdx
=
i
;
}
}
int
maxVal
=
pq
->
items
[
maxIdx
]
;
/* Move the last element to the deleted position (to avoid massive shifting) */
pq
->
items
[
maxIdx
]
=
pq
->
items
[
--
pq
->
size
]
;
return
maxVal
;
}
int
main
(
)
{
struct
PriorityQueue pq
;
initPQ
(
&
pq
)
;
enqueue
(
&
pq
,
5
)
;
enqueue
(
&
pq
,
9
)
;
enqueue
(
&
pq
,
3
)
;
enqueue
(
&
pq
,
7
)
;
printf
(
"Dequeue (highest priority): %d\n"
,
dequeue
(
&
pq
)
)
;
/* Output: 9 */
printf
(
"Dequeue (highest priority): %d\n"
,
dequeue
(
&
pq
)
)
;
/* Output: 7 */
printf
(
"Dequeue (highest priority): %d\n"
,
dequeue
(
&
pq
)
)
;
/* Output: 5 */
printf
(
"Dequeue: %d\n"
,
dequeue
(
&
pq
)
)
;
/* Output: 3 */
return
0
;
}
Implementation Based on Ordered Linked List
Example
#include
#include
struct
Node
{
int
data
;
int
priority
;
/* Priority (larger value means higher priority) */
struct
Node
next
;
}
;
/* Enqueue: insert in descending priority order, O(n) */
struct
Node
enqueue
(
struct
Node
head
,
int
data
,
int
priority
)
{
struct
Node
newNode
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
newNode
->
data
=
data
;
newNode
->
priority
=
priority
;
newNode
->
next
=
NULL
;
/* Empty list or new node has the highest priority -> insert at head */
if
(
head
==
NULL
||
head
->
priority
<
priority
)
{
newNode
->
next
=
head;
return
newNode
;
}
/* Traverse to find the proper position (maintain descending order) */
struct
Node
cur
=
head
;
while
(
cur
->
next
!=
NULL
&&
cur
->
next
->
priority
=
priority
)
{
cur
=
cur
->
next
;
}
newNode
->
next
=
cur
->
next
;
cur
->
next
=
newNode
;
return
head
;
}
/* Dequeue: remove the head node (highest priority), O(1) */
struct
Node
dequeue
(
struct
Node
head
)
{
if
(
head
==
NULL
)
return
NULL
;
struct
Node
temp
=
head
;
printf
(
"Dequeue: data=%d, priority=%d\n"
,
temp
->
data
,
temp
->
priority
)
;
head
=
head
->
next
;
free
(
temp
)
;
return
head
;
}
int
main
(
)
{
struct
Node
pq
=
NULL
;
pq
=
enqueue
(
pq
,
10
,
1
)
;
/* Data 10, priority 1 */
pq
=
enqueue
(
pq
,
20
,
5
)
;
/* Data 20, priority 5 */
pq
=
enqueue
(
pq
,
30
,
3
)
;
/* Data 30, priority 3 */
pq
=
dequeue
(
pq
)
;
/* Dequeue: data=20, priority=5 (highest priority) */
pq
=
dequeue
(
pq
)
;
/* Dequeue: data=30, priority=3 */
pq
=
dequeue
(
pq
)
;
/* Dequeue: data=10, priority=1 */
return
0
;
}
Implementation classes:It is the most commonly used and most balanced approach for implementing priority queues - both insertion and extracting the highest-priority element complete in O(log n) time. This topic will be covered in detail in Chapter 15 on heaps.
Application scenarios
| Scenario | Description |
|---|---|
| Task scheduling | The operating system decides CPU execution order based on task priority |
| Dijkstra's Shortest Path | Each time it selects the unvisited node with the smallest distance, relying on a priority queue |
| Huffman Coding | Repeatedly extract the two nodes with the lowest frequency and merge them to build the optimal coding tree |