liveBook · Manning

Stack

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A stack is a last-in-first-out data structure in which insertion and removal occur at the same end.

Imagine a tower of discs: only the top disc can move. A stack models that restriction. It follows last-in-first-out (LIFO) order: the last item pushed onto the stack is the first one popped off. `push` adds an item; `pop` removes and returns the top item. section 1.5

public class Stack<T> {
    private var container: [T] = [T]()
    public var isEmpty: Bool { return container.isEmpty }
    public func push(thing: T) { container.append(thing) }
    public func pop() -> T { return container.removeLast() }
}

A generic Swift stack uses an Array as its backing store. `push` appends to one end, `pop` removes from that same end, and `isEmpty` reports whether anything remains. section 2.2

The two ends must not get mixed up: appending and then calling `removeLast()` preserves LIFO order. Check `isEmpty` before popping when the stack might be exhausted; `removeLast()` fails on an empty Array. section 2.2

Move discs between towers

In the Towers of Hanoi, represent each tower as a `Stack<Int>`. Push the discs onto the first tower, then move a top disc by popping it from one tower and pushing it onto another. The puzzle also forbids placing a wider disc on a narrower one; the stack models which disc is available to move, not that rule by itself. section 1.5

A stack also serves as the frontier in depth-first search. Push a starting node, then repeatedly pop a node, test its state against the goal, and push unexplored successors. Because the most recently added node comes out next, the search follows a path until it must backtrack. A maze supplies the states and possible moves; parent links in the nodes allow a found path to be reconstructed. section 2.2

The frontier’s data structure changes the search order. Depth-first search uses a stack; breadth-first search uses a queue, which removes from the opposite end and searches locations closest to the start first. section 2.2

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines LIFO, implements a generic Array-backed stack, and addresses empty pops; stops short of enforcing Hanoi’s disc-size rule.
Examples
Uses Towers of Hanoi disc transfers and a maze-search frontier.
What it assumes
Assumes familiarity with Swift types and Arrays while defining LIFO and stack operations from the ground up.
What you can do after
Build it from scratch as a generic Swift Array-backed stack and apply it to Hanoi moves or a depth-first frontier.
How it connects
Connects stacks to Swift Arrays, recursion, depth-first search, and queues used in breadth-first search.

Also explained in

Compare how 14 books explain stack →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.