# 《swift-algorithm-club》——数据结构/队列

·  阅读 336

### 队列(Queues)

#### 栈

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

public var isEmpty: Bool {
return array.isEmpty
}

public var count: Int {
return array.count
}

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

public mutating func pop() -> T? {
return array.popLast()
}

public var top: T? {
return array.last
}
}

#### 队列

``````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
}
}

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

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

public var count: Int {
}

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 }

if array.count > 50 && percentage > 0.25 {
}
return element
}

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

#### 双端队列

``````public struct Deque<T> {
private 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 enqueueFront(_ element: T) {
array.insert(element, atIndex: 0)
}

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

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

public func peekFront() -> T? {
return array.first
}

public func peekBack() -> T? {
return array.last
}
}

`dequeue()``enqueueFront()` 的时间复杂度是O(n)，因为它们在数组的前面工作。

``````public struct Deque<T> {
private var array: [T?]
private var capacity: Int
private let originalCapacity: Int

public init(_ capacity: Int = 10) {
self.capacity = max(capacity, 1)
originalCapacity = self.capacity
array = [T?](repeating: nil, count: capacity)
}

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

public var count: Int {
}

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

public mutating func enqueueFront(_ element: T) {
capacity *= 2
let emptySpace = [T?](repeating: nil, count: capacity)
array.insert(contentsOf: emptySpace, at: 0)
}

}

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

if capacity >= originalCapacity && head >= capacity*2 {
let amountToRemove = capacity + capacity/2
array.removeFirst(amountToRemove)
capacity /= 2
}
return element
}

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

public func peekFront() -> T? {
if isEmpty {
return nil
} else {
}
}

public func peekBack() -> T? {
if isEmpty {
return nil
} else {
return array.last!
}
}
}

#### 优先级队列

• 事件驱动的模拟。每个事件都有一个时间戳，您希望按照时间戳的顺序执行事件。优先级队列可以轻松找到需要模拟的下一个事件。
• Dijkstra 的图搜索算法使用优先级队列来计算最低成本。
• 霍夫曼编码用于数据压缩。该算法构建压缩树。它反复需要找到具有最小频率且尚未具有父节点的两个节点。
• 用于人工智能的A*寻路。
• 很多其他地方！

``````public struct PriorityQueue<T> {
fileprivate var heap: Heap<T>

public init(sort: (T, T) -> Bool) {
heap = Heap(sort: sort)
}

public var isEmpty: Bool {
return heap.isEmpty
}

public var count: Int {
return heap.count
}

public var peek() -> T? {
return heap.peek()
}

public mutating func enqueue(element: T) {
heap.insert(element)
}

public mutating func dequeue() -> T? {
return heap.remove()
}

public mutating func changePriority(index i: Int, value: T) {
return heap.replace(index: i, value: value)
}
}

#### 环形缓冲区

Swift的一个简单实现

``````public struct RingBuffer<T> {
fileprivate var array: [T?]
fileprivate var writeIndex = 0

public init(count: Int) {
array = [T?](repeating: nil, count: count)
}

public mutating func write(_ element: T) -> Bool {
if !isFull {
array[writeIndex % array.count] = element
writeIndex += 1
return true
} else {
return false
}
}

public mutating func read() -> T? {
if !isEmpty {
let element = array[readIndex % array.count]
return element
} else {
return nil
}
}

}

public var isEmpty: Bool {
}

fileprivate var availableSpaceForWriting: Int {
}

public var isFull: Bool {
return availableSpaceForWriting == 0
}
}

`RingBuffer`对象有一个数组用于实际存储数据，`readIndex``writeIndex` 变量用于指向数组的“指针”。`wirte()` 函数将新元素放入`writeIndex` 中的数组中，`read()` 函数返回 `readIndex`中的元素。

``````var buffer = RingBuffer<Int>(count: 5)

buffer.write(123)
buffer.write(456)
buffer.write(789)
buffer.write(666)

buffer.write(333)
buffer.wirte(555)