QueueEasy

Queue Foundation Visualizer

What is Queue Foundation

A queue serves the value that has been waiting the longest. New values join at the rear. The front is the next one to leave.

Approach

The picture enqueues the values, then dequeues the front. The front stays on the left.

Visualization & algorithm

Enqueue, then dequeue the front
queue = []
enqueue at the rear
front is the first value
dequeue removes the front
Step 0/0 Answer —

The finished picture is on screen when the page opens. Next and Play start from the first step. The controls sit under the picture and the pseudocode.

Complexity

PieceCostWhy
Enqueue or dequeueO(1)With a real deque, either end is constant time.
SpaceO(n)Every waiting value is stored.

Implementation

from collections import deque
q = deque()
q.append(1)      # enqueue
q.append(2)
front = q[0]     # front
q.popleft()      # dequeue