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

# Algorithm for infix to postfix conversion

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

- Asked in: 2071 Magh, 1+4 marks; 2072 Asoj, 1+4 marks (asked 2 times)
- Video: [Application of Stacks (Infix to Postfix) - Part 1](https://www.youtube.com/watch?v=XfX5jlzWQsg) — Neso Academy
- 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-algorithm
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Define stack. Write an algorithm to convert infix expression into postfix expression using stack.

## Short answer

A stack is a linear LIFO structure supporting push and pop. The algorithm to convert infix to postfix scans each symbol: operands are appended to output; operators are pushed to a stack after popping any operators of higher or equal precedence; parentheses handle grouping. Example: A+B*C-D/E yields postfix ABC*+DE/-.

## 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).

#### Algorithm: infix to postfix

```text
Algorithm InfixToPostfix(infix)
Input : a valid infix expression
Output: postfix expression
1. Create an empty stack S; postfix = ""
2. For each symbol x of infix, from left to right:
     if x is an operand:
         append x to postfix
     else if x is '(':
         push(S, x)
     else if x is ')':
         while top(S) != '(':
             append pop(S) to postfix
         pop(S)                       /* discard '(' */
     else:                            /* x is an operator */
         while S not empty and top(S) != '(' and
               prec(top(S)) >= prec(x):   /* > for '^' */
             append pop(S) to postfix
         push(S, x)
3. While S is not empty:
     append pop(S) to postfix
4. Return postfix
```

#### Example: A + B * C - D / E

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

**Answer:** `ABC*+DE/-`

(Check: `B*C` and `D/E` bind tighter than `+` and `-`; the postfix `ABC*+DE/-` evaluates in that order.)

## Check yourself

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

1. A stack follows which principle?
   A. FIFO
   B. Random access
   C. LIFO
   D. Priority order
   Answer: C. The last element inserted is the first removed. (Key point: Stack principle)

2. In infix-to-postfix conversion, an operand is:
   A. Pushed on the stack
   B. Popped from the stack
   C. Discarded
   D. Appended directly to the output
   Answer: D. Operands go straight to postfix. (Key point: Operand handling)

3. On reading ')', the algorithm:
   A. Pushes ')' on the stack
   B. Pops everything including '(' to output
   C. Pops operators to output until '(' and discards '('
   D. Clears the output
   Answer: C. Parentheses are not output. (Key point: Closing bracket)

4. When an operator x arrives, which stack operators are popped first?
   A. Only those with lower precedence
   B. All operators
   C. None
   D. Those with precedence greater than or equal to x (stopping at '(')
   Answer: D. Then x is pushed. (Key point: Operator rule)

## Close variants set in other exams

- [Convert A+B-C*(D-E+F/G)/H to postfix](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apostfix-convert-abcdefgh) — Data Structure and Algorithm, chapter 2, asked 3×

## 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
- [Infix to postfix and evaluation](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Ainfix-postfix-explain-evaluate) — Data Structure and Algorithm, chapter 2
- [Convert A+B-(C*D/E+F)-G*H to postfix](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Apostfix-abcde-fgh) — 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
