# @File : stu.py
# @Author: Wang Zhimin
# @Date : 2019/10/19
class Node:
def __init__(self):
self.data=0
self.next=None
top=None
def isempty():
global top
if(top==None):
return True
else:
return False
def push(data):
global top
new_node=Node()
new_node.data=data
new_node.next=top
top=new_node
def pop():
global top
if isempty():
print("堆栈已空")
return -1
else:
ptr=top
top=top.next
temp=ptr.data
return temp
while True:
i=int(input("压入堆栈,请输入1,弹出请输入0,停止输入-1: "))
if i ==-1:
break
elif i==1:
value=int(input("请输入要输入的值"))
push(value)
elif i ==0:
print('弹出的元素为%d'%pop())
print("===============================================")
while(not isempty()):
print('堆栈弹出的顺序为:%d'%pop())
print("===============================================")
19.python 用链表实现堆栈操作
最新推荐文章于 2022-01-16 20:43:36 发布