protocol Stack {
associatedtype Element
var isEmpty : Bool {get}
var size : Int {get}
//栈顶元素
var peek : Element?{get}
func push(_ newElement : Element)
func pop() -> Element?
}
class IntegerStack : Stack {
typealias Element = Int
var isEmpty: Bool{
return stack.isEmpty
}
var size: Element{
return stack.count
}
var peek: Element?{
return stack.last
}
func push(_ newElement: Element) {
stack.append(newElement)
}
func pop() -> Element? {
return stack.popLast()
}
var stack = [Element]()
}
网友评论