Example 14: (x + y) * z / a – b
- Read ‘(‘ and push onto stack: Stack = (
- Read ‘x’ and add to output: Output = x
- Read ‘+’ and push onto stack: Stack = (, +
- Read ‘y’ and add to output: Output = xy
- Read ‘)’ and pop operators until ‘(‘: Output = xy+, Stack =
- Read ‘*’ and push onto stack: Stack = *
- Read ‘z’ and add to output: Output = xy+z
- Read ‘/’ and push onto stack: Stack = *, /
- Read ‘a’ and add to output: Output = xy+za
- Read ‘-‘ and pop ‘/’ and ‘*’ to output, push ‘-‘: Output = xy+z*/a, Stack = –
- Read ‘b’ and add to output: Output = xy+z*/ab
- 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)
- Read ‘m’ and add to output: Output = m
- Read ‘^’ and push onto stack: Stack = ^
- Read ‘n’ and add to output: Output = mn
- Read ‘*’ and push onto stack: Stack = ^, *
- Read ‘(‘ and push onto stack: Stack = ^, *, (
- Read ‘o’ and add to output: Output = mno
- Read ‘+’ and push onto stack: Stack = ^, *, (, +
- Read ‘p’ and add to output: Output =mnop
- Read ‘)’ and pop operators until ‘(‘: Output = mnop+*, Stack = ^
- Finish by popping operators from stack to output: Output = mnop+*^
Final Result: mnop+*^
Example 16: (p – q) / (r + s)
- Read ‘(‘ and push onto stack: Stack = (
- Read ‘p’ and add to output: Output = p
- Read ‘-‘ and push onto stack: Stack = (, –
- Read ‘q’ and add to output: Output = pq
- Read ‘)’ and pop operators until ‘(‘: Output = pq-, Stack =
- Read ‘/’ and push onto stack: Stack = /
- Repeat steps 1-5 for second (r + s): Output = pq-rs+
- Finish by popping operators from stack to output: Output = pq-rs+/
Final Result: pq-rs+/
Example 17: u * v + w / x – y
- Read ‘u’ and add to output: Output = u
- Read ‘*’ and push onto stack: Stack = *
- Read ‘v’ and add to output: Output = uv
- Read ‘+’ and pop ‘*’ to output, push ‘+’: Output = uv*, Stack = +
- Read ‘w’ and add to output: Output = uv*w
- Read ‘/’ and push onto stack: Stack = +, /
- Read ‘x’ and add to output: Output = uv*wx
- Read ‘-‘ and pop ‘/’ and ‘+’ to output, push ‘-‘: Output = uv*wx/+, Stack = –
- Read ‘y’ and add to output: Output = uv*wx/+y
- Finish by popping operators from stack to output: Output = uv*wx/+y-
Final Result: uv*wx/+y-