core Estimated learning time: 8 h

P-4.3 Stacks and Queues

You can implement both structures over an array and over linked storage, and recognise the problems whose natural shape is last-in-first-out or first-in-first-out.

These are the two most restricted containers there are, and the restriction is the point: allowing only one place to add and one to remove makes the implementation tiny and the reasoning easy. They are worth knowing less as data structures than as a way of recognising a shape, because function calls, undo history, breadth-first search and job scheduling are all one or the other underneath.

Work through these

  • Last in, first out, and what that is good for

    A stack lets you add and remove only at the same end, which matches anything that nests: function calls, bracket matching, undo history, and backtracking searches.

    Algorithms, 4th Edition — companion site · Reference
  • First in, first out, and what that is good for

    A queue adds at one end and removes at the other, which matches anything served in arrival order. Breadth-first search and every job scheduler are built on it.

  • Implementing both over an array and over linked nodes

    Both structures can sit on either foundation, and the choice changes only the constants. Writing each one twice makes clear that the interface and the implementation are separate decisions.

  • The circular buffer, and why a queue over an array needs one

    Removing from the front of an array would shift everything, so the front and back positions are allowed to wrap around instead. This is the standard fixed-capacity queue and it appears everywhere in systems code.

  • The call stack is a stack, and recursion depth is its limit

    Every function call pushes a frame that is removed on return, which is why deep recursion runs out of room. Seeing this connection makes the earlier memory material and this module fit together.

Sign in to keep your progress.

Free resources

Links last checked 31 Aug 2026.

Stuck here?

Ask a mentor. A real person answers, and they can see exactly which topic you're on. Usually within a couple of working days.

Checking your session…

Topics shown in module order.