Algorithm to evaluate expression given in postfix notation , Data Structure & Algorithms

Assignment Help:

Q. Write down an algorithm to evaluate an expression given to you in postfix notation. Show the execution of your algorithm for the following given expression.

AB^CD-EF/GH+/+*                                                                               

Ans.

Algorithm to evaluate Post fix Expression is shown as follows

 

Opndstk = the empty stack;

/*scan the input string reading one */

/*element at 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 operator */

opnd 2 = Pop (opnd stk);

opnd 1 = Pop (opnd stk);

value = result of applying symb to opnd 1 and opnd 2;

push (opndstk,value);

}/*end else */

}/*end while */

return (pop (opnd stk));

AB^CD-EF/GH+/+*

Symb

Opnd1

Opnd2

Value

Opndstk

A

 

 

 

A

B

 

 

 

A,B

^

A

B

A^B

A^B

C

A

B

A^B

A^B,C

D

A

B

A^B

A^B,C,D

-

C

D

C-D

A^B,C-D

E

C

D

C-D

A^B,C-D,E

F

C

D

C-D

A^B,C-D,E,F

/

E

F

E/F

A^B,C-D,E/F

G

E

F

E/F

A^B,C-D,E/F,G

H

E

F

E/F

A^B,C-D,E/F,G,H

+

G

H

G+H

A^B,C-D,E/F,G+H

/

E/F

G+H

(E/F)/(G+H)

A^B,C-D, (E/F) /(G+H)

+

C-D

(E/F)/(G+H)

(C-D)+(E/F)/(G+H)

A^B,(C-D)+

(E/F)/(G+H)

*

A^B

(C-

D)+(E/F)/(G+H)

A^B*((C-D)+

(E/F)/(G+H))

A^B*((C-D)+

(E/F)/(G+H))


Related Discussions:- Algorithm to evaluate expression given in postfix notation

Algorithm for inorder traversals, Step-1: For the current node, verify whet...

Step-1: For the current node, verify whether it contain a left child. If it has, then go to step-2 or else go to step-3 Step-2: Repeat step-1 for left child Step-3: Visit (th

State the output of avaerage value of numbers, Draw trace table and determi...

Draw trace table and determine output from the subsequent flowchart using below data:  X = 5, -3, 0, -3, 7, 0, 6, -11, -7, 12

What is keyed access- container, What is Keyed Access- Container A c...

What is Keyed Access- Container A collection may allow its elements to be accessed by keys. For instance, maps are unstructured containers which allows their elements to be

Implement stack using two queues, How To implement stack using two queues ,...

How To implement stack using two queues , analyze the running time of the stack operations ?

linear-expected-time algorithm, Implement a linear-expected-time algorithm...

Implement a linear-expected-time algorithm for selecting the k th smallest element Algorithm description 1. If |S| = 1, then k = 1 and return the element in S as the an

Which data structure is used for implementing recursion, Which data structu...

Which data structure is used for implementing recursion Stack.

What are the specific needs for realism, Normal 0 false false...

Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

What are the dynamic arrays, What are the Dynamic arrays Dynamic arrays...

What are the Dynamic arrays Dynamic arrays are convenient for programmers since they can never be too small-whenever more space is needed in a dynamic array, it can simply be e

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd