The document presents a course on Data Structures, focusing on the evaluation of arithmetic expressions using different notations: infix, prefix, and postfix. It details the precedence of binary operators and provides a step-by-step method for transforming infix expressions to postfix, as well as evaluating postfix expressions. The content is structured for educational purposes by Md. Atiqul Islam Rizvi and Animesh Chandra Roy from the Department of CSE at CUET.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views7 pages
ch06 02
The document presents a course on Data Structures, focusing on the evaluation of arithmetic expressions using different notations: infix, prefix, and postfix. It details the precedence of binary operators and provides a step-by-step method for transforming infix expressions to postfix, as well as evaluating postfix expressions. The content is structured for educational purposes by Md. Atiqul Islam Rizvi and Animesh Chandra Roy from the Department of CSE at CUET.
Arithmetic Expressions Evaluation •For binary operators, there are three types of notation. •Infix – most common, generally used. •Prefix – Polish notation, named after polish mathematician Jan Lukasiewicz •Postfix – Reverse Polish notation
•In case of polish and reverse polish, we never need to use
parentheses.
Data Structures with C 4
Transform Infix to Postfix Expression •Suppose, Q is an expression written in infix notation. Equivalent postfix notation is P. Then, 1. Push ‘(‘ onto STACK, add ‘)’ at the end of Q 2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until STACK is empty: 3. If operand, add it to P. 4. If left parenthesis, push it onto STACK. 5. If an operator , then: a) Repeatedly pop from STACK and add to P each operator which has the same or higher precedence than . b) Add to STACK. [End IF] Data Structures with C 5 Transform Infix to Postfix Expression (Cont.) •Suppose, Q is an expression written in infix notation. Equivalent postfix notation is P. Then, 6. If right parenthesis, then: a) Repeatedly pop from STACK and add to P each operator until a left parenthesis is found. b) Remove the left parenthesis. [End IF] [End of step 2 loop] 7. Exit.
Data Structures with C 6
Postfix Expression Evaluation •Suppose, P is an expression written in postfix. Then, 1. Add a right parenthesis ‘)’ at the end of P. 2. Scan P from left to right and repeat steps 3 and 4 for each element until ‘)’ is found: 3. If an operand, put it on STACK. 4. If an operator , then: a) Remove top two elements of STACK, where A is top and B next-to-top. b) Evaluate B A c) Place result of (b) back on STACK [End IF] [End step 2 loop] 5. Set VALUE equal to top element of STACK and Exit. Data Structures with C 7