DATA STRUCTURES
& ALGORITHMS
INFIX,POSTFIX & PREFIX
EXPRESSIONS USING STACK
Evaluation Process
Distribution of Continuous: Evaluation Tools:
Sessional- I 30% Assignment/Tutorials
Sessional- II 30% Sessional tests
Assignment/Tutorial 20% Surprise questions during
Class Work/ Performance 10% lectures/Class Performance
Attendance 10% End Semester Examination
Topic to be covered CO BTL
Applications of Stacks: Polish 3 2
expressions, Reverse Polish
Expression, conversions and
Evaluation, Recursion
Operators Symbols
Parenthesis ( ), {}, [ ]
Exponents ^
Multiplication and Division *, /
Addition and Subtraction +,-
The first preference is given to the parenthesis; then next preference is given to the
exponents. In the case of multiple exponent operators, then the operation will be
applied from right to left.
Rules for the conversion from infix to postfix expression
1.Print the operand as they arrive.
2.If the stack is empty or contains a left parenthesis on top, push the incoming operator on
to the stack.
3.If the incoming symbol is '(', push it on to the stack.
4.If the incoming symbol is ')', pop the stack and print the operators until the left
parenthesis is found.
5.If the incoming symbol has higher precedence than the top of the stack, push it on the
stack.
6.If the incoming symbol has lower precedence than the top of the stack, pop and print the
top of the stack. Then test the incoming operator against the new top of the stack.
7.If the incoming operator has the same precedence with the top of the stack then use the
associativity rules. If the associativity is from left to right then pop and print the top of the
stack then push the incoming operator. If the associativity is from right to left then push the
incoming operator.
8.At the end of the expression, pop and print all the operators of the stack.
Que. Convert the given infix expression into postfix expression using stack.
Infix expression: K + L - M*N + (O^P) * W/U/V * T + Q
Answer:
The final postfix expression of infix expression(K + L - M*N + (O^P) * W/U/V * T + Q) is
KL+MN*-OP^W*U/V/T*+Q+.
Rules for the conversion of infix to prefix expression:
•First, reverse the infix expression given in the problem.
•Scan the expression from left to right.
•Whenever the operands arrive, print them.
•If the operator arrives and the stack is found to be empty, then simply push the operator into the
stack.
•If the incoming operator has higher precedence than the TOP of the stack, push the incoming
operator into the stack.
•If the incoming operator has the same precedence with a TOP of the stack, push the incoming
operator into the stack.
•If the incoming operator has lower precedence than the TOP of the stack, pop, and print the top
of the stack. Test the incoming operator against the top of the stack again and pop the operator
from the stack till it finds the operator of a lower precedence or same precedence.
•If the incoming operator has the same precedence with the top of the stack and the incoming
operator is ^, then pop the top of the stack till the condition is true. If the condition is not true,
push the ^ operator.
•When we reach the end of the expression, pop, and print all the operators from the top of the
stack.
•If the operator is ')', then push it into the stack.
•If the operator is '(', then pop all the operators from the stack till it finds ) opening bracket in the
stack.
•If the top of the stack is ')', push the operator on the stack.
•At the end, reverse the output.