/// 队列
class Queue<Element> {
var left: [Element]
var right: [Element]
/// 队列是否为空
var isEmpty: Bool { return left.isEmpty && right.isEmpty }
/// 队列长度
var size: Int { return left.count + right.count }
/// 队列顶元素
var peek: Element? { return left.isEmpty ? right.first : left.last }
init() {
left = [Element]()
right = [Element]()
}
/// 入队
/// - Parameter object: 元素
func enqueue(object: Element) {
right.append(object)
}
/// 出队
/// - Returns: 元素
func dequeue() -> Element? {
if left.isEmpty {
left = right.reversed()
right.removeAll()
}
return left.popLast()
}
}
字典
基本用法
// 创建
var m = [String: Int]()
// 设置kv
m["hello"] = 1
// 删除k
m["hello"] = nil
if let removedValue = m.removeValue(forKey: "hello") {
print("The removed airport's name is \(removedValue).")
} else {
print("The airports dictionary does not contain a value for DUB.")
}
// 遍历
for (k, v) in m {
print("\(k): \(v)")
}