MCS-208 · Unit 4

Stacks

Section 5 of 7

Block 2 · Unit 4Stacks14 min · Section 5 of 7

4.5Expression Notations and Infix to Postfix Conversion

Covers IGNOU MCS-208 §4.5

The study material lists expression conversion and evaluation as the main applications of stacks. To evaluate an expression like A + B * C, a program must know that * is done before +. Converting the expression to postfix form removes the need for precedence rules and brackets, and a stack does the conversion.

Three notations

NotationOperator positionExample for (A+B)×C(A + B) \times C
InfixBetween the operands(A + B) * C
Prefix (Polish)Before the operands* + A B C
Postfix (reverse Polish)After the operandsA B + C *

Prefix and postfix need no brackets: the position of each operator already says which operands it applies to. They were introduced by the Polish logician Jan Łukasiewicz, hence “Polish notation”.

Precedence and associativity

OperatorPrecedenceAssociativity
^ (power)3 (highest)Right to left: A^B^C = A^(B^C)
* /2Left to right: A/B*C = (A/B)*C
+ -1Left to right: A-B+C = (A-B)+C

The conversion algorithm

Scan the infix expression from left to right:

  1. Operand: append it to the output.
  2. (: push it.
  3. ): pop operators to the output until a ( is on top; pop and discard the (.
  4. Operator op: while the top of the stack is an operator that has higher precedence than op, or equal precedence with op left-associative, pop it to the output. Then push op.
  5. End of input: pop all remaining operators to the output.

Each symbol is pushed and popped at most once, so the conversion is O(n)O(n) for an expression of nn symbols.

Key takeaways

  • Infix: operator between operands; prefix: before; postfix: after. Prefix and postfix need no brackets.
  • Precedence: ^ > * / > + -; ^ is right-associative, the others left-associative.
  • Conversion: operands to output, operators wait on a stack, ) pops back to (.
  • Each symbol is pushed and popped at most once: O(n)O(n).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the postfix form of (A + B) * (C - D)?

Show answer

Answer: B. A B + C D - *

Each bracket becomes its own postfix group, A B + and C D -, and the * comes last. * + A B - C D is the prefix form.

Question 2 · multiple choice

What is the prefix form of A * B + C / D?

Show answer

Answer: C. + * A B / C D

Fully bracketed: (A * B) + (C / D); move each operator before its operands. A B * C D / + is the postfix form.

Question 3 · multiple choice

During the conversion of A - B * C + D, which operators are popped when the + is read?

Show answer

Answer: D. * and then -

The stack holds - *. * has higher precedence than +, and - has equal precedence and is left-associative, so both are popped. The postfix is A B C * - D +.

Question 4 · multiple choice

What is the postfix form of A ^ B ^ C?

Show answer

Answer: B. A B C ^ ^

Exponentiation is right-associative: A ^ (B ^ C), so B C ^ is formed first. A B ^ C ^ would be (A ^ B) ^ C.

Question 5 · multiple choice

Why are postfix expressions convenient for computers?

Show answer

Answer: C. They need no brackets or precedence rules, so they can be evaluated in one left-to-right pass with a stack.

The order of the symbols already fixes the order of the operations. The number of operands and operators is the same as in infix.

Question 6 · short answer

Convert A * (B + C) - D / E to postfix using a stack, showing the stack after each symbol.

Show worked answer

(symbol: stack | output) A: empty | A; *: * | A; (: * ( | A; B: * ( | A B; +: * ( + | A B; C: * ( + | A B C; ): * | A B C +; -: - | A B C + * (the * has higher precedence and is popped); D: - | A B C + * D; /: - / | A B C + * D (/ is higher than -, so it is pushed); E: - / | A B C + * D E; end: empty | A B C + * D E / -. Postfix: A B C + * D E / -.

↑ ↓ move↵ openTab switch type