Stack
TimeO(n)SpaceO(n)
Scan the expression left to right. Operands go straight to the output; operators wait on a stack until a higher- or equal-precedence operator, or a closing parenthesis, forces them off. The result is postfix — readable with no parentheses and no precedence rules.
Key idea. A stack holds operators until precedence or a ')' forces them out — converting infix to postfix in one left-to-right pass. The same LIFO insight as bracket matching, now carrying operators instead of just openers.
1
Loading…
—