python实现代数式括号有效性检验?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

思路:
利用栈实现代数式中括号有效行的的检验:
代码:
class mychain(object): #利用链表建立栈,链表为父类
length=0
def __init__(self,value=None,next=None):#创建链表,长度并不包含头部
self.value=value
self.next=next
#mychain.length=mychain.length+1
def append(self,value=None):
while self.next!=None:
self=self.next
self.next=mychain(value)
mychain.length=mychain.length+1 #追加时,链表长度增加
def travle(self):#遍历链表
print(self.value)
if self.next!=None:
self.next.travle()
def drop (self,value):#删除特定值的第一个匹配节点
while self.next!=None:
if self.next.value!=value:
self=self.next
else:
self.next=self.next.next
mychain.length=mychain.length-1 #删除时,链表长度减小
break
def pop(self):#删除未节点
if self.next!=None:#并不删除头结点
while self.next.next!=None:
self=self.next
self.next=None
mychain.length=mychain.length-1#弹出为节点,并减小长度,头结点不弹出
class stock(mychain):#栈类
bottom=None #栈底
top=None #栈顶
n_count=0 #计数
def Max(self): #占中大值
if self.next!=None:
tmp = self.next.value
while self.next.next!=None:
self=self.next
if self.next.value>tmp:
tmp=self.next.value
return tmp
else:
print('栈为空!')
def Min(self):#栈中的最小值
if self.next!=None:
tmp = self.next.value
while self.next.next!=None:
self=self.next
if self.next.value
网站名称:python实现代数式括号有效性检验-创新互联
URL网址:http://cdysf.com/article/dpjpoe.html