Data Structures - Queue
A queue is a linear data structure that follows the First In First Out (FIFO) principle. It can be likened to queuing for tickets in real life - the person who queues first gets served first.
Concept and Principle of Queues
Queue - FIFO Principle Illustration
Front
front
→
20
30
Rear
rear
<- dequeue
enqueue ->
enqueue: insert at rear | dequeue: remove from front | complexity O(1)
FIFO verification: enqueue 10->20->30, dequeue 10->20->30 (first in, first out)
A queue only allows insertion at one end (the rear) and deletion at the other end (the front).
This is exactly the opposite of the LIFO principle of a stack.
Array Implementation of a Queue
Example
#include
#include
#define MAX 100
struct
Queue
{
int
items
[
MAX
]
;
int
front
;
/* Front pointer: points to the first element */
int
rear
;
/* Rear pointer: points to the next position to insert */
}
;
void
initQueue
(
struct
Queue
q
)
{
q
->
front
=
0
;
q
->
rear
=
0
;
}
bool isEmpty
(
struct
Queue
q
)
{
return
q
->
front
==
q
->
rear
;
}
bool isFull
(
struct
Queue
q
)
{
return
q
->
rear
==
MAX
;
}
/* Enqueue: add an element at the rear */
void
enqueue
(
struct
Queue
q
,
int
value
)
{
if
(
isFull
(
q
)
)
{
printf
(
"Queue is full!\n"
)
;
return
;
}
q
->
items
[
q
->
rear
++
]
=
value
;
/* Write at rear, then advance rear */
printf
(
"Enqueue: %d\n"
,
value
)
;
}
/* Dequeue: remove an element from the front */
int
dequeue
(
struct
Queue
q
)
{
if
(
isEmpty
(
q
)
)
{
printf
(
"Queue is empty!\n"
)
;
return
1
;
}
return
q
->
items
[
q
->
front
++
]
;
/* Return the element at front, then advance front */
}
int
main
(
)
{
struct
Queue q
;
initQueue
(
&
q
)
;
enqueue
(
&
q
,
10
)
;
/* Enqueue: 10 */
enqueue
(
&
q
,
20
)
;
/* Enqueue: 20 */
enqueue
(
&
q
,
30
)
;
/* Enqueue: 30 */
printf
(
"Dequeue: %d\n"
,
dequeue
(
&
q
)
)
;
/* Output: Dequeue: 10 (first in, first out) */
printf
(
"Dequeue: %d\n"
,
dequeue
(
&
q
)
)
;
/* Output: Dequeue: 20 */
printf
(
"Dequeue: %d\n"
,
dequeue
(
&
q
)
)
;
/* Output: Dequeue: 30 */
return
0
;
}
Queues implemented with a normal array suffer from the "false overflow" problem: as enqueue and dequeue keep happening, front and rear keep moving toward the end of the array, and even if there is a lot of free space at the front, it cannot be reused. The solution is to usea circular queue.
a circular queue
Circular Queue - Pointer Wrap-around via Modulo Operation
Circular Queue Key Formulas
Enqueue: items[rear] = value; rear = (rear + 1) % MAX;
Dequeue: value = items[front]; front = (front + 1) % MAX;
Empty: front == rear | Full: (rear + 1) % MAX == front
Run example (capacity=5, holds at most 4): enqueue 10->20->30->40, dequeue 10->20, enqueue 50->60 -> 60 stored at index 0 (wrap-around), perfectly reusing freed space
A circular queue logically treats the array as a ring structure with head and tail connected, achieving pointer wrap-around through the modulo operation.
Example
#include
#include
#define MAX 5 /* Intentionally set a small capacity to make the circular behavior easy to observe */
struct
CircularQueue
{
int
items
[
MAX
]
;
int
front
;
/* Front index */
int
rear
;
/* Rear index (next insertion position) */
}
;
void
initQueue
(
struct
CircularQueue
q
)
{
q
->
front
=
0
;
q
->
rear
=
0
;
}
/* Empty check: front and rear coincide */
bool isEmpty
(
struct
CircularQueue
q
)
{
return
q
->
front
==
q
->
rear
;
}
/* Full check: (rear + 1) % MAX == front
Note: a circular queue deliberately leaves one slot empty to distinguish empty from full */
bool isFull
(
struct
CircularQueue
q
)
{
return
(
q
->
rear
1
)
%
MAX
==
q
->
front
;
}
/* Enqueue: write at rear, then advance rear cyclically */
void
enqueue
(
struct
CircularQueue
q
,
int
value
)
{
if
(
isFull
(
q
)
)
{
printf
(
"Circular queue is full!\n"
)
;
return
;
}
q
->
items
[
q
->
rear
]
=
value
;
q
->
rear
=
(
q
->
rear
1
)
%
MAX
;
/* Modulo achieves wrap-around */
printf
(
"Enqueue: %d (rear=%d)\n"
,
value
,
q
->
rear
)
;
}
/* Dequeue: return the element at front, then advance front cyclically */
int
dequeue
(
struct
CircularQueue
q
)
{
if
(
isEmpty
(
q
)
)
{
printf
(
"Circular queue is empty!\n"
)
;
return
1
;
}
int
value
=
q
->
items
[
q
->
front
]
;
q
->
front
=
(
q
->
front
1
)
%
MAX
;
/* Modulo achieves wrap-around */
return
value
;
}
int
main
(
)
{
struct
CircularQueue q
;
initQueue
(
&
q
)
;
enqueue
(
&
q
,
10
)
;
/* Enqueue: 10 (rear=1) */
enqueue
(
&
q
,
20
)
;
/* Enqueue: 20 (rear=2) */
enqueue
(
&
q
,
30
)
;
/* Enqueue: 30 (rear=3) */
enqueue
(
&
q
,
40
)
;
/* Enqueue: 40 (rear=4) - capacity 5, holds at most 4 */
printf
(
"Dequeue: %d\n"
,
dequeue
(
&
q
)
)
;
/* Dequeue: 10 (front=1) */
printf
(
"Dequeue: %d\n"
,
dequeue
(
&
q
)
)
;
/* Dequeue: 20 (front=2) */
enqueue
(
&
q
,
50
)
;
/* Enqueue: 50 (rear=0 - wrapped to the beginning!) */
enqueue
(
&
q
,
60
)
;
/* Enqueue: 60 (rear=1) */
printf
(
"Remaining elements: "
)
;
while
(
!
isEmpty
(
&
q
)
)
{
printf
(
"%d "
,
dequeue
(
&
q
)
)
;
}
printf
(
"\n"
)
;
/* Output: Remaining elements: 30 40 50 60 */
return
0
;
}
Application Scenarios of Queues
| Application scenarios | Description |
|---|---|
| Breadth-First Search (BFS) | Traverse graph nodes level by level; a queue is needed to store the "to-be-visited" nodes |
| Operating System Task Scheduling | The CPU allocates time slices to tasks in the order they arrive |
| Print Job Queue | Multiple computers share one printer; print requests are queued and processed in submission order |
| Message queue | In distributed systems, messages produced by producers are passed to consumers through a queue |