Skip to content
Swift Algorithms 5th edition

Data Structures The climb Chapter 10

Stacks & Queues

In Chapter 9, we built linked lists—collections where elements connect through pointers rather than contiguous memory. Stacks and queues extend this concept by adding ordering rules: stacks process elements last-in, first-out (LIFO), while queues process elements first-in, first-out (FIFO). Stacks achieve constant time O(1) insertion and removal at a single end. Queues add at the back and remove from the front, and by keeping a reference to the back node, they achieve O(1) for both operations as well.

Real-world applications

Stacks reverse order—the last item added is first removed. This makes them perfect for undo/redo systems (track history, pop to undo), navigation (push views, pop to go back), and function call tracking (debugger shows call stack). iOS uses stacks extensively: NavigationStack maintains a view stack, UndoManager keeps undo and redo stacks so the most recent change is reversed first, and sheet presentations form a stack.

Queues preserve order—the first item added is first removed. This ensures fairness in task scheduling (process requests in order received), breadth-first graph traversal (Chapter 13 uses queues to explore graphs level-by-level), and buffering (network requests, print jobs, video frames). A serial DispatchQueue is a familiar example: it runs submitted tasks one at a time, in the order they were added.

Use Stack When Use Queue When
Need to reverse order Need to preserve order
Undo/redo functionality Task scheduling
Navigation history (back button) Workout queue (today’s planned sessions)
Backtracking algorithms Breadth-first search
Exercise history (most recent first) Fair resource allocation

The node structure

Both stacks and queues use a simple singly-linked node structure. Unlike the doubly-linked LLNode<T> from Chapter 9 (which needs previous pointers for bidirectional traversal), stacks and queues only traverse in one direction:

// Simple singly-linked node for stacks and queues
public class Node<T> {
    var tvalue: T?  // 'tvalue' means 'typed value' (matches production)
    var next: Node?

    init(tvalue: T? = nil) {
        self.tvalue = tvalue
    }
}

This minimalist structure is sufficient because stacks only access the top element and queues only access the front element. The simpler design reduces memory overhead compared to doubly-linked nodes.

Building a queue

Queues follow “first-in, first-out” ordering. Elements enter at the back and exit from the front, like a line at a store. The Queue<T> class maintains a top pointer to the front element and a tail pointer to the back element:

// Generic queue implementation with FIFO ordering
public class Queue<T> {
    var top = Node<T>()
    private var tail: Node<T>
    private var counter: Int = 0

    // Creates an empty queue whose front and back are the same node
    public init() {
        tail = top
    }

    // Number of elements in queue - O(1)
    public var count: Int {
        return counter
    }

    // Add element to back of queue - O(1)
    public func enQueue(_ key: T) {
        // Handle empty queue
        guard top.tvalue != nil else {
            top.tvalue = key
            tail = top
            counter += 1
            return
        }

        // Attach the new node at the tail - no traversal needed
        let childToUse = Node<T>()
        childToUse.tvalue = key
        tail.next = childToUse
        tail = childToUse
        counter += 1
    }

    // Remove element from front of queue - O(1)
    public func deQueue() -> T? {
        // Check if queue is empty
        guard top.tvalue != nil else {
            return nil
        }

        // Retrieve front item
        let queueItem: T? = top.tvalue

        // Move top pointer to next item
        if let nextItem = top.next {
            top = nextItem
            counter -= 1
        } else {
            top.tvalue = nil
            tail = top
            counter = 0
        }

        return queueItem
    }

    // View front element without removing - O(1)
    public func peek() -> T? {
        return top.tvalue
    }

    // Check if queue is empty - O(1)
    public func isEmpty() -> Bool {
        return top.tvalue == nil
    }
}

Without the tail reference, enqueuing would need to walk from top to the last node before attaching a new one, an O(n) traversal on every insertion. Keeping one extra pointer makes enqueuing O(1). Dequeuing is also O(1) — just update the top pointer. When the last element leaves, tail returns to top so the next insertion starts from a clean front node.

Building a stack

Stacks follow “last-in, first-out” ordering. Elements are added and removed from the same end (the top), like a stack of plates. This enables O(1) insertion and removal:

// Generic stack implementation with LIFO ordering
public class Stack<T> {
    public var top: Node<T>  // Public to match production code
    private var counter: Int = 0

    public init() {
        top = Node<T>()
    }

    // Number of elements in stack - O(1)
    public var count: Int {
        return counter
    }

    // Add element to top of stack - O(1)
    public func push(_ tvalue: T) {
        // Handle empty stack
        guard top.tvalue != nil else {
            top.tvalue = tvalue
            counter += 1
            return
        }

        // Create new node
        let childToUse = Node<T>()
        childToUse.tvalue = tvalue

        // Insert at top (new node points to current top)
        childToUse.next = top
        top = childToUse

        counter += 1
    }

    // Remove element from top of stack - O(1)
    @discardableResult
    public func pop() -> T? {
        guard let tvalue = top.tvalue else { return nil }

        if let next = top.next {
            top = next
            counter -= 1
        } else {
            top.tvalue = nil
            counter = 0
        }

        return tvalue
    }

    // View top element without removing - O(1)
    public func peek() -> T? {
        return top.tvalue
    }

    // Check if stack is empty - O(1)
    public func isEmpty() -> Bool {
        return count == 0
    }
}

Unlike queues (which add at the back and remove from the front), stacks add and remove from the same location. This makes all stack operations perform in constant time O(1)—no traversal needed.

Performance characteristics

Both structures excel at their core operations:

Operation Stack Queue Notes
Push/Enqueue O(1) O(1) Add element (queue uses its tail pointer)
Pop/Dequeue O(1) O(1) Remove element
Peek O(1) O(1) View top/front
isEmpty check O(1) O(1) Check if empty
Search O(n) O(n) Not their purpose

The O(1) performance means push, pop, enqueue, and dequeue take constant time regardless of size. For the queue, that guarantee depends on the implementation rather than the abstract structure: the same FIFO behavior built without a tail pointer would make each enqueue O(n), and a million insertions would cost on the order of n² node visits:

// Process 1 million items with consistent performance
let stack = Stack<Int>()
let queue = Queue<Int>()

for i in 0..<1_000_000 {
    stack.push(i)           // Each push: O(1)
    queue.enQueue(i)        // Each enQueue: O(1) with a tail pointer
}

for _ in 0..<1_000_000 {
    stack.pop()             // Each pop: O(1)
    queue.deQueue()         // Each deQueue: O(1)
}

This predictable performance makes both structures ideal for systems requiring consistent response times.

Building algorithmic intuition

Stacks and queues build directly on the linked list techniques from Chapter 9, but add a simple constraint—restricting which end we insert and remove from. That single rule gives each structure its character: a stack’s push and pop mirror how iOS manages navigation views and how debuggers work, while a queue’s enQueue and deQueue reflect the process we expect from task schedulers and network buffers. Both achieve O(1) operations because they never need to search or sort, as long as each end they touch is reachable through a stored pointer. We will rely on these structures repeatedly as we implement tree traversals in Chapter 11, graph algorithms in Chapter 13, and priority-based scheduling in Chapter 17.