---
title: "Convert expression to prefix and postfix"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:prefix-postfix-abcdefgk"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/prefix-postfix-abcdefgk
site: BCT Engineering
---

# Convert expression to prefix and postfix

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

- Asked in: 2072 Magh, 4+3 marks
- Video: [Infix, Prefix and Postfix](https://www.youtube.com/watch?v=jos1Flt21is) — mycodeschool
- 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/prefix-postfix-abcdefgk
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Convert a*b^c-d+(e/f*g)/(k-1) into prefix and postfix expression. Write an algorithm for top of stack fix stack operation.

## Short answer

Postfix: abc^*d-ef/g*k1-/+. Prefix: +-*a^bcd/*/efg-k1. Stack top algorithm: if top==-1 report error, else return S[top].

## Answer

Infix: `a * b ^ c - d + ( e / f * g ) / ( k - 1 )`. Precedence: `^` (right-associative) > `* /` > `+ -`. The last bracket is read as `(k - 1)` with the digit 1.

#### Postfix (scan left to right with a stack)

| 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 | ( | +( | abc^\*d- |
| 10 | e | +( | abc^\*d-e |
| 11 | / | +(/ | abc^\*d-e |
| 12 | f | +(/ | abc^\*d-ef |
| 13 | \* | +(\* | abc^\*d-ef/ |
| 14 | g | +(\* | abc^\*d-ef/g |
| 15 | ) | + | abc^\*d-ef/g\* |
| 16 | / | +/ | abc^\*d-ef/g\* |
| 17 | ( | +/( | abc^\*d-ef/g\* |
| 18 | k | +/( | abc^\*d-ef/g\*k |
| 19 | - | +/(- | abc^\*d-ef/g\*k |
| 20 | 1 | +/(- | abc^\*d-ef/g\*k1 |
| 21 | ) | +/ | abc^\*d-ef/g\*k1- |
| end | (pop all) | (empty) | abc^\*d-ef/g\*k1-/+ |

**Postfix:** `abc^*d-ef/g*k1-/+`

#### Prefix (reverse the infix, swap brackets, convert like postfix, reverse the result)

Reversed infix with swapped brackets: `( 1 - k ) / ( g * f / e ) + d - c ^ b * a`. For prefix, an incoming operator pops only operators of **strictly higher** precedence (`^` pops equal), because the output is reversed at the end.

| Step | Symbol (of reversed string) | Stack | Output (before reversing) |
|---|---|---|---|
| 1 | ( | ( | - |
| 2 | 1 | ( | 1 |
| 3 | - | (- | 1 |
| 4 | k | (- | 1k |
| 5 | ) | (empty) | 1k- |
| 6 | / | / | 1k- |
| 7 | ( | /( | 1k- |
| 8 | g | /( | 1k-g |
| 9 | \* | /(\* | 1k-g |
| 10 | f | /(\* | 1k-gf |
| 11 | / | /(\*/ | 1k-gf |
| 12 | e | /(\*/ | 1k-gfe |
| 13 | ) | / | 1k-gfe/\* |
| 14 | + | + | 1k-gfe/\*/ |
| 15 | d | + | 1k-gfe/\*/d |
| 16 | - | +- | 1k-gfe/\*/d |
| 17 | c | +- | 1k-gfe/\*/dc |
| 18 | ^ | +-^ | 1k-gfe/\*/dc |
| 19 | b | +-^ | 1k-gfe/\*/dcb |
| 20 | \* | +-\* | 1k-gfe/\*/dcb^ |
| 21 | a | +-\* | 1k-gfe/\*/dcb^a |
| end | (pop all) | (empty) | 1k-gfe/\*/dcb^a\*-+ |

Reverse the final output: **Prefix:** `+-*a^bcd/*/efg-k1`

#### Algorithms for stack operations (array `S[0..MAX-1]`, `top = -1` initially)

```text
PUSH(S, x)
1. if top == MAX - 1:  print "Stack overflow"; return
2. top = top + 1
3. S[top] = x

POP(S)
1. if top == -1:  print "Stack underflow"; return ERROR
2. x = S[top]
3. top = top - 1
4. return x

STACKTOP(S)               /* peek: read top without removing */
1. if top == -1:  print "Stack empty"; return ERROR
2. return S[top]
```
(The final part of the question is read as asking for the stack-top/peek operation together with push and pop.)

## Check yourself

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

1. Postfix of a*b^c-d+(e/f*g)/(k-1) is:
   A. abc^*d-ef/g*k1-/+
   B. abc*^d-ef/g*k1-/+
   C. ab*c^d-ef/g*k1-/+
   D. abc^*d-ef/g*k1/-+
   Answer: A. ^ is highest, then * /, then + -. (Key point: Postfix result)

2. Prefix of the same expression is:
   A. +-*a^bcd/*/efg-1k
   B. -+*a^bcd/*/efg-k1
   C. +-*a^bcd*//efg-k1
   D. +-*a^bcd/*/efg-k1
   Answer: D. Built by reversing, swapping brackets, converting and reversing the output. (Key point: Prefix result)

3. In the reversed-infix method for prefix, an incoming operator pops:
   A. Operators of higher or equal precedence
   B. All operators
   C. Only operators with strictly higher precedence
   D. Nothing
   Answer: C. Because the output is reversed at the end. (Key point: Prefix conversion rule)

4. In POP(S), what is checked first?
   A. top == MAX - 1
   B. top == -1 (underflow)
   C. top == 0
   D. S is sorted
   Answer: B. Then x = S[top]; top = top - 1. (Key point: Pop algorithm)

## Related in Data Structure and Algorithm

- [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
- [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 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×
- [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×
- [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
- [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
