While conversion of an Infix notation to its equivalent Prefix/Postfix notation,
only operators are Pushed onto the Stack.
When evaluating any Postfix expression using Stack, only operands are
PUSHed onto it.
Good Example
We’ll take this infix expression:
(A+B)∗C(A + B) * C(A+B)∗C
and assume values: A = 2, B = 3, C = 4.
1️⃣ Infix Expression
(A + B) * C
2️⃣ Convert Infix → Postfix
Scan ( → push
Scan A → operand → postfix = A
Scan + → push
Scan B → operand → postfix = AB
Scan ) → pop till ( → postfix = AB+
Scan * → push
Scan C → operand → postfix = AB+C
End of input → pop stack → postfix = AB+C*
✅ Postfix Expression = AB+C*
3️⃣ Evaluate Postfix (AB+C*)
Stack = [ ]
Read A=2 → push → [2]
Read B=3 → push → [2, 3]
Read + → pop(3, 2) → 2+3=5 → push → [5]
Read C=4 → push → [5, 4]
Read * → pop(4, 5) → 5*4=20 → push → [20]
✅ Final Result = 20
🔑 Summary
Infix: (A + B) * C
Postfix: AB+C*
Evaluation Result (A=2, B=3, C=4): 20
Important Rule
A+B*C
Step by step:
A → operand → goes directly to postExp → A
+ → operator → stack is empty, so push + → stack = [+]
B → operand → goes directly to postExp → AB
* → operator → compare with top of stack (+)
Precedence(*) > Precedence(+)
So we push * → stack = [+, *]
C → operand → goes to postExp → ABC
End of input → pop remaining stack → ABC*+
✅ Final postfix:
Copy code
ABC*+
Another Example (where popping happens)
Infix expression:
css
Copy code
A*B+C
A→A
* → push → stack = [*]
B → AB
+ → compare with * on top
Precedence(+) < Precedence(*)
So pop * → AB*
Now stack empty → push + → stack = [+]
C → AB*C
End → pop stack → AB*C+
✅ Final postfix:
mathematica
Copy code
AB*C+