Вы находитесь на странице: 1из 3

Infix to Postfix Conversion

Procedure for Postfix Conversion

1. Put the open and close parenthesis/delimeter at the start and end of the Infix
Expression.
2. Read the Infix string from left to Right.
3. Initialize an empty stack.
4. If the scanned character is an operand, add it to the OUTPUT Postfix string.
5. If the scanned character is an operator and if the stack is empty push the
character to stack.
6. If the scanned character is an Operator and the stack is not empty, compare the
precedence of the character with the element on top of the stack.
7. If top Stack has higher precedence over the scanned character pop the stack else
push the scanned character to stack. Repeat this step until the stack is not empty
and top Stack has precedence over the character.
8. Repeat 4 and 5 steps till all the characters are scanned.
9. After all characters are scanned, we have to add any character that the stack may
have to the Postfix string.
10. If stack is not empty add top Stack to Postfix string and Pop the stack.
11. Repeat this step as long as stack is not empty.

Conversion To Postfix
EXAMPLE: Infix String : A+(B*C-(D/E-F)*G)*H
Step-1: (A+(B*C-(D/E-F)*G)*H)

Output
Stack Input Description
(Postfix String)

Empty (A+(B*C-(D/E-F)*G)*H) - Read the Infix String from left.

Current token is "(". Since it is a delimiter,


( (A+(B*C-(D/E-F)*G)*H) -
Push into the stack.

Current token is "A". Since it is a character,


( A+(B*C-(D/E-F)*G)*H) A
move to the Output.
Current token is "+". Since it is a Operator,
(+ +(B*C-(D/E-F)*G)*H) A
Push into the stack.
Current token is "(". Since it is a delimiter,
(+( (B*C-(D/E-F)*G)*H) A
Push into the stack.
Current token is "B". Since it is a character,
(+( B*C-(D/E-F)*G)*H) AB
move to the Output.
Current token is "*". Since it is a Operator,
(+(* *C-(D/E-F)*G)*H) AB
Push into the stack.
Current token is "C". Since it is a character,
(+(* C-(D/E-F)*G)*H) ABC
move to the Output.
Current token is "-". Since it is a Operator,
(+(* Push into the stack. But in the top of the
-(D/E-F)*G)*H) ABC* stack "*" is there. "*" is at higher precedence
than"-". So pop the "*" to the Output and
(+(-
Push the "-" in to the stack.
Current token is "(". Since it is a delimiter,
(+(-( (D/E-F)*G)*H) ABC*
Push into the stack.
Current token is "D". Since it is a character,
(+(-( D/E-F)*G)*H) ABC*D
move to the Output.
Current token is "/". Since it is a Operator,
(+(-(/ /E-F)*G)*H) ABC*D
Push into the stack.
Current token is "E". Since it is a character,
(+(-(/ E-F)*G)*H) ABC*DE
move to the Output.

(+(-(/ Current token is "-". Since it is a Operator,


Push into the stack. But in the top of the
-F)*G)*H) ABC*DE/ stack "/" is there. "/" is at higher precedence
(+(-(- than"-". So pop the "/" to the Output and
Push the "-" in to the stack.

Current token is "F". Since it is a character,


(+(-(- F)*G)*H) ABC*DE/F
move to the Output.
Current token is ")". Since it is a delimiter,
(+(-(-) )*G)*H) ABC*DE/F
Push into the stack.
After pushing ")" into the stack, we found one
(+(- *G)*H) ABC*DE/F- set of delimiter. so pop the "-" to the output.
Then the updated stack is "(+(-".
Current token is "*". Since it is a Operator,
(+(-* *G)*H) ABC*DE/F-
Push into the stack.
Current token is "G". Since it is a character,
(+(-* G)*H) ABC*DE/F-G
move to the Output.
After pushing ")" into the stack, we found one
(+(-*) )*H) ABC*DE/F-G*- set of delimiter. so pop the "-*" to the output
respectively. Then the updated stack is "(+".
Current token is "*". Since it is a Operator,
(+* *H) ABC*DE/F-G*-
Push into the stack.
Current token is "H". Since it is a character,
(+* H) ABC*DE/F-G*-H
move to the Output.
After pushing ")" into the stack, we found one
set of delimiter. so pop the "+*" to the output
(+*) ) ABC*DE/F-G*-H
respectively. Then the updated stack is
Empty.

Empty End ABC*DE/F-G*-H*+

Вам также может понравиться