Python容器 - 栈

Python中没有没专门的stack类,而是可以把list当作栈来用

栈是一种先进后出的数据结构,主要的操作只有:入栈,出栈

 

stack1 = []
# 入栈
stack1.append(1)
stack1.append(2)
stack1.append(3)
print(type(stack1))  # <class 'list'>
print(len(stack1))  # 3

# 出栈
print(stack1.pop())  # 3
print(stack1.pop())  # 2
print(stack1.pop())  # 1
print(len(stack1))  # 0

 

Python3 数据结构 | 菜鸟教程 (runoob.com)

 

posted @ 2023-07-21 00:34  yanghui01  阅读(9)  评论(0)    收藏  举报