MCS-208 · Unit 4

Stacks

Section 6 of 7

Block 2 · Unit 4Stacks8 min · Section 6 of 7

4.6Evaluating Postfix Expressions

Covers IGNOU MCS-208 §4.5

Once an expression is in postfix form, evaluating it is easy: read the symbols from left to right, keep the operands on a stack, and apply each operator to the top two operands. This is the second step of the two-step method described in the study material.

The algorithm

Scan the postfix expression from left to right:

  1. Operand: push it.
  2. Operator op: pop the top item into b, pop the next item into a, compute a op b, and push the result.
  3. End: the stack must contain exactly one item; that is the value. Any other number of items means the expression was invalid.

The first item popped is the right operand. For -, / and ^ the order matters: 8 2 / means 8/2=48 / 2 = 4, not 2/82 / 8.

Putting the two steps together

To evaluate an infix expression such as (2 + 3) * 4: convert it to postfix (2 3 + 4 *), then evaluate the postfix (20). Each step is one pass with a stack, so the whole evaluation is O(n)O(n). A prefix expression can be evaluated the same way by scanning from right to left; then the first item popped is the left operand.

Key takeaways

  • Operand: push. Operator: pop b, pop a, push a op b.
  • The first value popped is the right operand.
  • A valid postfix expression leaves exactly one value; too few or too many operands mean an error.
  • Infix evaluation = infix → postfix + postfix evaluation, both 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 value of the postfix expression 5 1 2 + 4 * + 3 -?

Show answer

Answer: A. 14

1 + 2 = 3; 3 × 4 = 12; 5 + 12 = 17; 17 − 3 = 14. Stopping before the final - would give 17.

Question 2 · multiple choice

While evaluating 9 3 /, the evaluator pops 3 and then 9. What does it compute?

Show answer

Answer: B. 9 / 3

The first value popped (3) is the right operand, so the result is 9 / 3 = 3.

Question 3 · multiple choice

What does this program print?

C · q4-eval.c
#include <stdio.h>

int main(void)
{
    int st[10], top = -1;
    const char *e = "23*54*+9-";     /* single-digit operands */
    for (const char *p = e; *p; p++) {
        if (*p >= '0' && *p <= '9') {
            st[++top] = *p - '0';
        } else {
            int b = st[top--], a = st[top--];
            st[++top] = (*p == '+') ? a + b : (*p == '-') ? a - b : a * b;
        }
    }
    printf("%d\n", st[top]);
    return 0;
}
Show answer

Answer: C. 17

2 × 3 = 6; 5 × 4 = 20; 6 + 20 = 26; 26 − 9 = 17. −17 would come from computing b - a instead of a - b.

Question 4 · multiple choice

Which postfix expression is invalid?

Show answer

Answer: C. 1 + 2

When + is read, only one operand is on the stack, so it cannot be applied. A single operand such as 7 is a valid expression whose value is 7.

Question 5 · multiple choice

After all tokens of a postfix expression are processed, the stack holds two values. What does this mean?

Show answer

Answer: D. The expression had too many operands (or too few operators), so it is invalid.

Each binary operator reduces the stack by one item, so a valid expression with kk operands has k−1k - 1 binary operators and ends with one item.

Question 6 · short answer

Evaluate the postfix expression 8 2 3 ^ / 2 3 * + 5 1 * - showing the stack after each symbol.

Show worked answer

8 → [8]; 2 → [8 2]; 3 → [8 2 3]; ^: 2^3 = 8 → [8 8]; /: 8 / 8 = 1 → [1]; 2 → [1 2]; 3 → [1 2 3]; *: 2 × 3 = 6 → [1 6]; +: 1 + 6 = 7 → [7]; 5 → [7 5]; 1 → [7 5 1]; *: 5 × 1 = 5 → [7 5]; -: 7 − 5 = 2 → [2]. Value = 2.

↑ ↓ move↵ openTab switch type