The String Stack
We use the same String Stack approach. Operands are pushed as length-1 strings.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Converting between the two machine notations is simple. The logic is identical to Postfix-to-Infix, except we format the combined string as `operator + val1 + val2` instead of adding parentheses.
We use the same String Stack approach. Operands are pushed as length-1 strings.
When an operator is encountered, pop `val2` and `val1`. Since we want Prefix notation, the operator must go FIRST. So we concatenate: `operator + val1 + val2`.
Unlike Infix, Prefix notation does not require parentheses to maintain order of operations, keeping the strings clean and concise.