Queue Data Structure Applications: Types, Operations and Examples
Queues serve items first in, first out. See circular, priority and double-ended queues, real applications like BFS and scheduling, and short Python code.
A queue is a linear data structure that removes items in the order they arrived: first in, first out (FIFO). Queue data structure applications include CPU scheduling, breadth-first search, printer spooling, keyboard and audio buffers, network routers and message queues. Circular, priority and double-ended queues adapt the same idea to fixed memory, urgency and two-sided access.
Key takeaways
- A queue removes the oldest item first, so it fits scheduling, BFS and buffering.
- Circular queues reuse fixed memory, priority queues serve the most urgent item and deques work at both ends.
- Use collections.deque in Python. list.pop(0) costs O(n) per removal.
- Enqueue and dequeue are O(1) for simple and circular queues and O(log n) for heap-based priority queues.
On this page:
- What is a queue and what does FIFO mean?
- Which queue types exist and when do you use each?
- Where are queues used in real systems?
- How do you implement a queue and what does it cost?
- Next step
- Frequently asked questions
| Queue type | Who leaves first | Typical use | Enqueue / dequeue |
|---|---|---|---|
| Simple (FIFO) queue | Oldest item | BFS, print jobs, ready queue | O(1) / O(1) with a linked list or deque |
| Circular queue | Oldest item, fixed-size storage | Keyboard, audio and stream buffers | O(1) / O(1) |
| Priority queue | Highest-priority item | Dijkstra, task schedulers | O(log n) / O(log n) with a binary heap |
| Deque | Either end | Sliding window, undo and redo | O(1) at both ends |
What is a queue and what does FIFO mean?
A queue holds items in arrival order. You add at the back (enqueue) and remove from the front (dequeue), so the item that has waited longest is served first.
Think of a railway ticket counter. The person at the front buys a ticket and leaves, and new people join at the back. Nobody jumps the line.
A queue needs only a few operations:
- enqueue(x): add x at the back.
- dequeue(): remove and return the front item.
- peek(): read the front item without removing it.
- isEmpty() and isFull(): check the state before acting. A bounded queue can be full.
Queues matter whenever a fast producer meets a slow consumer. The queue is the waiting room between them. For the basics of stacks and queues side by side, read our guide to stacks and queues.
Which queue types exist and when do you use each?
Pick the type by asking who should leave first and how much memory you can spend.
Circular queue
A plain array queue wastes space: after many dequeues the front moves right and the left slots stay empty. A circular queue wraps the indices with a modulo, so freed slots are reused. Illustrative trace with capacity 4:
enqueue A, B, C, D -> slots [A, B, C, D], head = 0, size = 4 (full)
dequeue() -> returns A, head = 1, size = 3
enqueue E -> E goes to index (1 + 3) % 4 = 0, size = 4
slots now [E, B, C, D], front item is BPriority queue
Here the most urgent item leaves first, not the oldest. Binary heaps are the usual implementation, which is why enqueue and dequeue cost O(log n). We cover the structure in detail in heap and priority queue explained.
Deque (double-ended queue)
A deque lets you add and remove at both ends. Use it when you need queue and stack behaviour together, such as a sliding-window maximum.
Where are queues used in real systems?
Queues appear wherever order or buffering matters. The pattern is the same each time: items wait, and the oldest (or most urgent) goes next.
CPU scheduling with Round Robin
The operating system keeps ready processes in a queue. Round Robin gives each process a fixed time slice, the quantum, and sends unfinished processes to the back. Worked example (illustrative): three processes arrive together with CPU needs P1 = 5, P2 = 3, P3 = 2 units, quantum = 2.
0-2 P1 runs (3 left) queue: P2, P3, P1
2-4 P2 runs (1 left) queue: P3, P1, P2
4-6 P3 runs (done at 6)
6-8 P1 runs (1 left) queue: P2, P1
8-9 P2 runs (done at 9)
9-10 P1 runs (done at 10)Breadth-first search
BFS explores a graph level by level, so it needs a FIFO queue. This is how you find the fewest steps between two nodes in an unweighted graph. See the code in the next section.
Buffers, spoolers and routers
- Print spooler: jobs wait for a slow printer in arrival order.
- Keyboard and audio buffers: a fixed-size circular buffer lets input run slightly ahead of processing.
- Network routers: packets wait in an output queue when the link is busy. Many routers use priority or weighted queues so that urgent traffic is not stuck behind bulk downloads.
Message queues
Services such as Amazon SQS and RabbitMQ place a queue between the service that produces work and the service that does it. If the worker is slow or down, messages wait instead of being lost. Check each product's documentation for ordering guarantees, because they differ.
How do you implement a queue and what does it cost?
In Python, use collections.deque for a FIFO queue. Do not use list.pop(0), which shifts every remaining element and costs O(n).
from collections import deque
def bfs_steps(graph, start, goal):
queue = deque([(start, 0)])
seen = {start}
while queue:
node, steps = queue.popleft()
if node == goal:
return steps
for nxt in graph[node]:
if nxt not in seen:
seen.add(nxt)
queue.append((nxt, steps + 1))
return -1
graph = {"A": ["B", "C"], "B": ["D"], "C": ["D"], "D": []}
print(bfs_steps(graph, "A", "D")) # 2A fixed-size circular queue is short too:
class CircularQueue:
def __init__(self, capacity):
self.data = [None] * capacity
self.head = 0
self.size = 0
def enqueue(self, x):
if self.size == len(self.data):
raise OverflowError("queue full")
self.data[(self.head + self.size) % len(self.data)] = x
self.size += 1
def dequeue(self):
if self.size == 0:
raise IndexError("queue empty")
x = self.data[self.head]
self.head = (self.head + 1) % len(self.data)
self.size -= 1
return xBoth run enqueue and dequeue in O(1). Space is O(n) for n stored items. The same ideas in JavaScript are in our queue implementation guide, and BFS uses the same queue on a tree in binary tree traversal.
Next step
Once queues, heaps and graphs feel natural, you can apply them to data and models. If you want a structured path from programming fundamentals into AI, look at the Artificial Intelligence and Machine Learning programme from Vishlesan i-Hub, IIT Patna and check the programme page for the current batch date, eligibility and fees.
Frequently asked questions
What are the main applications of a queue?
Queues are used in CPU scheduling, breadth-first search, printer spooling, keyboard and audio buffers, network routers, web waiting rooms and message queues such as Amazon SQS. In each case, items wait and the oldest or most urgent one is handled next.
Why is a circular queue better than a simple array queue?
A simple array queue leaves unused slots at the front after dequeues. A circular queue wraps indices with a modulo, so those slots are reused and the fixed memory is fully used. Enqueue and dequeue stay O(1).
Where is a priority queue used?
Priority queues drive Dijkstra's shortest-path algorithm, task schedulers and event simulations, where the most urgent item must leave first. A binary heap gives O(log n) insertion and removal.
Why is BFS implemented with a queue?
BFS must visit all nodes at distance one before any at distance two. A FIFO queue keeps nodes in the order they were discovered, which gives exactly this level-by-level order and the shortest path in unweighted graphs.
Why not use list.pop(0) as a queue in Python?
Removing from the front of a list shifts every other element, so each dequeue costs O(n). collections.deque removes from the left in O(1), which is why it is the standard choice for queues.
Are queues still relevant in modern software?
Yes. Message queues, job workers, rate limiters and stream processors all rely on queue behaviour. Understanding FIFO, buffering and backpressure also helps in system design interviews.