0% found this document useful (0 votes)
9 views1 page

Infix to Postfix Conversion Algorithm

The document outlines an algorithm for converting infix expressions to postfix notation using a stack. It details the steps for handling operands, operators, and parentheses, as well as the precedence rules for operators. An example expression is provided to illustrate the conversion process from infix to postfix.

Uploaded by

gannealekya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Infix to Postfix Conversion Algorithm

The document outlines an algorithm for converting infix expressions to postfix notation using a stack. It details the steps for handling operands, operators, and parentheses, as well as the precedence rules for operators. An example expression is provided to illustrate the conversion process from infix to postfix.

Uploaded by

gannealekya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Algorithm for Infix → Postfix Conversion

We scan the infix expression left to right.

1. If character is operand (A, B, 1, 2, …)

👉 Append it directly to output.

2. If character is (

👉 Push it onto the stack.

3. If character is )

👉 Pop from stack and append to output until a ( is found.


👉 Remove the ( from stack (don’t add it to output).

4. If character is an operator (+, -, *, /, ^)

👉 While stack is not empty AND precedence of top of stack ≥ precedence of current
operator:

Pop from stack and append to output.


👉 Push current operator to stack.

5. After scanning full expression

👉 Pop all remaining operators from stack and append to output.

🔹 Precedence Rules

+ and - → precedence 1

* and / → precedence 2

^ (power) → precedence 3 (right associative → handled carefully)

🔹 Example (Quick)

Expression: A+B*C

A → operand → output = A

+ → operator → stack = +

B → operand → output = AB

* → operator → check top = +

precedence(*) > precedence(+) → push *

stack = +, *

C → operand → output = ABC

End → pop stack → output = ABC*+

You might also like