PCCST303 · Module 1: Basic Concepts of Data Structures

Postfix Evaluation

Postfix notation places every operator right after its operands, for example A B C * + for A + B * C. That ordering already encodes exactly which operation happens first, so evaluating it doesn't need any precedence rules at all, just a single left-to-right scan with a stack.

The evaluation algorithm

EvaluatePostfix(postfix)
 
1. Create empty stack S
 
2. For each token in postfix (left to right):
 
   a) If token is an operand:
      Push its value onto S
 
   b) If token is an operator (+, -, *, /):
      operand2 = Pop from S   (this is popped first)
      operand1 = Pop from S   (this is popped second)
      result = operand1 operator operand2
      Push result onto S
 
3. answer = Pop from S (the final result)
4. Return answer

Every operand gets pushed as it's read. Every operator pops the two most recent values, combines them, and pushes the result back, so the stack always holds exactly the intermediate results still waiting to be combined.

Why operand order matters

Key Idea

For addition and multiplication the pop order doesn't matter, but for subtraction and division it does. The second value popped is always the left operand, and the first value popped is always the right operand.

postfix: A B -
 
Pop B first  → this is the right operand
Pop A second → this is the left operand
Result = A - B, not B - A

Example

Evaluate 5 3 -:

Push 5:  Stack: [5]
Push 3:  Stack: [5, 3]
 
Operator -:
  operand2 = Pop() = 3   (right operand)
  operand1 = Pop() = 5   (left operand)
  result = 5 - 3 = 2
 
Result: 2

Getting the pop order backwards here would silently compute 3 - 5 = -2 instead, the wrong answer, with no error to catch it. This is the single most common mistake in postfix evaluation, worth double-checking every time.

Simple worked example

Evaluate 2 3 * 5 +:

TokenActionStack
2Push2
3Push2, 3
*Pop 3, pop 2, push 2×3 = 66
5Push6, 5
+Pop 5, pop 6, push 6+5 = 1111

At the end, exactly one value remains on the stack: the final answer, 11.

KTU PYQ: full worked evaluation

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 is the second half of the question, evaluating the postfix expression from the Infix to Postfix conversion. Given A=3, B=2, C=4, D=2, E=5, F=8, G=4, substituting into A B C * D / + E F G / - - gives:

3 2 4 * 2 / + 5 8 4 / - -
TokenStackAction
3[3]Push
2[3, 2]Push
4[3, 2, 4]Push
*[3, 8]2 × 4 = 8
2[3, 8, 2]Push
/[3, 4]8 / 2 = 4
+[7]3 + 4 = 7
5[7, 5]Push
8[7, 5, 8]Push
4[7, 5, 8, 4]Push
/[7, 5, 2]8 / 4 = 2
-[7, 3]5 - 2 = 3
-[4]7 - 3 = 4

Final result: 4

Key Idea

Both - steps here rely on getting the operand order right, popped-first is the right operand. The first - computes 5 - 2 = 3 (not 2 - 5), and the final - computes 7 - 3 = 4 (not 3 - 7), matching the structure of the original expression: (A+B*C/D) - (E-F/G).

Practice

Q1.Evaluate 4 5 6 * - 3 +.
Show answer

-23

TokenActionStack
4Push4
5Push4, 5
6Push4, 5, 6
*Pop 6, pop 5, push 5 * 6 = 304, 30
-Pop 30 (right), pop 4 (left), push 4 - 30 = -26-26
3Push-26, 3
+Pop 3, pop -26, push -26 + 3 = -23-23
Q2.Evaluate 10 2 3 * - 4 /.
Show answer

1

TokenActionStack
10Push10
2Push10, 2
3Push10, 2, 3
*Pop 3, pop 2, push 2 * 3 = 610, 6
-Pop 6 (right), pop 10 (left), push 10 - 6 = 44
4Push4, 4
/Pop 4 (right), pop 4 (left), push 4 / 4 = 11
Q3.Evaluate 5 1 2 + 4 * + 3 -.
Show answer

14

TokenActionStack
5Push5
1Push5, 1
2Push5, 1, 2
+Pop 2, pop 1, push 1 + 2 = 35, 3
4Push5, 3, 4
*Pop 4, pop 3, push 3 * 4 = 125, 12
+Pop 12, pop 5, push 5 + 12 = 1717
3Push17, 3
-Pop 3 (right), pop 17 (left), push 17 - 3 = 1414

Quick recall

  • Postfix evaluation is a single left-to-right scan: push operands, and on each operator pop two values, combine them, and push the result back.
  • The first value popped is the right operand, the second value popped is the left operand, this matters for subtraction and division specifically.
  • At the end of the scan, the stack holds exactly one value, the result of the whole expression.

That covers every topic in Module 1. For a focused list of the questions most likely to appear from this module, see Most Expected Questions.