def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
a = factorial(5)
print(a)
############################
def push_stack(x):#入
if len(ls)<num:
print("入",x)
ls.append(x)
else:
print("ERR: push_stack from full list")
def pop_stack():#出
if ls:
r=ls.pop()
print("出", r)
return r
else:
print("ERR: pop_stack from empty list")
return None
############################
ls = []
num = 3 # 长度为10
def push_quene(x):#入
if len(ls)<num:
print("入",x)
ls.append(x)
else:
print("ERR: push_quene from full list")
def pop_quene():#出
if ls:
r=ls.pop(0)
print("出", r)
return r
else:
print("ERR: pop_quene from empty list")
return None
############################
print("-"*88)
print("先进先出 队列")
push_quene("A")
push_quene("B")
push_quene("C")
push_quene("D")
pop_quene()
pop_quene()
pop_quene()
pop_quene()
############################
print("先进后出 栈")
# 先进后出 栈 规定栈的长度为10
push_stack("1")
push_stack("2")
push_stack("3")
pop_stack()
pop_stack()
r=pop_stack()
print(r)
r=pop_stack()
print(r)
用python列表实现队列和栈的功能
最新推荐文章于 2024-03-28 10:38:56 发布