Covert infix notation to postfix notation:
Example:
Convert 4+2*3-6/2 to postfix expression.
Principle: store operators
in a stack.
1. read 4 and write 4.
2. read + and push it to a stack.
3. read 2 and write 2.
4. read * and push it to the stack.
5. read 3 and write 3.
6. pop * out and write it.
7. pop + out and write it.
| Postfix: 4 2 3 * + |
stack: empty |
8. read - and push it to the stack.
| Postfix: 4 2 3 * + |
stack:
|
9. read 6 and write 6.
| Postfix: 4 2 3 * + 6 |
stack:
|
10. read / and push it to the stack.
| Postfix: 4 2 3 * + 6 |
stack:
|
11. read 2 and write 2.
| Postfix: 4 2 3 * + 6 2 |
stack:
|
12. pop / and write it.
| Postfix: 4 2 3 * + 6 2 / |
stack:
|
13. pop - and write it.
| Postfix: 4 2 3 * +
6 2 / - |
stack: empty |