Data Structures · Visualization
Circular Queue: Dequeue
Circular Queue
Removing an element from a circular queue reads the value at front, then advances front modulo the capacity, freeing that slot for future wrap-around enqueues. Time: O(1). Space: O(1) extra.
Big-O: enqueue / dequeue both O(1) via wrap-around indices on a fixed array.
Circular Queue: Dequeue
Circular QueueRemoving an element from a circular queue reads the value at front, then advances front modulo the capacity, freeing that slot for future wrap-around enqueues.
How Dequeue Works
- Check if the queue is empty (size === 0); if so, return an error/undefined.
- Read the value stored at index front.
- Advance front: front = (front + 1) % capacity.
- Decrement size by 1.
Dequeue Worked Example
Input: Capacity 4, [60,20,30,40] front=1 rear=0 size=4, dequeue
- size(4) !== 0, so proceed
- value = array[1] = 20
- front = (1+1) % 4 = 2
- size becomes 3
Output: Returned 20; queue now conceptually holds 30,40,60
Press ▶ Run to watch it step by step.
def dequeue(q, head, size, cap):
if size == 0:
raise RuntimeError("empty")
v = q[head]
head = (head + 1) % cap
return v, head, size - 1
# head, size = 0, 2
# v, head, size = dequeue(q, head, size, 4)
# example: dequeue on [60, 20, 30, 40]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
- Consumer side of producer-consumer buffers
- Processing the oldest event in a ring buffer
- Round-robin CPU scheduling dispatch
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.