infix to postfix -8

Example 29: (a AND b) OR (NOT c AND d)

  1. Read ‘(‘ and push onto stack: Stack = (
  2. Read ‘a’ and add to output: Output = a
  3. Read ‘AND’ and push onto stack: Stack = (, AND
  4. Read ‘b’ and add to output: Output = ab
  5. Read ‘)’ and pop operators until ‘(‘: Output = abAND, Stack =
  6. Read ‘OR’ and push onto stack: Stack = OR
  7. Read ‘(‘ and push onto stack: Stack = OR, (
  8. Read ‘NOT’ and push onto stack: Stack = OR, (, NOT
  9. Read ‘c’ and add to output: Output = abANDc
  10. Read ‘AND’ and push onto stack: Stack = OR, (, NOT, AND
  11. Read ‘d’ and add to output: Output = abANDcNOTd
  12. Read ‘)’ and pop operators until ‘(‘: Output = abANDcNOTdAND, Stack = OR
  13. Finish by popping operators from stack to output: Output = abANDcNOTdANDOR

Final Result: abANDcNOTdANDOR

Example 30: NOT (a AND (b OR c) AND d)

  1. Read ‘NOT’ and push onto stack: Stack = NOT
  2. Read ‘(‘ and push onto stack: Stack = NOT, (
  3. Read ‘a’ and add to output: Output = a
  4. Read ‘AND’ and push onto stack: Stack = NOT, (, AND
  5. Read ‘(‘ and push onto stack: Stack = NOT, (, AND, (
  6. Read ‘b’ and add to output: Output = ab
  7. Read ‘OR’ and push onto stack: Stack = NOT, (, AND, (, OR
  8. Read ‘c’ and add to output: Output = abc
  9. Read ‘)’ and pop operators until ‘(‘: Output = abcOR, Stack = NOT, (, AND
  10. Read ‘AND’ and push onto stack: Stack = NOT, (, AND, AND
  11. Read ‘d’ and add to output: Output = abcORd
  12. Read ‘)’ and pop operators until ‘(‘: Output = abcORdANDAND, Stack = NOT
  13. Finish by popping operators from stack to output: Output = abcORdANDANDNOT

Final Result: abcORdANDANDNOT

Example 31: (p AND q) OR NOT (r AND s OR t)

  1. Repeat steps for inside expression (p AND q): Output = pqAND
  2. Read ‘OR’ and push onto stack: Stack = OR
  3. Read ‘NOT’ and push onto stack: Stack = OR, NOT
  4. Read ‘(‘ and push onto stack: Stack = OR, NOT, (
  5. Read ‘r’ and add to output: Output = pqANDr
  6. Read ‘AND’ and push onto stack: Stack = OR, NOT, (, AND
  7. Read ‘s’ and add to output: Output = pqANDrs
  8. Read ‘OR’ and push onto stack: Stack = OR, NOT, (, AND, OR
  9. Read ‘t’ and add to output: Output = pqANDrst
  10. Read ‘)’ and pop operators until ‘(‘: Output = pqANDrstORAND, Stack = OR, NOT
  11. Finish by popping operators from stack to output: Output = pqANDrstORANDNOTOR

Final Result: pqANDrstORANDNOTOR

Example 32: (a OR b) AND (NOT c OR d AND e) OR f

  1. Repeat steps for inside expression (a OR b): Output = abOR
  2. Read ‘AND’ and push onto stack: Stack = AND
  3. Read ‘(‘ and push onto stack: Stack = AND, (
  4. Read ‘NOT’ and push onto stack: Stack = AND, (, NOT
  5. Read ‘c’ and add to output: Output = abORc
  6. Read ‘OR’ and push onto stack: Stack = AND, (, NOT, OR
  7. Read ‘d’ and add to output: Output = abORcNOTd
  8. Read ‘AND’ and push onto stack: Stack = AND, (, NOT, OR, AND

Leave a Comment

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

Scroll to Top