A first-in-last-out structure follows the LIFO rule: the most recently inserted item is the first item removed. Its basic operations are push, which adds an item, and pop, which removes and returns the newest item. section 1.5 section 2.2
public class Stack<T>: CustomStringConvertible {
private var container: [T] = [T]()
public func push(_ thing: T) { container.append(thing) }
public func pop() -> T { return container.removeLast() }
public var description: String { return container.description }
}
Implements a generic stack with Swift’s Array as backing storage: append for push and removeLast for pop. section 1.5
With an Array backing store, push appends at the right end and pop removes from that same end. An empty pop fails because Array.removeLast() has no item to remove; an isEmpty property supports safe traversal logic. section 2.2
Towers of Hanoi
Model each tower as a stack: push a disc onto a tower, then pop its top disc and push it onto another tower. The top-only rule naturally enforces legal moves, while recursion coordinates the transfers. section 1.5
Depth-first search
A DFS stack stores unexplored states. Popping the newest state drives the search deeper; when a path reaches a barrier, the stack exposes the latest decision point for backtracking. section 2.2
