---
title: "Infix to postfix and evaluation"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:infix-postfix-explain-evaluate"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/infix-postfix-explain-evaluate
site: BCT Engineering
---

# Infix to postfix and evaluation

Data Structure and Algorithm · Chapter 2: The Stack and Queue

- Asked in: 2075 Baisakh, 2+4+4 marks
- Video: [3.4 Infix Prefix and Postfix expressions | Data Structures Tutorials](https://www.youtube.com/watch?v=RY4GkLahbCI) — Jenny's Lectures CS IT
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/infix-postfix-explain-evaluate
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Define stack. How to convert infix to postfix notation? Explain with example. Evaluate the postfix expression AB+C*DEFG-*+ with status of stack where A = 2, B = 3, C = 10, D = 5, E = 2, F = 4 and G = 6.

## Short answer

Stack is a LIFO structure. Infix to postfix: scan, output operands, push operators respecting precedence; brackets used. Evaluating AB+C*DEFG-*+ with given values: push operands; on operator, pop two, apply. Two values remain: 50 and 1 (expression lacks a final operator).

## Answer

A **stack** is a linear data structure in which insertion (push) and deletion (pop) take place only at one end called the **top**. The last element inserted is the first to be removed, so it follows **LIFO** (Last In First Out).

#### Converting infix to postfix

Postfix (reverse Polish) places each operator after its operands, so no parentheses or precedence rules are needed when evaluating. Rules with a stack:

1. Scan the infix expression left to right.
2. **Operand**: append it to the postfix output.
3. **`(`**: push it on the stack.
4. **`)`**: pop operators to the output until `(` is found; discard both parentheses.
5. **Operator**: while the stack top is an operator with **higher or equal precedence** (for `^`, only higher, since `^` is right-associative), pop it to the output; then push the scanned operator.
6. At the end, pop all remaining operators to the output.

Precedence (high to low): `^`, then `* /`, then `+ -`.

**Example: (A + B) * C - D / E**

| Step | Symbol | Stack (bottom → top) | Postfix output |
|---|---|---|---|
| 1 | ( | ( | - |
| 2 | A | ( | A |
| 3 | + | (+ | A |
| 4 | B | (+ | AB |
| 5 | ) | (empty) | AB+ |
| 6 | \* | \* | AB+ |
| 7 | C | \* | AB+C |
| 8 | - | - | AB+C\* |
| 9 | D | - | AB+C\*D |
| 10 | / | -/ | AB+C\*D |
| 11 | E | -/ | AB+C\*DE |
| end | (pop all) | (empty) | AB+C\*DE/- |

**Answer:** `AB+C*DE/-`

#### Evaluating the postfix expression `AB+C*DEFG-*+`

Values: A=2, B=3, C=10, D=5, E=2, F=4, G=6. Rule: operand → push; operator → pop two (first pop is the right operand), apply, push the result.

| Step | Symbol | Action | Stack (bottom → top) |
|---|---|---|---|
| 1 | A | push 2 | 2 |
| 2 | B | push 3 | 2, 3 |
| 3 | + | pop 3 (right), 2 (left); 2 + 3 = 5; push | 5 |
| 4 | C | push 10 | 5, 10 |
| 5 | \* | pop 10 (right), 5 (left); 5 \* 10 = 50; push | 50 |
| 6 | D | push 5 | 50, 5 |
| 7 | E | push 2 | 50, 5, 2 |
| 8 | F | push 4 | 50, 5, 2, 4 |
| 9 | G | push 6 | 50, 5, 2, 4, 6 |
| 10 | - | pop 6 (right), 4 (left); 4 - 6 = -2; push | 50, 5, 2, -2 |
| 11 | \* | pop -2 (right), 2 (left); 2 \* -2 = -4; push | 50, 5, -4 |
| 12 | + | pop -4 (right), 5 (left); 5 + -4 = 1; push | 50, 1 |

Result: the single scan finishes with **two** values on the stack (50 and 1), because the expression as given has 7 operands but only 5 operators (6 are needed). The sub-results are (A+B)*C = **50** and D + E*(F−G) = **1**. If the intended expression had one more operator at the end (for example `+`), the value would be 50 + 1 = **51**.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Postfix notation needs:
   A. Parentheses
   B. Precedence tables at evaluation
   C. No parentheses or precedence rules for evaluation
   D. Right-to-left scanning only
   Answer: C. Operator position decides the order. (Key point: Postfix advantage)

2. (A + B) * C - D / E converts to:
   A. ABC*+DE/-
   B. AB+CDE/*-
   C. AB+C*DE/-
   D. AB+C*D/E-
   Answer: C. Brackets force A+B first, then * C; D/E; then -. (Key point: Conversion result)

3. When evaluating postfix, on an operator you pop two values. The first pop is the:
   A. Left operand
   B. Result
   C. Operator
   D. Right operand
   Answer: D. So for 4 6 -, we get 4 - 6, not 6 - 4. (Key point: Evaluation order of operands)

4. With F=4, G=6, the step 'F G -' gives:
   A. 2
   B. 10
   C. 24
   D. -2
   Answer: D. 4 - 6 = -2. (Key point: Value of F-G)

## Related in Data Structure and Algorithm

- [Importance of postfix; convert expression](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apostfix-importance-conversion) — Data Structure and Algorithm, chapter 2
- [Convert expression to prefix and postfix](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Ainfix-prefix-postfix-tabular) — Data Structure and Algorithm, chapter 2
- [Convert expression to prefix and postfix](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aprefix-postfix-abcdefgk) — Data Structure and Algorithm, chapter 2
- [Evaluate postfix expression 574-*8/4+](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apostfix-evaluate-574) — Data Structure and Algorithm, chapter 2
- [Algorithm for infix to postfix conversion](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Ainfix-postfix-algorithm) — Data Structure and Algorithm, chapter 2, asked 3×
- [Convert and evaluate A/B-(C+D)*E](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apostfix-evaluate-ab-slash-cd-e) — Data Structure and Algorithm, chapter 2, asked 2×
