# 【译】Swift算法俱乐部-队列

0.288字数 2237阅读 388

Swift Algorithm Clubraywenderlich.com网站出品的用Swift实现算法和数据结构的开源项目，目前在GitHub上有18000+⭐️，我初略统计了一下，大概有一百左右个的算法和数据结构，基本上常见的都包含了，是iOSer学习算法和数据结构不错的资源。
🐙andyRon/swift-algorithm-club-cn是我对Swift Algorithm Club，边学习边翻译的项目。由于能力有限，如发现错误或翻译不妥，请指正，欢迎pull request。也欢迎有兴趣、有时间的小伙伴一起参与翻译和学习🤓。当然也欢迎加⭐️，🤩🤩🤩🤨🤪。

``````queue.enqueue(10)
``````

``````queue.enqueue(3)
``````

``````queue.enqueue(57)
``````

``````queue.dequeue()
``````

``````queue.dequeue()
``````

## 代码

``````public struct Queue<T> {
fileprivate var array = [T]()

public var isEmpty: Bool {
return array.isEmpty
}

public var count: Int {
return array.count
}

public mutating func enqueue(_ element: T) {
array.append(element)
}

public mutating func dequeue() -> T? {
if isEmpty {
return nil
} else {
return array.removeFirst()
}
}

public var front: T? {
return array.first
}
}
``````

``````var queue = Queue<String>()
queue.enqueue("Steve")
queue.enqueue("Tim")
``````

``````  [ "Ada", "Steve", "Tim", xxx, xxx, xxx ]
``````

`xxx` 代表已经申请，但还没有使用的内存。在尾部添加一个新的元素就会用到下一块未被使用的内存：

``````  [ "Ada", "Steve", "Tim", "Grace", xxx, xxx ]
``````

``````  出队前   [ "Ada", "Steve", "Tim", "Grace", xxx, xxx ]
/       /      /
/       /      /
/       /      /
/       /      /
出队后   [ "Steve", "Tim", "Grace", xxx, xxx, xxx ]
``````

## 更加高效的队列

``````  [ xxx, "Steve", "Tim", "Grace", xxx, xxx ]
``````

`"Steve"` 出队后，数组如下：

``````  [ xxx, xxx, "Tim", "Grace", xxx, xxx ]
``````

``````  [ "Tim", "Grace", xxx, xxx, xxx, xxx ]
``````

``````public struct Queue<T> {
fileprivate var array = [T?]()
fileprivate var head = 0

public var isEmpty: Bool {
return count == 0
}

public var count: Int {
return array.count - head
}

public mutating func enqueue(_ element: T) {
array.append(element)
}

public mutating func dequeue() -> T? {
guard head < array.count, let element = array[head] else { return nil }

let percentage = Double(head)/Double(array.count)
if array.count > 50 && percentage > 0.25 {
}

return element
}

public var front: T? {
if isEmpty {
return nil
} else {
}
}
}
``````

``````  [ "Ada", "Steve", "Tim", "Grace", xxx, xxx ]
``````

``````  [ xxx, "Steve", "Tim", "Grace", xxx, xxx ]
``````

``````    let percentage = Double(head)/Double(array.count)
if array.count > 50 && percentage > 0.25 {
}
``````

``````var q = Queue<String>()
q.array                   // [] empty array

q.enqueue("Steve")
q.enqueue("Tim")
q.array             // [{Some "Ada"}, {Some "Steve"}, {Some "Tim"}]
q.count             // 3

q.array             // [nil, {Some "Steve"}, {Some "Tim"}]
q.count             // 2

q.dequeue()         // "Steve"
q.array             // [nil, nil, {Some "Tim"}]
q.count             // 1

q.enqueue("Grace")
q.array             // [nil, nil, {Some "Tim"}, {Some "Grace"}]
q.count             // 2
``````

``````    if array.count > 50 && percentage > 0.25 {
``````

``````    if head > 2 {
``````

``````q.dequeue()         // "Tim"
q.array             // [{Some "Grace"}]
q.count             // 1
``````