Once an expression is in postfix form, a single scan with a stack gives its value.
Algorithm
Scan the postfix expression from left to right. When an operand is read, push it onto the stack. When an operator is read, pop the top two elements: the first popped is the right operand B, and the second is the left operand A. Compute A operator B and push the result. When the scan ends, the stack holds one value, which is the result. If the stack does not hold exactly one value at the end, or an operator finds fewer than two operands, the expression is invalid. The algorithm takes O(n) time for an expression of n symbols.
Definition: Postfix evaluation. The process of computing the value of a postfix expression by pushing operands onto a stack and, for each operator, popping two operands, applying the operator and pushing the result.
Why the Order of Operands Matters
For + and ×, the order does not matter, but for − and / it does. In the postfix 8 2 −, the operator pops 2 first and then 8, and computes 8 − 2 = 6, not 2 − 8. Mixing up the order is the commonest error in this algorithm.
Evaluating Prefix and Checking Brackets
A prefix expression is evaluated in the same way, but scanning from right to left, and the first value popped is then the left operand. A related use of the stack is checking balanced brackets: push each opening bracket, and for each closing bracket pop and check that it matches. The brackets are balanced if every closing bracket matches and the stack is empty at the end. Compilers use this to find errors such as a missing brace.
Table 3.3: Evaluating 6 2 3 + − 3 8 2 / + × 2 ^ 3 +
| Symbol | Action | Stack after |
|---|---|---|
| 6, 2, 3 | Push | 6 2 3 |
| + | 2 + 3 = 5 | 6 5 |
| − | 6 − 5 = 1 | 1 |
| 3, 8, 2 | Push | 1 3 8 2 |
| /, + | 8 / 2 = 4, 3 + 4 = 7 | 1 7 |
| ×, 2, ^ | 1 × 7 = 7, 7 ^ 2 = 49 | 49 |
| 3, + | 49 + 3 = 52 | 52 |
Example 3.3: Evaluating a postfix expression
Problem: Evaluate the postfix expression 5 3 + 8 2 − ×.
Solution:
- Push 5, push 3. + pops 3 and 5, pushes 8.
- Push 8, push 2. − pops 2 and 8, pushes 6.
- × pops 6 and 8, pushes 48.
Result: The value is 48, which is (5 + 3) × (8 − 2).
#include <stdio.h>
#include <ctype.h>
int st[50], top = -1;
int main(void) {
const char *e = "53+82-*";
for (const char *p = e; *p; p++) {
if (isdigit((unsigned char)*p)) {
st[++top] = *p - '0'; continue;
}
int b = st[top--], a = st[top--];
switch (*p) {
case '+': st[++top] = a + b; break;
case '-': st[++top] = a - b; break;
case '*': st[++top] = a * b; break;
case '/': st[++top] = a / b; break;
}
}
printf("value = %d\n", st[top]);
return 0;
}
Output
value = 48
Common mistakes
- Popping the operands in the wrong order for − and /.
- Not checking for a single value left at the end.
- Treating multi-digit numbers as separate digits without a separator.
Key takeaways
- Push operands; for an operator, pop B then A, push A op B.
- The final single value on the stack is the answer.
- Postfix evaluation takes O(n) time.
- Balanced brackets can be checked with a stack.
Practice questions
- What does an operator do in postfix evaluation?
- Evaluate 2 3 4 × +.
- When is a postfix expression invalid?
Answers to the practice questions
- It pops two operands, applies the operation and pushes the result.
- 2 + 12 = 14.
- When an operator has fewer than two operands, or more than one value remains at the end.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit III
