Data Structures · Visualization

Queue: Enqueue

Queue

Enqueueing adds a new item to the back of the queue, behind everyone already waiting. Time: O(1). Space: O(1) per operation.

Big-O: enqueue / dequeue are both O(1).

Queue: Enqueue

Queue

Enqueueing adds a new item to the back of the queue, behind everyone already waiting. The visualizer starts from the default queue [A, B, C] — A at the front — and enqueues the default value 7, producing [A, B, C, 7] while the front stays A. Nothing shifts: a linked-list queue links one new node, and a circular buffer just advances its back index modulo capacity, so enqueue is O(1) until the buffer is full, at which point a fixed-size implementation must reject the item or overwrite the oldest one.

How Enqueue Works

  1. Take the new value to add — the visualizer uses 7.
  2. Check capacity: a bounded circular queue reports 'full' instead of overwriting the front silently.
  3. Place it at the back, immediately after the current last item (C in the default queue).
  4. Update the 'back' reference to the new item, or advance it modulo capacity.
  5. Leave the front pointer alone — the order of everyone already waiting is unchanged.

Enqueue Worked Example

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

  1. Current queue, front on the left: [A, B, C] — A is next to be served.
  2. 7 joins at the back: [A, B, C, 7].
  3. The front is still A, so three dequeues are needed before 7 reaches the front.

Output: [A, B, C, 7] with A still at the front

Try your own input
Queue: EnqueueO(1)
Adds a new value to the back of the queue.
front
3
5
back
Active / highlighted Min stack Result
Queue: Enqueue
Press ▶ Run to watch it step by step.
Python · Stacks & Queues in Code
from collections import deque
q = deque([1])
q.append(2)   # enqueue at the back
print(q)      # deque([1, 2])
# example: qpush 7 on [3, 5]
Input: enqueue(7) 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.

Enqueue Time & Space Complexity

TimeO(1)
SpaceO(1) per operation

When to Use Enqueue

  • Adding a new customer to the back of a service line
  • Submitting a new print job to the printer queue
  • Adding a newly discovered node to the BFS frontier

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.