0% found this document useful (0 votes)
56 views3 pages

DS Lab4

The document provides instructions for converting infix expressions to postfix expressions using a stack data structure and the precedence and associativity rules for operators. It also includes rules for evaluating a postfix expression by popping operands from the stack and pushing the results. Students are tasked with writing C++ functions to implement infix to postfix conversion and postfix evaluation, and testing them in a main program using classes.

Uploaded by

Kakashi Hataka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

DS Lab4

The document provides instructions for converting infix expressions to postfix expressions using a stack data structure and the precedence and associativity rules for operators. It also includes rules for evaluating a postfix expression by popping operands from the stack and pushing the results. Students are tasked with writing C++ functions to implement infix to postfix conversion and postfix evaluation, and testing them in a main program using classes.

Uploaded by

Kakashi Hataka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF COMPUTER SCIENCE

( Rachna College of Engineering and Technology Gujranwala )

DATA STRUCTURES AND ALGORITHMS

Name: Registration No:

LAB 04

INFIX TO POSTFIX EXPRESSION CONVERSION

AND EVALUATION USING STACK

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.

RULES FOR INFIX TO POSTFIX USING STACK DS –

1. Scan Expression from Left to Right

2. Print OPERANDs as the arrive

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.

6. IF incoming OPERATOR has EQUAL precedence with TOP of Stack, use


ASSOCIATIVITY Rules.

7. For ASSOCIATIVITY of LEFT to RIGHT –

1. POP and print the TOP of stack, then push the incoming OPERATOR

8. For ASSOCIATIVITY of RIGHT to LEFT –

1. PUSH incoming OPERATOR on stack.


9. At the end of Expression, POP & print all  OPERATORS from the stack

10. IF incoming SYMBOL is ‘(‘ PUSH it onto Stack.

11. IF incoming SYMBOL is ‘)’ POP the stack and print OPERATORs till ‘(‘ is found. POP
that ‘(‘

12. IF TOP of stack is ‘(‘ PUSH OPERATOR on Stack

Rules for Evaluation of Postfix Expression

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.

You might also like