Queue Simulator — Data Structures

Simulate a queue: 12 levels of enqueue, dequeue, circular and priority queues. A visual queue data structure game. 12 levels, right in the browser.

What you learn in this game

A queue keeps values in order. enqueue(x) puts x at the back (the tail), dequeue() takes the one at the front (the head) and returns it. The first in is the first out: FIFO. Bu Romlah's bench is a ring buffer: a fixed row of chairs, head pointing at the chair called next, and tail at the chair the next card sits in. A call moves nobody: only head moves on.

JavaScript has no built-in queue class. The usual stand-in is an array: push at the back, shift at the front. The result is right (FIFO), but the spec's shift moves every remaining element down one index. A ring queue keeps a head and moves nobody.

A ring has a fixed capacity. When it is full, head and tail point at the same chair, just as when it is empty: that is why a queue keeps its size, not only a head and a tail. The bench on this level refuses when full.

A bench that grows never refuses. When it is full, its people are copied to a bench twice as long, in order from the head, not in chair-number order: whoever is at the head moves to chair 0. Copying is expensive once, but it happens less and less often.

BFS explores a graph with a queue. Put in the start; then while the queue is not empty, take the front one (dequeue) and put in (enqueue) every neighbour of it not marked yet, marking it at once. In Gang Cempaka, houses are the nodes, gangs are the edges, and the news is the search.

When is a node marked? In BFS, when it is put in the queue. Between joining and its turn, another neighbour can find it again: the mark is what keeps it from joining twice.