INFIX TO POSTFIX CONVERSION IN C++, Study notes of Data Structures and Algorithms

sHSqhsjKdawjidada5wihow to convert from Infix expression to postfix expression (with c++ code).

Typology: Study notes

2018/2019

Uploaded on 11/09/2019

abhishek-singh-19
abhishek-singh-19 ๐Ÿ‡ฎ๐Ÿ‡ณ

1 document

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Conversion of Infix to Postfix
Infix to Postfix
expression
Dr B R Ambedkar National Institute of Technology, Jalandhar
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download INFIX TO POSTFIX CONVERSION IN C++ and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Conversion of

Infix to Postfix

Infix to Postfix

expression

Dr B R Ambedkar National Institute of Technology, Jalandhar

Infix Notation

Postfix Notation

Operator Precedence

Infix to Postfix Conversion Algorithm

Example

C++ Code for Infix to Postfix Conversion

Conclusion

Contents

Used to represent algebraic expressions

This notation is easier to parse for a machine

Characterized by placement of operators after the operand

Example :

ABC*+

ABC+*D/E-

Postfix Notation

Operator Precedence

Token Operator Precedence Associativity

()

[]

.

Function call

Array element

Structure element

17 Left to Right

++

--

Increment

decrement

16 Left to Right

++

--

!

-+

Sizeof

Increment

Decrement

Logical not

Unary minus,plus

Size(in bytes)

15 Right to left

type Type cast 14 Right to Left

  • / % Multiplicative 13 Left to Right
    • Binary sum and

diff

12 Left to Right

<< >> shift 11 Left to Right

Infix to Postfix Conversion Algorithm

Step 1: Scan the infix expression from left to right for

tokens(operators, operands and parenthesis).

Step 2: If the token is operand append it in postfix expression

Step 3: If the token is a left parenthesis โ€œ(โ€œ, push it in the stack

Infix to Postfix Conversion

Algorithm(continued)

Step 4: If token is an operator,

Pop all operators which are of higher or equal

precedence then the incoming token(in same order)

and append them to output expression

After popping out all such operators, push new token

on the stack.

Infix to Postfix Conversion

Algorithm(continued)

Step 6: After all tokens of infix expression have been scanned.

Pop all elements from stack and append them to

output expression

Step 7: Output expression obtained is the required Postfix

expression

Example

Let Infix expression be : A*(B+C)/D โ€“ E

Stage 1: Initially stack is empty

Scan the infix expression from left to right

Infix Notation :

A*(B+C)/D โ€“ E

Postfix Notation :

STACK

top NULL

Example (continued)

Stage 3 : Next token is operator * , Push it into the stack

Infix Notation :

(B+C)/D โ€“ E

Postfix Notation :

A

STACK

top

Example (continued)

Stage 4: Next token is left parenthesis (, Push it into the stack

Infix Notation :

B+C)/D โ€“ E

Postfix Notation :

A

STACK

top

Example (continued)

Stage 6: Next token is operator +,

We consider the outgoing precedence of top element of

stack,(

Outgoing precedence of ( is least, so + is pushed into the

stack

Infix Notation :

C)/D โ€“ E

Postfix Notation :

AB

STACK

top

Example (continued)

Stage 7: Next token is operand C, operands are appended to output

expression as it is

Infix Notation :

)/D โ€“ E

Postfix Notation :

ABC

STACK

top

Example (continued)

Stage 9: Next token is operator /,

We consider the outgoing precedence of top element of

stack,*

Outgoing precedence of * is same as /,

So, pop * and push / into the stack

Infix Notation :

D โ€“ E

Postfix Notation :

ABC+*

STACK

top

Example (continued)

Stage 10: Next token is operand D, operands are appended to

output expression as it is

Infix Notation :

โ€“ E

Postfix Notation :

ABC+*D

STACK

top