- 2075 Baisakh · 2+4+4 marks
Infix to postfix and evaluation
Short answer
Stack is a LIFO structure. Infix to postfix: scan, output operands, push operators respecting precedence; brackets used. Evaluating AB+C*DEFG-*+ with given values: push operands; on operator, pop two, apply. Two values remain: 50 and 1 (expression lacks a final operator).
Define stack. How to convert infix to postfix notation? Explain with example. Evaluate the postfix expression AB+CDEFG-+ with status of stack where A = 2, B = 3, C = 10, D = 5, E = 2, F = 4 and G = 6.
A stack is a linear data structure in which insertion (push) and deletion (pop) take place only at one end called the top. The last element inserted is the first to be removed, so it follows LIFO (Last In First Out).
Converting infix to postfix
Postfix (reverse Polish) places each operator after its operands, so no parentheses or precedence rules are needed when evaluating. Rules with a stack:
- Scan the infix expression left to right.
- Operand: append it to the postfix output.
(: push it on the stack.): pop operators to the output until(is found; discard both parentheses.- Operator: while the stack top is an operator with higher or equal precedence (for
^, only higher, since^is right-associative), pop it to the output; then push the scanned operator. - At the end, pop all remaining operators to the output.
Precedence (high to low): ^, then * /, then + -.
Example: (A + B) * C - D / E
| Step | Symbol | Stack (bottom → top) | Postfix output |
|---|---|---|---|
| 1 | ( | ( | - |
| 2 | A | ( | A |
| 3 | + | (+ | A |
| 4 | B | (+ | AB |
| 5 | ) | (empty) | AB+ |
| 6 | * | * | AB+ |
| 7 | C | * | AB+C |
| 8 | - | - | AB+C* |
| 9 | D | - | AB+C*D |
| 10 | / | -/ | AB+C*D |
| 11 | E | -/ | AB+C*DE |
| end | (pop all) | (empty) | AB+C*DE/- |
Answer: AB+C*DE/-
Evaluating the postfix expression AB+C*DEFG-*+
Values: A=2, B=3, C=10, D=5, E=2, F=4, G=6. Rule: operand → push; operator → pop two (first pop is the right operand), apply, push the result.
| Step | Symbol | Action | Stack (bottom → top) |
|---|---|---|---|
| 1 | A | push 2 | 2 |
| 2 | B | push 3 | 2, 3 |
| 3 | + | pop 3 (right), 2 (left); 2 + 3 = 5; push | 5 |
| 4 | C | push 10 | 5, 10 |
| 5 | * | pop 10 (right), 5 (left); 5 * 10 = 50; push | 50 |
| 6 | D | push 5 | 50, 5 |
| 7 | E | push 2 | 50, 5, 2 |
| 8 | F | push 4 | 50, 5, 2, 4 |
| 9 | G | push 6 | 50, 5, 2, 4, 6 |
| 10 | - | pop 6 (right), 4 (left); 4 - 6 = -2; push | 50, 5, 2, -2 |
| 11 | * | pop -2 (right), 2 (left); 2 * -2 = -4; push | 50, 5, -4 |
| 12 | + | pop -4 (right), 5 (left); 5 + -4 = 1; push | 50, 1 |
Result: the single scan finishes with two values on the stack (50 and 1), because the expression as given has 7 operands but only 5 operators (6 are needed). The sub-results are (A+B)C = 50 and D + E(F−G) = 1. If the intended expression had one more operator at the end (for example +), the value would be 50 + 1 = 51.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Importance of postfix; convert expression · Data Structure and Algorithm, chapter 2
- Convert expression to prefix and postfix · Data Structure and Algorithm, chapter 2
- Convert expression to prefix and postfix · Data Structure and Algorithm, chapter 2
- Evaluate postfix expression 574-*8/4+ · Data Structure and Algorithm, chapter 2
- Algorithm for infix to postfix conversion · Data Structure and Algorithm, chapter 2 · asked 3×
- Convert and evaluate A/B-(C+D)*E · Data Structure and Algorithm, chapter 2 · asked 2×