Infix to Postfix Conversion
Example 1: a + b
- Read ‘a’ and add to output: Output = a
- Read ‘+’ and push onto stack: Stack = +
- Read ‘b’ and add to output: Output = ab
- Finish by popping operators from stack to output: Output = ab+
Final Result: ab+
Example 2: (a + b) * c
- Read ‘(‘ and push onto stack: Stack = (
- Read ‘a’ and add to output: Output = a
- Read ‘+’ and push onto stack: Stack = (, +
- Read ‘b’ and add to output: Output = ab
- Read ‘)’ and pop operators until ‘(‘: Output = ab+, Stack =
- Read ‘*’ and push onto stack: Stack = *
- Read ‘c’ and add to output: Output = ab+c
- Finish by popping operators from stack to output: Output = ab+c*
Final Result: ab+c*
Example 3: a + b * c
- Read ‘a’ and add to output: Output = a
- Read ‘+’ and push onto stack: Stack = +
- Read ‘b’ and add to output: Output = ab
- Read ‘*’ and push onto stack: Stack = +, *
- Read ‘c’ and add to output: Output = abc
- Finish by popping operators from stack to output: Output = abc*+
Final Result: abc*+
Example 4: a * b + c / d
- Read ‘a’ and add to output: Output = a
- Read ‘*’ and push onto stack: Stack = *
- Read ‘b’ and add to output: Output = ab
- Read ‘+’ and pop ‘*’ to output, push ‘+’: Output = ab*, Stack = +
- Read ‘c’ and add to output: Output = ab*c
- Read ‘/’ and push onto stack: Stack = +, /
- Read ‘d’ and add to output: Output = ab*cd
- Finish by popping operators from stack to output: Output = ab*cd/+
Final Result: ab*cd/+
Example 5: (a + b) * (c + d)
- Read ‘(‘ and push onto stack: Stack = (
- Read ‘a’ and add to output: Output = a
- Read ‘+’ and push onto stack: Stack = (, +
- Read ‘b’ and add to output: Output = ab
- Read ‘)’ and pop operators until ‘(‘: Output = ab+, Stack =
- Read ‘*’ and push onto stack: Stack = *
- Repeat steps 1-5 for second (c + d): Output = ab+cd+, Stack = *
- Finish by popping operators from stack to output: Output = ab+cd+*
Final Result: ab+cd+*