Professional Documents
Culture Documents
Infix To Postfix Conversion: Course: Data Structures Course Code: CSE-203
Infix To Postfix Conversion: Course: Data Structures Course Code: CSE-203
Conversion
Multiplication *, Division /
Addition +, Subtraction -
We will use a stack to convert infix
expression to its equivalent postfix
expression
In computer science
, a stack is an
abstract data type
and data structure
based on the
principle of
Last In First Out (LIFO).
Psuedocode
Get the input of an infix expression into an array
tos=0 (tos=top of the stack)
for (i=0;i<=arraySize;i++)
{
if array[i]is an operand:
Append array[i] to the postfix expression
break;
if array[i]is an operator:
if (tos==0)
push array[i] into the stack
tos++;
if (tos>0)
{
for(j=tos;j>=0;j--){
compare the precedence of array[i] and stack[j];
if array[i] is an operator of greater or equal precedence
pop stack[j] and append it to the postfix expression;
tos--;
else break; }
then push array[i] into the stack
tos++;
break;
}
if array[i] is '(' :
push array[i] into the stack
tos++;
if array[i] is ')' :
for(k=tos;k>=0;k--)
{
if ( stack[k]!=’(’ ) pop stack[k] and append it to the postfix expression;
tos --;
if ( stack==’(’ ) tos --;
break;
}
if array[i]is NULL:
for(l=tos;l>=0;l--)
{
pop stack[l] and append it to the postfix expression;
}
}
Infix input
postfix expression
Infix to postfix conversion
infix expression
*b + (b-c)/g * ( (a+d) * (b-c/f) )
postfix expression
a
Infix to postfix conversion
infix expression
b + (b-c)/g * ( (a+d) * (b-c/f) )
postfix expression
a
*
Infix to postfix conversion
infix expression
+ (b-c)/g * ( (a+d) * (b-c/f) )
postfix expression
ab
*
Infix to postfix conversion
infix expression
(b-c)/g * ( (a+d) * (b-c/f) )
postfix expression
ab*
+
Infix to postfix conversion
infix expression
b-c)/g * ( (a+d) * (b-c/f) )
postfix expression
ab*
+
Infix to postfix conversion
infix expression
-c)/g * ( (a+d) * (b-c/f) )
postfix expression
ab*b
+
Infix to postfix conversion
infix expression
c)/g * ( (a+d) * (b-c/f) )
postfix expression
ab*b
-
(
+
Infix to postfix conversion
infix expression
)/g * ( (a+d) * (b-c/f) )
postfix expression
ab*bc
-
(
+
Infix to postfix conversion
infix expression
/g * ( (a+d) * (b-c/f) )
postfix expression
ab*bc-
+
Infix to postfix conversion
infix expression
g * ( (a+d) * (b-c/f) )
postfix expression
ab*bc-
/
+
Infix to postfix conversion
infix expression
* ( (a+d) * (b-c/f) )
postfix expression
ab*bc-g
/
+
Infix to postfix conversion
infix expression
( (a+d) * (b-c/f) )
postfix expression
ab*bc–g/
*
+
Infix to postfix conversion
infix expression
(a+d) * (b-c/f) )
postfix expression
ab*bc–g/
(
*
+
Infix to postfix conversion
infix expression
a+d) * (b-c/f) )
( postfix expression
ab*bc–g/
(
*
+
Infix to postfix conversion
infix expression
+d) * (b-c/f) )
( postfix expression
ab*bc–g/a
(
*
+
Infix to postfix conversion
infix expression
d) * (b-c/f) )
+
( postfix expression
ab*bc–g/a
(
*
+
Infix to postfix conversion
infix expression
) * (b-c/f) )
+
( postfix expression
ab*bc–g/ad
(
*
+
Infix to postfix conversion
infix expression
* (b-c/f) )
postfix expression
ab*bc–g/ad+
(
*
+
Infix to postfix conversion
infix expression
(b-c/f) )
* postfix expression
ab*bc–g/ad+
(
*
+
Infix to postfix conversion
infix expression
b-c/f) )
(
* postfix expression
ab*bc–g/ad+
(
*
+
Infix to postfix conversion
infix expression
-c/f) )
(
* postfix expression
ab*bc–g/ad+b
(
*
+
Infix to postfix conversion
infix expression
- c/f) )
(
* postfix expression
ab*bc–g/ad+b
(
*
+
Infix to postfix conversion
infix expression
- /f) )
(
* postfix expression
ab*bc–g/ad+bc
(
*
+
Infix to postfix conversion
/
infix expression
- f) )
(
* postfix expression
ab*bc–g/ad+bc
(
*
+
Infix to postfix conversion
/
infix expression
- ))
(
* postfix expression
ab*bc–g/ad+bcf
(
*
+
Infix to postfix conversion
infix expression
)
* postfix expression
ab*bc–g/ad+bcf/-
(
*
+
Infix to postfix conversion
infix expression
postfix expression
ab*bc–g/ad+bcf/-*
*
+
Infix to postfix conversion
infix expression
postfix expression
ab*bc–g/ad+bcf/-**
+
Infix to postfix conversion
infix expression
postfix expression
ab*bc–g/ad+bcf/-**+
Equivalent postfix
expression
ab*bc–g/ad+bcf/-**+
We have observed
When we get any integer or character it goes to the output.
The order of the operands in the postfix expression is the same as
the order in the infix expression, and the operands that appear to the
left of an operator in the infix expression also appear to its left in the
postfix expression.
Course teacher:
Abul Hasan Samee
Made by……