INfix to postfix -2

Example 6: (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*/

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

Final Result: ab/c+d-

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

Final Result: abc+*d/

Leave a Comment

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

Scroll to Top