infix to postfix-5

Example 14: (x + y) * z / a – b

  1. Read ‘(‘ and push onto stack: Stack = (
  2. Read ‘x’ and add to output: Output = x
  3. Read ‘+’ and push onto stack: Stack = (, +
  4. Read ‘y’ and add to output: Output = xy
  5. Read ‘)’ and pop operators until ‘(‘: Output = xy+, Stack =
  6. Read ‘*’ and push onto stack: Stack = *
  7. Read ‘z’ and add to output: Output = xy+z
  8. Read ‘/’ and push onto stack: Stack = *, /
  9. Read ‘a’ and add to output: Output = xy+za
  10. Read ‘-‘ and pop ‘/’ and ‘*’ to output, push ‘-‘: Output = xy+z*/a, Stack = –
  11. Read ‘b’ and add to output: Output = xy+z*/ab
  12. Finish by popping operators from stack to output: Output = xy+z*/a-b

Final Result: xy+z*/a-b

Example 15: m ^ n * (o + p)

  1. Read ‘m’ and add to output: Output = m
  2. Read ‘^’ and push onto stack: Stack = ^
  3. Read ‘n’ and add to output: Output = mn
  4. Read ‘*’ and push onto stack: Stack = ^, *
  5. Read ‘(‘ and push onto stack: Stack = ^, *, (
  6. Read ‘o’ and add to output: Output = mno
  7. Read ‘+’ and push onto stack: Stack = ^, *, (, +
  8. Read ‘p’ and add to output: Output =mnop
  9. Read ‘)’ and pop operators until ‘(‘: Output = mnop+*, Stack = ^
  10. Finish by popping operators from stack to output: Output = mnop+*^

Final Result: mnop+*^

Example 16: (p – q) / (r + s)

  1. Read ‘(‘ and push onto stack: Stack = (
  2. Read ‘p’ and add to output: Output = p
  3. Read ‘-‘ and push onto stack: Stack = (, –
  4. Read ‘q’ and add to output: Output = pq
  5. Read ‘)’ and pop operators until ‘(‘: Output = pq-, Stack =
  6. Read ‘/’ and push onto stack: Stack = /
  7. Repeat steps 1-5 for second (r + s): Output = pq-rs+
  8. Finish by popping operators from stack to output: Output = pq-rs+/

Final Result: pq-rs+/

Example 17: u * v + w / x – y

  1. Read ‘u’ and add to output: Output = u
  2. Read ‘*’ and push onto stack: Stack = *
  3. Read ‘v’ and add to output: Output = uv
  4. Read ‘+’ and pop ‘*’ to output, push ‘+’: Output = uv*, Stack = +
  5. Read ‘w’ and add to output: Output = uv*w
  6. Read ‘/’ and push onto stack: Stack = +, /
  7. Read ‘x’ and add to output: Output = uv*wx
  8. Read ‘-‘ and pop ‘/’ and ‘+’ to output, push ‘-‘: Output = uv*wx/+, Stack = –
  9. Read ‘y’ and add to output: Output = uv*wx/+y
  10. Finish by popping operators from stack to output: Output = uv*wx/+y-

Final Result: uv*wx/+y-

Leave a Comment

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

Scroll to Top