用数组实现栈
class Stack {
var stack: [AnyObject]
var isEmpty: Bool { return stack.isEmpty }
var peek: AnyObject? { return stack.last }
var count: Int { return stack.count }
init() {
stack = [AnyObject]()
}
func push(object: AnyObject) {
stack.append(object)
}
func pop() -> AnyObject? {
if (!isEmpty) {
return stack.removeLast()
} else {
return nil
}
}
}
let stack = Stack()
stack.push(object: "123" as AnyObject)
stack.push(object: 123 as AnyObject)
stack.push(object: 43242 as AnyObject)
print(stack.count)
stack.pop()
stack.peek
stack.pop()
stack.peek
stack.pop()
stack.peek
print(stack.count)
网友评论