KEMBAR78
Convert An Infix Expression Into A Postfix Expression | PDF
0% found this document useful (0 votes)
9 views3 pages

Convert An Infix Expression Into A Postfix Expression

The document describes an algorithm for converting infix expressions to postfix expressions without whitespace. It outlines four cases for processing tokens: handling brackets, operands, and operators while managing a stack for precedence. Examples of input and output are provided to illustrate the conversion process.

Uploaded by

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

Convert An Infix Expression Into A Postfix Expression

The document describes an algorithm for converting infix expressions to postfix expressions without whitespace. It outlines four cases for processing tokens: handling brackets, operands, and operators while managing a stack for precedence. Examples of input and output are provided to illustrate the conversion process.

Uploaded by

Sheela Mense
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Convert an infix expression into a postfix

expression

Given an infix expression, convert it to the postfix expression. Assume that

the infix expression is a string of tokens without any whitespace.

For example,

Input: A*B+C

Output: AB*C+

Input: (A+B)*(C/D)

Output: AB+CD/*

Input: A*(B*C+D*E)+F

Output: ABC*DE*+*F+

Input: (A+B)*C+(D-E)/F+G

Output: AB+C*DE-F/+G+

The following algorithm will output a string in postfix order. We process the

infix expression from left to right. For each token, four cases can arise:

1. If the current token is an opening bracket, '(', push it into the stack.
2. If the current token is a closing bracket, ')', pop tokens from the stack

until the corresponding opening bracket ‘(‘ is removed. Append each

operator at the end of the postfix expression.

3. If the current token is an operand, append it at the end of the postfix

expression.

4. If the current token is an operator, push it on the top of the stack.

Before doing that, first pop from the stack till we have a lower

precedence operator on top, or the stack becomes empty. Append

each operator at the end of the postfix expression.

Finally, append any remaining operators in the stack at the end of the postfix

expression and return the postfix expression. Following is the pictorial

representation of the above logic for the infix expression A*(B*C+D*E)+F:

You might also like