ec-211 data structures lecture 8. stack applications infix, prefix, and postfix expressions example...

12
EC-211 DATA STRUCTURES LECTURE 8

Upload: audrey-evans

Post on 05-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

EC-211 DATA STRUCTURES

LECTURE 8

Page 2: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

STACK APPLICATIONS

• Infix, Prefix, and Postfix Expressions• Example– Infix: A+B– Prefix: +AB– Postfix: AB+

Page 3: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Postfix

• Example:Infix: A+(B*C)

Convert to PostfixA+(B*C)=A+(BC*)=ABC*+ which is the required postfix form

Page 4: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Postfix

• Example:Infix: (A+B)*(C-D)

Convert to Postfix(A+B)*(C-D) =(AB+)*(CD-)=AB+CD-* which is the required postfix form

Page 5: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Evaluating a Postfix Expression• Each operator in a postfix expression refers to the

previous two operands in the expression.• Each time we reach an operand we push it onto a

stack.• When we reach an operator, its operands will be the

top two elements on the stack.• We then pop these two elements, perform the

indicated operation on them, and push the result on the stack.

• In the end, the result of the expression is left as the only element in the stack, which is popped and returned as the result of evaluation

Page 6: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Algorithm for Evaluating a Postfix Expression opndstk=the empty stack;//scan the input string one element a time into symb while (not end of input) { symb=next input character; if (symb is an operand)

push(opndstk, symb); else {

//symb is an operatoropnd2=pop(opndstk);opnd1=pop(opndstk);value=result of applying symb to opnd1 and opnd2;push(opndstk, value);

} //end else} //end whilereturn (pop(opndstk));

Page 7: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Example

symb opnd1 opnd2 value opndstk

6 6

2 6,2

3 6,2,3

+ 2 3 5 6,5

- 6 5 1 1

3 1,3

8 1,3,8

2 1,3,8,2

/ 8 2 4 1,3,4

+ 3 4 7 1,7

* 1 7 7 7

6 2 3 + - 3 8 2 / + *

Page 8: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Converting an Expression from Infix to Postfix opstk=the empty stack; while (not end of input) {

symb=next input character; if (symb is an operand)

add symb to the postfix string else {

while (!empty(opstk) && (operator at top of the stack has precedence greater than or equal symb)) {

topsymb=pop(opstk);add topsymb to the postfix string;

}push(opstk, symb);

} //end else} //end while//output any remaining operator while (!empty(opstk) {

topsymb=pop(opstk); add topsymb to the postfix string;

} //end while

Page 9: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Example

Symb Postfix string opstkA A+ A +B AB +* AB +*C ABC +*

ABC* +ABC*+

Convert to Postfix:A+B*C

Page 10: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Example

Symb Postfix string opstkA A* A *B AB *+ AB* +C AB*C +* AB*C +*D AB*CD +*

AB*CD* +AB*CD*+

Convert to Postfix:A*B+C*D

Page 11: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

CLASS WORK

• Suppose S is a non-empty stack of integers. Write a main() function that appropriately utilizes calls to push, pop, and empty to remove the bottom element from the stack. Rest of the stack stays the same.

• Remember, you do not know the number of elements currently present in stack

• Prototypes of stack functions:– void push( int)– int pop();– bool empty();

Page 12: EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+

Solution void main(){

stack T;int element;while (!S.empty()) {

element=S.pop();T.push( element);

}element=T.pop(T);while (!T.empty() {

element=T.pop();S.push( element);

}