class Stack():
#以顺序表形式实现
def __init__(self):
self.__list = []
def push(self,item):
#压栈
self.__list.append(item)
def pop(self):
#出栈
self.__list.pop()
def peek(self):
#弹出栈顶元素
if self.__list:
return self.__list[-1]
else:
return None
def is_empty(self):
#判断栈是否为空
return len(self.__list) == 0
def size(self):
#计算栈的长度
return len(self.__list)
if __name__ == '__main__':
stack = Stack()
stack.push(50)
stack.push(60)
print(stack.peek())
stack.pop()
stack.pop()
print(stack.size())
print(stack.is_empty())
栈基本操作(压栈出栈弹出栈顶元素判空)Python版本
最新推荐文章于 2022-04-30 18:28:41 发布