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

Queue

Dequeueing 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

  1. Check that the queue is not empty — underflow must be an explicit error.
  2. Read the value at the front index (A in the default queue).
  3. Unlink the front node, or advance the front index past it.
  4. Update the 'front' reference to the next item in line, B.
  5. Return the removed value; capacity frees up for the next enqueue.

Dequeue Worked Example

Input: dequeue() on the default queue [A, B, C]

  1. Front item is A.
  2. Remove it — the queue becomes [B, C].
  3. New front is B; two more dequeues drain the queue, and a third raises underflow.

Output: Dequeued value: A, remaining queue: [B, C]

Try your own input
Queue: DequeueO(1)
Removes and returns the value at the front of the queue.
front
3
5
back
Active / highlighted Min stack Result
Queue: Dequeue
Press ▶ Run to watch it step by step.
Python · Stacks & Queues in Code
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]
Input: dequeue() on the default queue [A, B, C]

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

TimeO(1)
SpaceO(1) per operation

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.