0% found this document useful (0 votes)
171 views

1.4 Conversion of Infix To Postfix

The document discusses converting infix notation mathematical expressions to postfix notation. It explains that infix notation uses operators between operands, while postfix notation places operators after operands. To perform the conversion, it uses a stack data structure and processes the expression from left to right, pushing operators to the stack and outputting operands until the entire expression has been converted to postfix form.

Uploaded by

Mohamed Bilal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views

1.4 Conversion of Infix To Postfix

The document discusses converting infix notation mathematical expressions to postfix notation. It explains that infix notation uses operators between operands, while postfix notation places operators after operands. To perform the conversion, it uses a stack data structure and processes the expression from left to right, pushing operators to the stack and outputting operands until the entire expression has been converted to postfix form.

Uploaded by

Mohamed Bilal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SUBJECT CODE

TYPE THE SUBJECT NAME HERE

UNIT NO 1

LINEAR DATA STRUCTURES - LIST

1.4 Conversion of Infix to Postfix

II III

CS8391
DATA STRUCTURES (Common to CSE & IT)
CS8391
DATA STRUCTURES (Common to CSE & IT)

1.4 Conversion of Infix to postfix


•Infix expression: The expression of the form a op b. When an operator is in-between
every pair of operands.
Input: The infix expression. x^y/(5*z)+2
•Postfix expression: The expression of the form a b op. When an operator is
followed for every pair of operands.
Output: Postfix Form Is: xy^5z*/2+
CS8391
DATA STRUCTURES (Common to CSE & IT)

● Infix expressions are readable and solvable by humans.

● We can easily distinguish the order of operators, and also can use the parenthesis
to solve that part first during solving mathematical expressions.

● The computer cannot differentiate the operators and parenthesis easily, that’s why
postfix conversion is needed.

● To convert infix expression to postfix expression, we will use the stack data
structure.

● By scanning the infix expression from left to right, when we will get any operand,
simply add them to the postfix form, and for the operator and parenthesis, add
them in the stack maintaining the precedence of them.
CS8391
DATA STRUCTURES (Common to CSE & IT)

1.Create an empty stack called opstack for keeping operators. Create an


empty list for output.

2.Convert the input infix string to a list by using the string method split.

3.Scan the token list from left to right.

If the token is an operand, append it to the end of the output list.

○ If the token is a left parenthesis, push it on the opstack.


○ If the token is a right parenthesis, pop the opstack until the corresponding left
parenthesis is removed. Append each operator to the end of the output list.
○ If the token is an operator, *, /, +, or -, push it on the opstack. However, first
remove any operators already on the opstack that have higher or equal
precedence and append them to the output list.

4.When the input expression has been completely processed, check the
opstack. Any operators still on the stack can be removed and appended to
the end of the output list.
CS8391
DATA STRUCTURES (Common to CSE & IT)

For example –
Infix notation: (2+4) * (4+6)
Post-fix notation: 2 4 + 4 6 + *
Result: 60
The stack operations for this expression evaluation is shown below:
CS8391
DATA STRUCTURES (Common to CSE & IT)
Example:Convert A * (B + C) * D to postfix notation.

Move Curren Ttoken Stack Output

1 A empty A

2 * * A

3 ( (* A

4 B (* AB

5 + +(* AB

6 C +(* ABC

7 ) * ABC+

8 * * ABC+*

9 D * ABC+*D

10 empty
CS8391
DATA STRUCTURES (Common to CSE & IT)

Input String Output Stack Operator Stack

A+B*C/(E-F) A

A+B*C/(E-F) A +

A+B*C/(E-F) AB +

A+B*C/(E-F) AB +*

A+B*C/(E-F) ABC +*

A+B*C/(E-F) ABC* +/

A+B*C/(E-F) ABC* +/(

A+B*C/(E-F) ABC*E +/(

A+B*C/(E-F) ABC*E +/(-

A+B*C/(E-F) ABC*EF +/(-

A+B*C/(E-F) ABC*EF- +/

A+B*C/(E-F) ABC*EF-/+

You might also like