๐Ÿšƒ Train Shunting Stack Lab

Four wagons. One dead-end siding. Can you build the right departure order using only push and pop?

1. Plan your departure

This is a stack: the last wagon in is the first wagon out (LIFO). All arrivals must go into the siding before departure. You cannot skip an arrival, remove a buried wagon, or send a wagon back to the arrival line. The top is the open end of the siding.

A fictional railway model for learning data structures, not real shunting instructions. Each push or successful dispatch counts as one move. Wrong dispatches are blocked without moving anything.

Arrivals โ†’

Next arrival is first in this list.

    Siding โ†”

    Top / exit is first in this list.

      Departures โ†’

      First dispatched is first in this list.

        2. Think like a programmer

        Push adds an item to the top. Pop removes the top item. Peek looks at it without removing it. A stack can help model undo history: the latest saved change is the first one you undo. Try completing a mission, then undo its final move.

        A queue is different: first in, first out (FIFO), like a fair line for a ticket counter. With arrivals A, B, C, D, a single ordinary queue dispatches A, B, C, D. A stack can also keep that order if you push and pop each wagon before pushing the next.

        See the stack operations (pseudocode)
        push(wagon): add wagon to the top
        pop(): if not empty, remove and return the top
        peek(): if not empty, read the top without removing it

        The target check and blocked wrong moves are training aids in this game, not part of a general-purpose stack.

        Predict before clicking: which mission needs all four wagons in the siding at once? Which one only needs room for one? Restart and compare.

        3. Can every order be made?

        Arrivals are still A, B, C, D. Push A, then B, then C. Dispatch C. You want A next, to make C โ†’ A โ†’ B โ†’ D.

        Explain why pushing D would not help you reach A next. Could a second siding change what is possible?

        Keep exploring

        Sort wagons using adjacent swaps ยท Search sorted platform numbers ยท All courses

        No account, saved scores or input uploads. Your moves stay in this page; reloading resets the yard. The lab itself does not use cookies or browser storage.