# -*- coding: utf-8 -*-
class Stack(object):
"""栈"""
def __init__(self):
self.__list = []
def push(self, item):
"""添加一个新元素item到站定"""
self.__list.append(item)
@property
def pop(self):
"""弹出栈顶元素"""
return self.__list.pop()
@property
def peek(self):
"""返回栈顶元素"""
if self.__list:
return self.__list[-1]
@property
def is_empty(self):
"""判断是否为空"""
return not self.__list
@property
def size(self):
"""返回栈的元素个数"""
return len(self.__list)
if __name__ == '__main__':
s = Stack()
print(s.is_empty)
s.push(1)
s.push(2)
s.push(3)
s.push(4)
s.push(5)
print(s.is_empty)
print(s.size)
print(s.peek)
print(s.pop)
print(s.pop)
print(s.pop)
print(s.pop)
print(s.pop)
print(s.peek)
print(s.size)
print(s.is_empty)
python实现简单栈
最新推荐文章于 2024-04-21 09:34:20 发布
5万+

被折叠的 条评论
为什么被折叠?



