Q.Convert the following infix notations to postfix notations, showing stack and string contents at each step.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Convert two infix expressions to postfix using the stack-based algorithm, showing the operator stack and output string at each step.
Infix notation places operators between operands (), which is natural for humans but requires parentheses and precedence rules. Postfix (Reverse Polish Notation) places operators after their operands (), eliminating ambiguity — no parentheses needed, and evaluation proceeds strictly left-to-right using a stack.
The conversion algorithm uses an operator stack to respect precedence and associativity:
- Scan the infix expression left to right.
- Operand → append directly to the output string.
- Left parenthesis
(→ push onto stack. - Right parenthesis
)→ pop operators to output until(is encountered; discard the(. - Operator → pop operators from stack to output while they have higher or equal precedence (for left-associative operators), then push the current operator.
- End of expression → pop all remaining operators to output.
Precedence: * and / (higher) > + and - (lower). All are left-associative.
(a) A + B - C * D
| Step | Symbol | Stack (top→bottom) | Output String | Action |
|---|---|---|---|---|
| 1 | A | (empty) | A | Operand → output |
| 2 | + | + | A | Operator → push (stack empty) |
| 3 | B | + | A B | Operand → output |
| 4 | - | - | A B + | Pop + (same precedence), push - |
| 5 | C | - | A B + C | Operand → output |
| 6 | * | * - | A B + C | * higher than - → push |
| 7 | D | * - | A B + C D | Operand → output |
| End | (empty) | A B + C D * - | Pop *, then - |
Postfix: A B + C D * -
At step 4, - has the same precedence as + and is left-associative, so we pop + before pushing -. At step 6, * has higher precedence than -, so it stays on the stack above -.
(b) A * (( C + D)/E)
| Step | Symbol | Stack (top→bottom) | Output String | Action |
|---|---|---|---|---|
| 1 | A | (empty) | A | Operand → output |
| 2 | * | * | A | Operator → push |
| 3 | ( | ( * | A | Left paren → push |
| 4 | ( | ( ( * | A | Left paren → push |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.