代码
class Stack:
def __init__(self):
self.__data = []
def push(self, item):
self.__data.append(item)
def pop(self):
if self.is__empty():
raise ValueError("栈为空")
return self.__data.pop()
def top(self):
if self.is__empty():
raise ValueError("栈为空")
return self.__data[-1]
def is__empty(self):
return self.__data == []
def size(self):
return len(self.__data)
if __name__ == '__main__':
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
结果
