class Node:
def __init__(self):
self.next=None
self.data=0
top=None
def isempty():
if top==None:
return 1
else:
return 0
def push(data):
global top
new_node=Node()
new_node.next=top
new_node.data=data
top=new_node
def pop():
global top
if isempty()==1:
print("堆栈已空")
return -1
temp=top.data
top=top.next
return temp
while True:
i=int(input('输入-1停止,输入1压栈,输入0出栈'))
if i==-1:
break
elif i==1:
data=int(input('输入'))
push(data)
else:
pop()
print('============================================')
while(not isempty()):
print('堆栈弹出的顺序为:%d'%pop())
print('============================================')
参考图解数据结构书