team 4

13
DATA STRUCTURES STACK APPLICATIONS By Shanthi.S Saradhaswathi.G Thangapriyaa.V.B Sriram.N Ramesh.R Poornachandran.R

Upload: sathasivam-r

Post on 02-Aug-2015

47 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Team 4

DATA STRUCTURES

STACK APPLICATIONS

By

Shanthi.S

Saradhaswathi.G

Thangapriyaa.V.B

Sriram.N

Ramesh.R

Poornachandran.R

Page 2: Team 4

Conversion And Evaluation of Expressions

Group - 4

Page 3: Team 4

Conversion And Evaluation of Expressions

• StackA Stack is an ordered list in which all

insertions and deletions are made at one end called Top.

Page 4: Team 4

Expressions

Expression is a string of operands and operators.Operands are some numeric values and operators

are of two types : Unary operators such as ‘+’ and ‘-’.Binary operators such as ‘+’, ‘-’, ‘*’, ‘/’ and

exponential.

Page 5: Team 4

Types of Expressions

• Infix Expressions

• Postfix Expressions

• Prefix Expressions

Page 6: Team 4

Infix Expressions

• Parenthesis can be used in these expressions.• Infix expressions are the most natural way of

representing expressions.

Infix expression = Operand1 operator Operand2Examples: 1) (a+b)

2) (a+b)*(c-d)3) a+b/c)*(d+f)

Page 7: Team 4

Postfix ExpressionIn postfix expression, there are no

parenthesis used. All the corresponding operands come first and then operator can be placed.Postfix Expression = Operand1 operand2 operator

Examples:1) ab+2) ab+cd-3) ab+c/df+*

Page 8: Team 4

Prefix ExpressionIn prefix expression, there are no

parentheses used. All the corresponding operators come first and then the operands are arranged.Prefix Expression = Operator Operand1 Operand2

Examples:1) +ab2) *+ab-cd3) */+abc+df

Page 9: Team 4

Conversion of Infix to Postfix Expressions:Eg.: ((a-b)*(a+b))

S.No Input Character

Stack Postfix Expressions

1. ( ( 2. ( (( 3. a (( a4. - ((- a5. b ((- ab6. ) ( ab-7. * (* ab-8. ( (*( ab-9. a (*( ab-a

10. + (*(+ ab-a11. b (*(+ ab-ab12. ) (* ab-ab+13. ) Empty ab-ab+*

The postfix expression is ab-ab+*

Page 10: Team 4

Conversion of Postfix to Prefix:Eg.:ABCDE/*-F/G++

Prefix = Operator Operand1 Operand2Step1: ABCDE/*-F/G++Step2: ABC(/DE)*-F/G++Step3: AB(*C/DE)-F/G++Step4: A(-B*C/DE)F/G++Step5: A(/-B*C/DEF)G++Step6: A(+/-B*C/DEFG)+Step7: +A+/-B*C/DEFG

+A+/-B*C/DEFG is the prefix expression.

Page 11: Team 4

Conversion of Postfix to Infix form:Eg.:ABC(DE/*-F/G++

Infix = Operand1 Operator Operand2Step1: ABCDE/*-F/G++Step2: ABC/DE*-F/G++Step3: AB(C*(D/E)-F/G++Step4: A(-B(C*D/E)))F/G++Step5: A(((B-(C(D/E)))/F)G++Step6: A(((B-(C(D/E))/F+G)+Step7: (A+(B-(C*(D/E)))/F)+G))

(A+(B-(C*(D/E)))/F)+G)) is the infix expression.

Page 12: Team 4

Evaluation of ExpressionsAlgorithm:

Add the right paranthesis.Scan the expressions from left to right of each

element until the closed parenthesis is encountered.

If operand is encountered, then Push to Stack.If operator is encountered, then next two

elements from Stack is Poped.Evaluate the expression with the operator .

E.g.(b*c).Set the result to the Top of the Stack.Exit.

Page 13: Team 4

Evaluation of Postfix expressionp=abc*/d+

Assume a=50, b=10, c=7, d=0

Steps Symbols Scanned Stack1. ( 2. 50{a} (,503. 10{b} (,50,104. 7{c} (,50,10,75. * (,50,706. / (,1.47. 2{d} (,1.4,28. + (,3.49. ) 3.4