Understand Enqueue and Dequeue operations. Visualize First-In-First-Out (FIFO) logic in real-time.
About this algorithm
A queue is a list you may only add to at one end, the rear, and only remove from at the other end, the front. Whatever went in first comes out first, which is why it is called FIFO: first in, first out. It is how a line at a shop works, and it is how computers hold work that has to be done in the order it arrived.
Use a queue whenever items must be handled in arrival order: print jobs, requests waiting for a web server, messages between two parts of a program, keystrokes waiting to be read. Breadth-first search runs on a queue, because it must finish every node one step away before any node two steps away. A stack is the opposite tool: it hands back the newest item first, which suits undo history, matching brackets and depth-first search. A deque lets you add and remove at both ends, and a priority queue hands back the most urgent item rather than the oldest.
Put the new value behind the current last one. The rear pointer moves to it. Nothing at the front changes.
First check whether the queue is empty; if it is, there is nothing to return (this page returns null). Otherwise take the front value out, move the front pointer to the value that was second in line, and return what you took.
Check for an empty queue the same way, then return the front value and leave the queue as it was. Use it when you need to decide what to do with the next item before committing to taking it.
Most queue types also report how many items they hold and whether that number is zero. Always check before a dequeue or a peek, since asking an empty queue for its front is the most common queue bug.
The page opens with 10 at the front and 30 at the rear. Press Play with the box empty and it runs a dequeue: the queue is not empty, so 10 comes off the front and is returned, leaving 20, 30. Type 40 and press Enqueue: 40 joins behind 30, giving 20, 30, 40. Peek returns 20 and changes nothing. Three more dequeues return 20, then 30, then 40, the same order they went in, even though 40 arrived long after the others. One more dequeue finds the queue empty and returns null.
The code panel uses a JavaScript array, adding with push and removing with shift. That is short and fine for a demo, but shift moves every remaining element one slot to the left, so on a big queue each dequeue costs O(n). Real queues avoid that in one of three ways: a linked list with head and tail pointers, a circular buffer where front and rear are indexes that wrap around a fixed array, or an array with a moving head index that is compacted now and then. Python's collections.deque and Java's ArrayDeque do this for you.
With a fixed array of size k, keep front and count. Enqueue writes to (front + count) mod k and adds one to count; dequeue reads at front, sets front to (front + 1) mod k and subtracts one. Both ends move forward and wrap, so no value is ever shifted.
Time: with a linked list or a circular buffer, each operation changes one pointer or index at one end and never touches the other items; the array shift in the code panel is the exception and costs O(n). Space: the queue stores each of its n items once, plus the front and rear markers.