Professional Documents
Culture Documents
postfix: ABC *+
Moving Operators to the Left for Prefix Notation
prefix: +A*BC
Infix to prefix/postfix conversion
Infix: A + B * C + D Infix: A * B + C * D
Prefix: + + A * B C D Prefix: + * A B * C D
Postfix: A B C * + D + Postfix: A B * C D * +
Infix: (A + B) * (C + Infix: A + B + C + D
D) Prefix: + + + A B C D
Prefix: * + A B + C D Postfix: A B + C + D
Postfix: A B + C D + * +
Convert infix to postfix using stack
Convert A * (B + C) * D to postfix notation.
1 A empty A
2 * * A
3 ( (* A
4 B (* AB
5 + +(* AB
6 C +(* ABC
7 ) * ABC+
8 * * ABC+*
9 D * A B C + * D*
10 empty
Convert infix to postfix
(( A – ( B + C ) ) * D ) ^ ( E + F )
Evaluation of postfix expression
Evaluate the expression 2 3 4 + * 5 * which was
created by the previous algorithm for infix to
postfix.
Stack (grows toward
Move Current Token
left)
1 2 2
2 3 32
3 4 432
4 + 72
5 * 14
6 5 5 14
7 * 70
Stack S, char ch, element;
while( tokens are available)
Algorithm for Infix to
{
ch=read(token)
postfix conversion
if (ch is operand) using stack
print ch
else if (ch is ‘(‘ )
push (S, ch)
else if (ch is ‘)’ )
{
while((element =pop(S)) ≠ ‘(‘)
print ch
}
else
{
while (priority (top of stack) ≥ priority (ch))
{
element = pop(S);
print (element);
}
push(S, ch);
}
} // end while tokens are not available
while( !empty(S))
{
element =pop(S);
print (element);
}
Summary
•20
Stack ADT
Infix to postfix
Examples
Algorithm for Infix to postfix