Update python code of stack.

This commit is contained in:
Yudong Jin
2022-11-29 23:35:51 +08:00
parent 3edfe649af
commit 53cc651af2
3 changed files with 60 additions and 60 deletions

View File

@ -4,14 +4,15 @@ Created Time: 2022-11-29
Author: Peng Chen (pengchzn@gmail.com)
'''
import os.path as osp
import sys
import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
""" Driver Code """
if __name__ == "__main__":
""" 初始化栈 """
# Python 没有内置的栈类,可以把 list 当作栈来使用
stack = []
""" 元素入栈 """
@ -20,20 +21,20 @@ if __name__ == "__main__":
stack.append(2)
stack.append(5)
stack.append(4)
print("栈 stack = ", stack)
print("栈 stack =", stack)
""" 访问栈顶元素 """
peek = stack[-1]
print("栈顶元素 peek = ", peek)
print("栈顶元素 peek =", peek)
""" 元素出栈 """
pop = stack.pop()
print("出栈元素 pop = ", pop)
print("出栈后 stack = ", stack)
print("出栈元素 pop =", pop)
print("出栈后 stack =", stack)
""" 获取栈的长度 """
size = len(stack)
print("栈的长度 size = ", size)
print("栈的长度 size =", size)
""" 判断是否为空 """
isEmpty = (stack == [])
isEmpty = len(stack) == 0