Infix to Postfix Conversion
• Stacks are widely used in the design and implementation of
• For example, they are used to convert arithmetic expressions
from infix notation to postfix notation.
• An infix expression is one in which operators are located
between their operands.
• In postfix notation, the operator immediately follows its
Precedence and Priority
struct or union member
increment, decrement2 16
unary minus or plus
address or indirection
size (in bytes)
binary add or subtract
bitwise exclusive or
1. Scan the expression from left to right.
2. If any operands comes print it simply
3. If any operator comes compare the incoming operator with stack
operator. If the incoming operator priority is higher than stack
operator priority push the incoming operator.
4. If the incoming operator has less priority than the operator
inside the stack then go on popping the operator from top of the
stack and print them till this condition is true and then push the
incoming operator on top of the stack..
5. If both incoming and stack operator priority are equal then pop
the stack operator till this condition is true.
6. If the operator is ‘)’ then go on popping the operators from top
of the stack and print them till a matching ‘(‘ operator is found.
Delete ‘(‘ from top of the stack..
Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form,
So, the Postfix Expression is 23*21-/53*+
Postfix Demo: The Stack
• What is a ‘STACK’?
• At the grocery store, on the canned goods aisle, the cans are
STACKED on top of each other.
Which one do we take to make sure the stack doesn’t
How did the store worker put the cans into the
Where did he or she place the new can?
• We take the top item and we place new items on the top. So does
• To evaluate the problem (1 + (2 * ((3 + (4 * 5)) * 6))), the computer
uses a stack and postfix notation.