栈:先进后出
特性:次序反转
用python实现栈
class Stack():
def __init__(self):
self.items = []
def isEmpty(self):
return self.items ==[]
def push(self,item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.item[len(self.items)-1]
def size(self):
return len(self.items)
栈的代码测试
from pythonds.basic.stack import Stack
s = Stack()
print(s.isEmpty())
s.push(1)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())