今天写了一个容器,first in first out,代码如下:
class Node<T>:NSObject{
let data:T
init(data:T) {
self.data = data
}
var next:Node?
}
/// 先进先出的算法。
class FirstInFirstOut<T> {
private var head:Node<T>? = nil
private var rear:Node<T>? = nil
private(set) var count:Int = 0
var isEmpty:Bool{
return count == 0
}
/// 加入数据
func push(object:T){
if isEmpty {
head = Node(data: object)
head?.next = nil
rear = head
}else{
rear?.next = Node(data: object)
rear = rear?.next
}
count += 1
}
///拿出数据
func pop() -> T?{
guard !isEmpty else {
return nil
}
let res = head!.data
head = head?.next
count -= 1
return res
}
}