栈是一种“先进后出”的一种数据结构,有压栈出栈两种操作方式。如下图:
栈的操作:
class Stack(object):
def init(self):
self.__list = []
def push(self,item):
self.__list .append(item)
def pop(self):
return self.__list .pop()
def peek(self):
if self.__list :
return self.__list[-1]
else:
return None
def is_empty(self):
return self.__list ==[]
def size(self):
return (self.__list )
if name ==“main”:
s = Stack()
s.push(1)
s.push(2)
s.push(3)
s.push(4)
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())
输出:
G:\anaconda\python.exe G:/pycharm/python_jichu/02_stack.py
4
3
2
1
Process finished with exit code 0