Infix to postfix-3

Example 10: (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. Read ‘c’ and add to output: Output = ab-c
  8. Read ‘-‘ and pop ‘*’ to output, push ‘-‘: Output = ab-c*, Stack = –
  9. Read ‘d’ and add to output: Output = ab-c*d
  10. Finish by popping operators from stack to output: Output = ab-c*d-

Final Result: ab-c*d-

Example 11: a / (b + c * d)

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

Final Result: abcd*+/

Example 12: 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 push onto stack: Stack = ^, *
  5. Read ‘c’ and add to output: Output = abc
  6. Read ‘+’ and pop ‘*’ and ‘^’ to output, push ‘+’: Output = abc*^, Stack = +
  7. Read ‘d’ and add to output: Output = abc*^d
  8. Finish by popping operators from stack to output: Output = abc*^d+

Final Result: abc*^d+

Example 13: (a + b) / (c – d * e)

  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 * e): Output = ab+cde*-
  8. Finish by popping operators from stack to output: Output = ab+cde*-/

Final Result: ab+cde*-/

Leave a Comment

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