DS Lab4
DS Lab4
LAB 04
INFIX: the expressions in which operands surround the operator, e.g. x+y, 6*3 etc this way of
writing the Expressions is called infix notation.
POSTFIX: Postfix notation are also Known as Reverse Polish Notation (RPN). They are
different from the infix and prefix notations in the sense that in the postfix notation, operator
comes after the operands, e.g. xy+, xyz+* etc.
3. If OPERATOR arrives & Stack is empty, push this operator onto the stack
4. IF incoming OPERATOR has HIGHER precedence than the TOP of the Stack, push it on
stack
5. IF incoming OPERATOR has LOWER precedence than the TOP of the Stack, then POP
and print the TOP. Then test the incoming operator against the NEW TOP of stack.
1. POP and print the TOP of stack, then push the incoming OPERATOR
11. IF incoming SYMBOL is ‘)’ POP the stack and print OPERATORs till ‘(‘ is found. POP
that ‘(‘
1. While reading the expression from left to right, push the element in the stack if it is an
operand.
2. Pop the two operands from the stack, if the element is an operator and then evaluate it.
3. Push back the result of the evaluation. Repeat it till the end of the expression.
LAB TASK
1- Write a C++ Function to Convert a given infix expression into postfix expression using the
rules mentioned in the manual.
2- Write Function to Evaluate the Postfix Expression.
3- Write main program to test your code.
NOTE: You can use the Stack ADT as provided by C++ Compiler or you can use your own
stack code.
REMEMBER: Write your Code in Classes.