|
| 1 | +# Data Structures: Queue |
| 2 | + |
| 3 | +A Queue is a fundamental data structure that follows the First-In-First-Out (FIFO) principle. Think of it like a line at a coffee shop - the first person to join the line is the first person to be served. |
| 4 | + |
| 5 | +## Official Documentation & References |
| 6 | +- [Swift Algorithm Club - Queue](https://github.com/kodecocodes/swift-algorithm-club/tree/master/Queue) |
| 7 | +- [Apple Developer - Swift Standard Library](https://developer.apple.com/documentation/swift/swift_standard_library) |
| 8 | +- [Data Structures in Swift - Queue Implementation](https://www.kodeco.com/books/data-structures-algorithms-in-swift/v4.0/chapters/8-queues) |
| 9 | +- [Swift Collections Documentation](https://developer.apple.com/documentation/swift/collections) |
| 10 | +- [Generic Programming in Swift](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/generics) |
| 11 | +- [Protocol-Oriented Programming](https://developer.apple.com/videos/play/wwdc2015/408/) |
| 12 | + |
| 13 | +## Queue Characteristics |
| 14 | + |
| 15 | +### Key Properties |
| 16 | +- **FIFO (First-In-First-Out)**: Elements are processed in the order they were added |
| 17 | +- **Two main operations**: Enqueue (add to rear) and Dequeue (remove from front) |
| 18 | +- **Linear data structure**: Elements are arranged in a sequence |
| 19 | + |
| 20 | +### When to Use Queues |
| 21 | +- Task scheduling (process tasks in order received) |
| 22 | +- Breadth-first search algorithms |
| 23 | +- Message queuing systems |
| 24 | +- Print spoolers |
| 25 | +- Call center phone systems |
| 26 | + |
| 27 | +## Core Operations |
| 28 | + |
| 29 | +### Essential Methods |
| 30 | +1. **enqueue(_:)** - Add an element to the rear of the queue |
| 31 | +2. **dequeue()** - Remove and return the element from the front |
| 32 | +3. **peek()** - View the front element without removing it |
| 33 | +4. **isEmpty** - Check if the queue has no elements |
| 34 | +5. **count** - Get the number of elements in the queue |
| 35 | + |
| 36 | +## Implementation Approaches |
| 37 | + |
| 38 | +### Array-Based Queue |
| 39 | +```swift |
| 40 | +struct Queue<Element> { |
| 41 | + private var elements: [Element] = [] |
| 42 | + |
| 43 | + mutating func enqueue(_ element: Element) { |
| 44 | + elements.append(element) |
| 45 | + } |
| 46 | + |
| 47 | + mutating func dequeue() -> Element? { |
| 48 | + guard !elements.isEmpty else { return nil } |
| 49 | + return elements.removeFirst() |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | +**Pros**: Simple implementation |
| 54 | +**Cons**: O(n) dequeue operation due to array shifting |
| 55 | + |
| 56 | +### Optimized Array-Based Queue |
| 57 | +Uses two arrays or a circular buffer to achieve O(1) amortized operations. |
| 58 | + |
| 59 | +### Linked List-Based Queue |
| 60 | +Uses nodes with references to achieve true O(1) operations for both enqueue and dequeue. |
| 61 | + |
| 62 | +## Time Complexity |
| 63 | + |
| 64 | +| Operation | Array-Based | Optimized | Linked List | |
| 65 | +|-----------|------------|-----------|-------------| |
| 66 | +| Enqueue | O(1)* | O(1) | O(1) | |
| 67 | +| Dequeue | O(n) | O(1)* | O(1) | |
| 68 | +| Peek | O(1) | O(1) | O(1) | |
| 69 | +| Space | O(n) | O(n) | O(n) | |
| 70 | + |
| 71 | +*Amortized time complexity |
| 72 | + |
| 73 | +## Exercise Progression |
| 74 | + |
| 75 | +1. **queue1.swift** - Basic Structure |
| 76 | + - Create a Queue struct with storage array |
| 77 | + - Understand the basic structure |
| 78 | + |
| 79 | +2. **queue2.swift** - Enqueue Operation |
| 80 | + - Implement adding elements to the queue |
| 81 | + - Understand rear insertion |
| 82 | + |
| 83 | +3. **queue3.swift** - Dequeue Operation |
| 84 | + - Implement removing elements from the queue |
| 85 | + - Handle empty queue cases |
| 86 | + |
| 87 | +4. **queue4.swift** - Peek and Helper Methods |
| 88 | + - Implement peek to view without removing |
| 89 | + - Add isEmpty and count properties |
| 90 | + |
| 91 | +5. **queue5.swift** - Generic Queue |
| 92 | + - Convert to generic implementation |
| 93 | + - Work with any type, not just Int |
| 94 | + |
| 95 | +6. **queue6.swift** - Protocol Conformance |
| 96 | + - Make Queue conform to Collection |
| 97 | + - Enable iteration and standard collection operations |
| 98 | + |
| 99 | +## Tips for Success |
| 100 | + |
| 101 | +1. **Visualize the Queue**: Draw out the queue operations on paper |
| 102 | +2. **Handle Edge Cases**: Always check for empty queue before dequeue/peek |
| 103 | +3. **Think About Efficiency**: Consider time complexity of operations |
| 104 | +4. **Use Optionals**: Return nil for operations on empty queues |
| 105 | +5. **Test Thoroughly**: Try edge cases like single element, empty queue |
| 106 | + |
| 107 | +## Advanced Topics (Beyond These Exercises) |
| 108 | + |
| 109 | +- **Priority Queues**: Elements are dequeued based on priority |
| 110 | +- **Deque (Double-ended Queue)**: Can add/remove from both ends |
| 111 | +- **Circular Queues**: Fixed-size queue that wraps around |
| 112 | +- **Concurrent Queues**: Thread-safe queue implementations |
| 113 | + |
| 114 | +## Real-World Applications |
| 115 | + |
| 116 | +- **iOS Development**: DispatchQueue for managing tasks |
| 117 | +- **Networking**: Request queuing and rate limiting |
| 118 | +- **Gaming**: Turn-based game mechanics |
| 119 | +- **UI**: Animation queues and event handling |
| 120 | + |
| 121 | +## Further Reading |
| 122 | + |
| 123 | +- [Algorithms, 4th Edition - Queues](https://algs4.cs.princeton.edu/13stacks/) |
| 124 | +- [Introduction to Algorithms - CLRS](https://mitpress.mit.edu/books/introduction-algorithms-third-edition) |
| 125 | +- [Swift Forums - Data Structures Discussion](https://forums.swift.org/c/related-projects/swift-collections/72) |
0 commit comments