0% found this document useful (0 votes)
36 views2 pages

Algorithm To Convert Infix To Postfix

The document outlines an algorithm for converting infix expressions to postfix expressions using a stack. It details the steps involved, including handling operands, operators, and parentheses, with an example provided for clarity. The document also highlights the advantages of postfix expressions, particularly their ease of evaluation by machines due to inherent operator precedence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Algorithm To Convert Infix To Postfix

The document outlines an algorithm for converting infix expressions to postfix expressions using a stack. It details the steps involved, including handling operands, operators, and parentheses, with an example provided for clarity. The document also highlights the advantages of postfix expressions, particularly their ease of evaluation by machines due to inherent operator precedence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithm to convert Infix to Postfix

Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent
postfix expression Y.

1. Push “(“onto Stack, and add “)” to the end of the given arithmetic expression.
2. Scan the given expression from left to right and repeat step 3 to 6 until the Stack is
empty.
3. If an operand is encountered, add it to expression.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to expression each operator (on the top of
Stack) which has the same precedence as or higher precedence than operator.
2. Add operator to Stack.
3. [End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to expression each operator (on the top of
Stack) until a left parenthesis is encountered.
2. Remove the left Parenthesis.
3. [End of If]
4. [End of If]
7. END.
Let’s take an example to better understand the algorithm

Infix Expression: A+ (B*C-(D/E^F)*G)*H

Postfix Expression: ABC*DEF^/G*-H*+

Advantage of Postfix Expression over Infix Expression

An infix expression is difficult for the machine to know and keep track of the precedence of
operators. On the other hand, a postfix expression itself determines the precedence of operators
(as the placement of operators in a postfix expression depends upon its precedence).Therefore,
for the machine, it is easier to carry out a postfix expression than an infix expression.

You might also like