Converting From Prefix to Infix: The Stack Method

Here is the algorithm we will use for converting from infix to prefix:

1. Reverse the expression

2. Read expression one character at a time

3. Depending on character read:
        ")": Push onto stack
        Operator: Push onto stack
        Operand: Push on and pop off (straight to output)
        "(": Keep popping until corresponding ")" is encountered

4. Reverse the output

And that’s it!

For now you can use the stack code from Moodle┬áto practice, but we’ll shortly have an interactive version here too.