def size(self):
return len(self.items)
def parChecker(symbolstring):
print(symbolstring)
#实例化栈(空栈)
s = Stack()
#括号匹配法则
balanced = True
index = 0
while index < len(symbolstring) and balanced:
symbol = symbolstring[index]
if symbol == “(”:
s.push(symbol)
else:
#右括号多了或左括号少了
if s.isEmpty():#判定栈 是否为空
balanced = False
else:
s.pop()
index = index + 1
if balanced and s.isEmpty():
return True
else:
return False
result = parChecker(“(())”)
print(result)
print(parChecker(“(()”)) 运行结果:
怎样构造各范例括号匹配识别算法