Data Structures · Visualization
Queue: Dequeue
Queue
Dequeueing removes and returns the item at the front — the one that has waited longest. Time: O(1). Space: O(1) per operation.
Big-O: enqueue / dequeue are both O(1).
Queue: Dequeue
QueueDequeueing removes and returns the item at the front — the one that has waited longest. On the visualizer's default queue [A, B, C] the first dequeue removes A and leaves [B, C] with B at the front. With a linked list the front node is unlinked, and with a circular buffer the front index advances modulo capacity; either way no element moves, so the operation is O(1). The empty case is the one to guard: dequeue on an empty queue is an underflow error rather than a valid removal.
How Dequeue Works
- Check that the queue is not empty — underflow must be an explicit error.
- Read the value at the front index (A in the default queue).
- Unlink the front node, or advance the front index past it.
- Update the 'front' reference to the next item in line, B.
- Return the removed value; capacity frees up for the next enqueue.
Dequeue Worked Example
Input: dequeue() on the default queue [A, B, C]
- Front item is A.
- Remove it — the queue becomes [B, C].
- New front is B; two more dequeues drain the queue, and a third raises underflow.
Output: Dequeued value: A, remaining queue: [B, C]
Press ▶ Run to watch it step by step.
from collections import deque
q = deque([1, 2])
front = q.popleft() # dequeue: removes 1
print(front, q) # 1 deque([2])
# example: qpop on [3, 5]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Dequeue Time & Space Complexity
When to Use Dequeue
- Serving the next customer in line
- Processing the next task in a job scheduler
- Visiting the next node in a BFS traversal
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.