|
1 | 1 | /*
|
| 2 | + Queue |
2 | 3 |
|
3 |
| -Queue |
4 |
| - |
5 |
| -A queue is a list where you can only insert new items at the back and remove items from the front. This ensures that the first item you enqueue is also the first item you dequeue. First come, first serve! |
6 |
| - |
7 |
| -A queue gives you a FIFO or first-in, first-out order. The element you inserted first is also the first one to come out again. It's only fair! (A very similar data structure, the stack, is LIFO or last-in first-out.) |
8 |
| - |
9 |
| -Insertion - O(1) |
10 |
| -Deletion - O(n) |
| 4 | + A queue is a list where you can only insert new items at the back and |
| 5 | + remove items from the front. This ensures that the first item you enqueue |
| 6 | + is also the first item you dequeue. First come, first serve! |
11 | 7 |
|
| 8 | + A queue gives you a FIFO or first-in, first-out order. The element you |
| 9 | + inserted first is also the first one to come out again. It's only fair! |
12 | 10 |
|
| 11 | + In this implementation, enqueuing is an O(1) operation, dequeuing is O(n). |
13 | 12 | */
|
14 | 13 |
|
15 |
| - |
16 |
| -import UIKit |
17 |
| - |
18 |
| - |
19 | 14 | public struct Queue<T> {
|
20 |
| - private var array = [T]() |
21 |
| - |
22 |
| - public var count: Int { |
23 |
| - return array.count |
24 |
| - } |
25 |
| - |
26 |
| - public var isEmpty: Bool { |
27 |
| - return array.isEmpty |
28 |
| - } |
29 |
| - |
30 |
| - public mutating func enqueue(element: T) { |
31 |
| - array.append(element) |
32 |
| - } |
33 |
| - |
34 |
| - public mutating func dequeue() -> T? { |
35 |
| - if count == 0 { |
36 |
| - return nil |
37 |
| - } else { |
38 |
| - return array.removeFirst() |
39 |
| - } |
40 |
| - } |
41 |
| - |
42 |
| - public func peek() -> T? { |
43 |
| - return array.first |
| 15 | + private var array = [T]() |
| 16 | + |
| 17 | + public var count: Int { |
| 18 | + return array.count |
| 19 | + } |
| 20 | + |
| 21 | + public var isEmpty: Bool { |
| 22 | + return array.isEmpty |
| 23 | + } |
| 24 | + |
| 25 | + public mutating func enqueue(element: T) { |
| 26 | + array.append(element) |
| 27 | + } |
| 28 | + |
| 29 | + public mutating func dequeue() -> T? { |
| 30 | + if count == 0 { |
| 31 | + return nil |
| 32 | + } else { |
| 33 | + return array.removeFirst() |
44 | 34 | }
|
| 35 | + } |
| 36 | + |
| 37 | + public func peek() -> T? { |
| 38 | + return array.first |
| 39 | + } |
45 | 40 | }
|
46 | 41 |
|
| 42 | +// Create a queue and put some elements on it already. |
| 43 | +var queueOfNames = Queue(array: ["Carl", "Lisa", "Stephanie", "Jeff", "Wade"]) |
47 | 44 |
|
48 |
| -var queueOfNames = Queue(array: ["Carl", "Lisa", "Stephanie", "Jeff", "Wade"]) // Initialized queue |
49 |
| - |
50 |
| -queueOfNames.enqueue("Mike") // Adds element to the end of the queue. Queue is now ["Carl", "Lisa", "Stephanie", "Jeff", "Wade", "Mike"] |
| 45 | +// Adds an element to the end of the queue. |
| 46 | +queueOfNames.enqueue("Mike") |
51 | 47 |
|
52 |
| -queueOfNames.dequeue() // Removes and returns the first element in the queue. Returns "Carl" and then removes it from the queue |
| 48 | +// Queue is now ["Carl", "Lisa", "Stephanie", "Jeff", "Wade", "Mike"] |
| 49 | +print(queueOfNames.array) |
53 | 50 |
|
54 |
| -queueOfNames.peek() // Returns the first element in the queue. Returns "Lisa" since "Carl" was dequeued on the previous line |
| 51 | +// Remove and return the first element in the queue. This returns "Carl". |
| 52 | +queueOfNames.dequeue() |
55 | 53 |
|
56 |
| -queueOfNames.isEmpty // Checks to see if the queue is empty. Returns "False" since the queue still has elements in it. |
| 54 | +// Return the first element in the queue. |
| 55 | +// Returns "Lisa" since "Carl" was dequeued on the previous line. |
| 56 | +queueOfNames.peek() |
57 | 57 |
|
| 58 | +// Check to see if the queue is empty. |
| 59 | +// Returns "false" since the queue still has elements in it. |
| 60 | +queueOfNames.isEmpty |
0 commit comments