如何检测后缀表达式的合法性 python(数据结构——栈的应用)
def check_postfix_expr(postfix_expr):
stack = []
for token in postfix_expr:
if token.isdigit():
stack.append(token)
else:
if len(stack) >= 2:
operand2 = stack.pop()
operand1 = stack.pop()
stack.append('1') # push a dummy value to the stack
else:
return False
return len(stack) == 1
尝试一下,你的问题不太明确