先进后出
pyhton 3.x写不写object都可以, class stack(object):
默认写了object,拥有好多操作对象以前不写object的话,就只有几个默认的操作对象
1 class Stack(): 2 def __init__(self,mystack=[]): 3 self._stack = mystack 4 def push(self,data): 5 self._stack.append(data) 6 def pop(self): 7 self._stack.pop() 8 def is_empty(self): 9 return len(self._stack) == 0 10 def peek(self): 11 return self._stack[-1] 12 def size(self): 13 return len(self._stack) 14 def print1(self): 15 print(self._stack) 16 myStack = Stack() 17 print('原栈:',end='') 18 myStack.print1() 19 20 print('压栈:') 21 for i in range(5): 22 myStack.push(i) 23 myStack.print1() 24 print('出栈:') 25 for j in range(5): 26 myStack.pop() 27 myStack.print1()