Application areas of Stack (Infix to postfix, Infix to prefix)

Computers evaluate expressions by first converting human infix notation (A + B × C) into postfix (A B C × +) or prefix (+ A × B C), removing the need for precedence rules and brackets, and a stack does the conversion.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The till must not fail maths

A combo order lands at the till: 2 samosas at 15 plus 3 chai... the software must evaluate 2 + 3 × 5.

Is that 17, or 25? You know it is 17 (multiply first), because your school drilled precedence into you.

But a machine reading left to right sees 2 + 3 first and happily produces 25. Teaching every calculator all of BODMAS, with brackets, is messy. Computer science found a cleaner trick: change the notation itself, using the stack from two lessons ago.

Theory

Three ways to say one sentence

Where the operator sits is just grammar:

  • Infix: A + B (operator in the middle: human style)
  • Postfix: A B + (operator after: "take A and B, now add")
  • Prefix: + A B (operator before: "add the following two")

Same meaning, three word orders. The magic: in postfix and prefix, no brackets and no precedence rules are ever needed. The order of symbols alone fixes the order of work.

Theory

The rules of rank

Conversion runs on operator precedence (who binds tighter):

1. Brackets ( ) first, always

2. Powers ^

3. × and / next

4. + and - last

5. Equal rank: work left to right

So in 2 + 3 × 5, the × grabs 3 and 5 before + gets a turn: the expression really means 2 + (3 × 5).

That insight IS the conversion method: make the invisible brackets visible, then relocate each operator.

Follow along

The exam recipe: convert by full bracketing

  1. Fully parenthesize the infix expression using precedence A + B × C becomes (A + (B × C)): every operator gets its own bracket pair.
  2. For POSTFIX: move each operator to just AFTER its closing bracket (A + (B × C)) : the × jumps after (B C), the + jumps after everything: (A (B C) ×) +
  3. For PREFIX: move each operator to just BEFORE its opening bracket + (A × (B C)) : operators lead their bracket instead.
  4. Erase all brackets Postfix: A B C × + Prefix: + A × B C. Done, no brackets survive.

Theory

Worked twice: the bracket changes everything

A + B × C (× binds first):

Bracketed: (A + (B × C))

Postfix: A B C × + Prefix: + A × B C

(A + B) × C (bracket forces + first):

Bracketed: ((A + B) × C)

Postfix: A B + C × Prefix: × + A B C

Compare the two postfix answers: same three letters, different story. The bracket in the infix version survives as a different ordering, not as a bracket.

Think first

Your turn on paper

Convert A × (B + C) - D to postfix using the bracket recipe. Do it on paper first: bracket fully, relocate operators, erase brackets. Then tap.

Show the answer

Fully bracketed by precedence: ((A × (B + C)) - D)

Move operators after their closing brackets:

+ after (B C) → B C +

× after (A ...) → A B C + ×

  • last → A B C + × D -

Postfix: A B C + × D -

If you wrote A B C + × D - you have the method; if the + and × swapped, re-check which bracket closes first (innermost wins).

Quiz

What is the postfix form of A + B × C, and why is it NOT A B + C ×?

  1. A B C × +, because × has higher precedence so B × C is grouped first
  2. A B + C ×, because conversion always goes left to right
  3. × + A B C, because operators move to the front
  4. A B C + ×, because + appears first in the infix expression
Show the answer

A B C × +, because × has higher precedence so B × C is grouped first

Precedence groups B × C before anything else: (A + (B × C)) → A B C × +. Option B is the classic error of converting by reading order instead of precedence: A B + C × actually means (A + B) × C, a different expression. Option C is the PREFIX form's logic (misapplied), and option D repeats the left-to-right trap in new clothes.

Watch out

Where marks vanish

Ignoring precedence is the killer: converting A + B × C left to right gives a wrong answer that LOOKS tidy. Always bracket first, convert second.

Equal-precedence operators go left to right: A - B + C brackets as ((A - B) + C), never (A - (B + C)).

And label your answers: examiners ask for both postfix AND prefix; a correct postfix labelled prefix scores zero.

Formula

Why the stack owns this job

In the machine, conversion and evaluation both run on a stack: operands pass through, operators wait on the stack until a lower-ranked operator (or a closing bracket) pops them out. Evaluating postfix is even simpler: see a number, push; see an operator, pop two, compute, push back. The full stack applications list for exams: expression conversion/evaluation, function calls, undo/redo, browser back, balanced-bracket checking.

Summary

Key takeaways

  • Infix (A + B) needs precedence and brackets; postfix (A B +) and prefix (+ A B) need neither.
  • Precedence: brackets, then ^, then × /, then + -, equal ranks left to right.
  • Recipe: fully bracket, move operators after (postfix) or before (prefix) their bracket, erase brackets.
  • A + B × C → postfix A B C × +, prefix + A × B C; (A + B) × C → A B + C ×.
  • The machine converts and evaluates with a stack: operands flow, operators wait.
  • Stack applications: expressions, call stack, undo, browser back, bracket checking.
  • Memory hook: bracket first, then relocate the operators.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Data Structure

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Application areas of Stack (Infix to postfix, Infix to prefix) · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn