Array 实现栈
struct Stack<Element> {
private var stack: [Element]
var isEmpty: Bool { return stack.isEmpty }
var peek: AnyObject? { return stack.last as AnyObject? }
init() {
stack = [Element]()
}
mutating func push(_ element: Element) {
stack.append(element)
}
mutating func pop() -> Element? {
return stack.popLast()
}
}
swift4.0 关键字 mutating
可以修饰值类型(struct,enum)内实现的方法,允许在方法内修改struct,enum 中定义的属性。
网友评论