first in first out
970 ワード
今日は容器、first in first outと書いてあります.コードは以下の通りです.
class Node:NSObject{
let data:T
init(data:T) {
self.data = data
}
var next:Node?
}
/// 。
class FirstInFirstOut {
private var head:Node? = nil
private var rear:Node? = 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
}
}