PCCST303 · Module 1: Basic Concepts of Data Structures
Infix to Postfix
Humans commonly write expressions like A + B * C, with the operator sitting between its operands. That's called infix notation. It's natural to read, but a computer evaluating it has to work out precedence and parentheses on the fly. Postfix notation sidesteps that by writing every operator right after its operands, so evaluation becomes a simple left-to-right scan with no precedence rules needed at evaluation time. Converting from one to the other is where a stack comes in.
Infix and postfix notation
Definition
Infix notation places an operator between its two operands, for example A + B. Postfix notation places the operator immediately after its operands, for example A B +.
A + B → A B +
A * B → A B *
A + B * C → A B C * +Postfix doesn't need parentheses to preserve evaluation order, the position of each operator relative to its operands already encodes exactly which operations happen first. That's precisely what makes it easy for a computer to evaluate directly, covered next.
Operator precedence
Converting infix to postfix correctly requires knowing which operators bind tighter than others:
Highest
^
* /
+ -
LowestParentheses override precedence explicitly, forcing whatever they enclose to be evaluated as a unit. When two operators have equal precedence, associativity (left-to-right, for + - * /) decides which one is applied first.
The conversion algorithm
A stack holds operators and open parentheses temporarily, while the output string builds up the final postfix expression, one token at a time.
InfixToPostfix(infix)
1. Create empty stack S and empty output string P
2. For each token in infix (left to right):
a) If token is operand:
Add token to P
b) If token is '(':
Push token to S
c) If token is ')':
Pop from S to P until '(' is found
Discard the '('
d) If token is operator (+, -, *, /):
While S is not empty
AND top(S) is not '('
AND precedence(top(S)) >= precedence(token):
Pop from S, append to P
Push token to S
3. After scanning all tokens:
While S is not empty:
Pop from S, append to P
4. Return PKey Idea
The core rule is step 2d: whenever an operator is read, first pop and output every operator already on the stack that has equal or higher precedence, then push the new operator. This is what enforces left-to-right evaluation among equal-precedence operators and makes sure higher-precedence operators end up applied first in the output.
An operand always goes straight to the output, it never touches the stack, since operands don't need to wait for anything, only operators do.
Simple worked example
Convert A + B * C:
| Token | Stack | Output | What happened |
|---|---|---|---|
| A | (empty) | A | Operand, straight to output |
| + | + | A | Stack empty, just push |
| B | + | AB | Operand, straight to output |
| * | + * | AB | * has higher precedence than +, nothing to pop, push * |
| C | + * | ABC | Operand, straight to output |
| end | (empty) | ABC*+ | Pop remaining stack: * then + |
Result: A + B * C becomes ABC*+.
KTU PYQ: full worked conversion
KTU PYQ
KTU PYQ, 2025, 6 marks: "Convert the infix expression (A+B*C/D)-(E-F/G) to postfix form using a stack. Evaluate the resultant postfix expression by replacing A, B, C, D, E, F, G with 3, 2, 4, 2, 5, 8 and 4."
This question has two parts: converting to postfix (this page) and evaluating the result (the Postfix Evaluation page, which continues from the postfix expression produced here).
| Token | Stack | Output | Action |
|---|---|---|---|
| ( | ( | Push | |
| A | ( | A | Operand → output |
| + | (+ | A | Push |
| B | (+ | A B | Operand → output |
| * | (+* | A B | Push (* > +) |
| C | (+* | A B C | Operand → output |
| / | (+/ | A B C * | Same precedence as *, pop * first |
| D | (+/ | A B C * D | Operand → output |
| ) | A B C * D / + | Pop until ( | |
| - | - | A B C * D / + | Push - |
| ( | -( | A B C * D / + | Push ( |
| E | -( | A B C * D / + E | Operand → output |
| - | -(- | A B C * D / + E | Push - |
| F | -(- | A B C * D / + E F | Operand → output |
| / | -(-/ | A B C * D / + E F | Push (/ > -) |
| G | -(-/ | A B C * D / + E F G | Operand → output |
| ) | - | A B C * D / + E F G / - | Pop until ( |
| (end) | A B C * D / + E F G / - - | Pop remaining stack |
Postfix expression: A B C * D / + E F G / - -
Key Idea
For a 6-mark question like this, showing the full token-by-token trace table is what earns marks, not just the final answer. Notice the / after C pops * first (same precedence, left-to-right), and the second / (after F) doesn't pop the earlier -, since - is behind a ( that hasn't closed yet, operators never pop past an open parenthesis.
Practice
Q1.Convert A + B * (C - D / E) to postfix.Show answer
A + B * (C - D / E) to postfix.ABCDE/-*+
| Token | Stack | Output | What happened |
|---|---|---|---|
| A | (empty) | A | Operand → output |
| + | + | A | Stack empty, just push |
| B | + | AB | Operand → output |
| * | + * | AB | Push (* > +) |
| ( | + * ( | AB | Push |
| C | + * ( | ABC | Operand → output |
| - | + * ( - | ABC | Push (top is () |
| D | + * ( - | ABCD | Operand → output |
| / | + * ( - / | ABCD | Push (/ > -) |
| E | + * ( - / | ABCDE | Operand → output |
| ) | + * | ABCDE/- | Pop until (: / then - |
| end | (empty) | ABCDE/-*+ | Pop remaining stack: * then + |
Q2.Convert (A - B / C) * (D + E * F) to postfix.Show answer
(A - B / C) * (D + E * F) to postfix.ABC/-DEF*+*
| Token | Stack | Output | What happened |
|---|---|---|---|
| ( | ( | Push | |
| A | ( | A | Operand → output |
| - | ( - | A | Push (top is () |
| B | ( - | AB | Operand → output |
| / | ( - / | AB | Push (/ > -) |
| C | ( - / | ABC | Operand → output |
| ) | (empty) | ABC/- | Pop until (: / then - |
| * | * | ABC/- | Stack empty, just push |
| ( | * ( | ABC/- | Push |
| D | * ( | ABC/-D | Operand → output |
| + | * ( + | ABC/-D | Push (top is () |
| E | * ( + | ABC/-DE | Operand → output |
| * | * ( + * | ABC/-DE | Push (* > +) |
| F | * ( + * | ABC/-DEF | Operand → output |
| ) | * | ABC/-DEF*+ | Pop until (: * then + |
| end | (empty) | ABC/-DEF*+* | Pop remaining * |
Q3.Convert A + (B - C) * D / E - F to postfix.Show answer
A + (B - C) * D / E - F to postfix.ABC-D*E/+F-
| Token | Stack | Output | What happened |
|---|---|---|---|
| A | (empty) | A | Operand → output |
| + | + | A | Stack empty, just push |
| ( | + ( | A | Push |
| B | + ( | AB | Operand → output |
| - | + ( - | AB | Push (top is () |
| C | + ( - | ABC | Operand → output |
| ) | + | ABC- | Pop until (: - |
| * | + * | ABC- | Push (* > +) |
| D | + * | ABC-D | Operand → output |
| / | + / | ABC-D* | Pop * (equal-or-higher), stop at + (lower), push / |
| E | + / | ABC-D*E | Operand → output |
| - | - | ABC-D*E/+ | Pop / then +, push - |
| F | - | ABC-D*E/+F | Operand → output |
| end | (empty) | ABC-D*E/+F- | Pop remaining - |
Quick recall
- Infix places the operator between operands; postfix places it right after them, removing the need for parentheses or precedence rules at evaluation time.
- The conversion algorithm uses a stack to hold operators and
(temporarily, and an output string that only ever grows, operands go straight in, operators wait their turn. - On reading an operator, pop and output every stack operator with equal or higher precedence first, then push the new one, this is what enforces correct precedence in the output.
)pops everything back to the matching(and discards it; anything still on the stack at the end gets popped to the output, in order.