infix to postfix

Infix to Postfix Conversion

Infix to Postfix Conversion

Example 1: a + b

  1. Read ‘a’ and add to output: Output = a
  2. Read ‘+’ and push onto stack: Stack = +
  3. Read ‘b’ and add to output: Output = ab
  4. Finish by popping operators from stack to output: Output = ab+

Final Result: ab+

Example 2: (a + b) * c

  1. Read ‘(‘ and push onto stack: Stack = (
  2. Read ‘a’ and add to output: Output = a
  3. Read ‘+’ and push onto stack: Stack = (, +
  4. Read ‘b’ and add to output: Output = ab
  5. Read ‘)’ and pop operators until ‘(‘: Output = ab+, Stack =
  6. Read ‘*’ and push onto stack: Stack = *
  7. Read ‘c’ and add to output: Output = ab+c
  8. Finish by popping operators from stack to output: Output = ab+c*

Final Result: ab+c*

Example 3: a + b * c

  1. Read ‘a’ and add to output: Output = a
  2. Read ‘+’ and push onto stack: Stack = +
  3. Read ‘b’ and add to output: Output = ab
  4. Read ‘*’ and push onto stack: Stack = +, *
  5. Read ‘c’ and add to output: Output = abc
  6. Finish by popping operators from stack to output: Output = abc*+

Final Result: abc*+

Example 4: a * b + c / d

  1. Read ‘a’ and add to output: Output = a
  2. Read ‘*’ and push onto stack: Stack = *
  3. Read ‘b’ and add to output: Output = ab
  4. Read ‘+’ and pop ‘*’ to output, push ‘+’: Output = ab*, Stack = +
  5. Read ‘c’ and add to output: Output = ab*c
  6. Read ‘/’ and push onto stack: Stack = +, /
  7. Read ‘d’ and add to output: Output = ab*cd
  8. Finish by popping operators from stack to output: Output = ab*cd/+

Final Result: ab*cd/+

Example 5: (a + b) * (c + d)

  1. Read ‘(‘ and push onto stack: Stack = (
  2. Read ‘a’ and add to output: Output = a
  3. Read ‘+’ and push onto stack: Stack = (, +
  4. Read ‘b’ and add to output: Output = ab
  5. Read ‘)’ and pop operators until ‘(‘: Output = ab+, Stack =
  6. Read ‘*’ and push onto stack: Stack = *
  7. Repeat steps 1-5 for second (c + d): Output = ab+cd+, Stack = *
  8. Finish by popping operators from stack to output: Output = ab+cd+*

Final Result: ab+cd+*

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top