Algorithm to convert Infix to Postfix using STACK:
- Read next element in the input expression.
- If it is an operand, output it.
- If it is an opening parenthesis, push on to the Stack.
- If it is an operator, then
- If the Stack is empty, push operator on Stack
- If the Top of stack is Opening parenthesis, push operator on to the Stack.
- If the operator has higher priority than top of the Stack, push operator on to the Stack else Pop operator from the Stack and output it, repeat Step 4
- If the is a closing parenthesis, pop operators from stack and output them until opening parenthesis is encountered. POP and discard opening parenthesis.
- If there is more input Go to step 1.
- If no more input, unstuck all operators from stack and output.
Note: Prefix and Postfix expressions do not contain parenthesis. In conversion part, we remove parenthesis using algorithm rules.
Expression: 2*3/(2-1) + 5*(4-1)