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













