Skip to content
Exercises · Q6

Q.Convert the following infix notations to postfix notations, showing stack and string contents at each step.

(a) A + B - C * D
(b) A * (( C + D)/E)
Uttar Pradesh UpmspTextbookSubjective· 3mImportance★★★★★
62% · 8/13 Questions
🔒 Locked · start free trial →

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 (A+BA + B), which is natural for humans but requires parentheses and precedence rules. Postfix (Reverse Polish Notation) places operators after their operands (A B +A \, B \, +), 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:

  1. Scan the infix expression left to right.
  2. Operand → append directly to the output string.
  3. Left parenthesis ( → push onto stack.
  4. Right parenthesis ) → pop operators to output until ( is encountered; discard the (.
  5. Operator → pop operators from stack to output while they have higher or equal precedence (for left-associative operators), then push the current operator.
  6. End of expression → pop all remaining operators to output.

Precedence: * and / (higher) > + and - (lower). All are left-associative.


(a) A + B - C * D

StepSymbolStack (top→bottom)Output StringAction
1A(empty)AOperand → output
2++AOperator → push (stack empty)
3B+A BOperand → output
4--A B +Pop + (same precedence), push -
5C-A B + COperand → output
6** -A B + C* higher than - → push
7D* -A B + C DOperand → output
End(empty)A B + C D * -Pop *, then -

Postfix: A B + C D * -

Note

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)

StepSymbolStack (top→bottom)Output StringAction
1A(empty)AOperand → output
2**AOperator → push
3(( *ALeft paren → push
4(( ( *ALeft 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.