Chapter 9: Challenges: Queue Data Structure

Challenge 1: Explain the difference between a stack and a queue. Provide two real-life examples for each data structure.

  • Queues have a behavior of first-in-first-out

    • Line in a movie theatre

    • Printer

  • Stacks have a behavior of last-in-first-out

    • Stack of plates

    • Undo functionality

Challenge 2

Challenge 3: Imagine that you are playing a game of Monopoly with your friends. The problem is that everyone always forget whose turn it is! Create a Monopoly organizer that always tells you whose turn it is. Below is a protocol that you can conform to

extension QueueArray: BoardGameManager {

    public typealias Player = T

    public mutating func nextPlayer() -> T? {
        // 大富翁的順序FIFO,符合queue本質
        guard let person = dequeue() else {
            return nil
        }
        // 把next player再次加入queue中,等待下次循環
        enqueue(person)

        return person
    }
}

Challenge 4: Implement a method to reverse the contents of a queue

Last updated