class Stack(object):
def __init__(self):
self.__stack = []
def push(self, data):
self.__stack.append(data)
def is_empty(self):
return True if len(self.__stack) == 0 else False
def get_size(self):
return len(self.__stack)
def pop(self):
if not self.is_empty():
return self.__stack.pop()
else:
raise IndexError("pop from empty stack")
def top(self):
if not self.is_empty():
return self.__stack[-1]
else:
raise IndexError("stack is empty")
s = Stack()
s.push(1)
s.push(2)
print(s.get_size())
s.pop()
print(s.get_size())
Python栈的实现
最新推荐文章于 2024-07-16 22:30:00 发布