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

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))

Posted Date: 7/13/2012 1:53:38 AM | Location : United States







Related Discussions:- Algorithm to evaluate expression given in postfix notation , Assignment Help, Ask Question on Algorithm to evaluate expression given in postfix notation , Get Answer, Expert's Help, Algorithm to evaluate expression given in postfix notation Discussions

Write discussion on Algorithm to evaluate expression given in postfix notation
Your posts are moderated
Related Questions
a) Given a digraph G = (V,E), prove that if we add a constant k to the length of every arc coming out from the root node r, the shortest path tree remains the same. Do this by usin


List areutilized to maintainPOLYNOMIALS in the memory. For example, we have a functionf(x)= 7x 5 + 9x 4   - 6x³ + 3x². Figure depicts the representation of a Polynomial by means o

In the book the following methods are presented: static void selectionSort(Comparable[] list) static void insertionSort(Comparable[] list) static boolean linearSearch(Comparable

discuss the operating system under the following: MONOLITHIC SYSTEM,LAYER SYSTEM AND VIRTUAL MACHINES

Instructions Design and test a reference array. Reference array stores the references to user supplied objects of different types. Just think it as a heterogeneous array wh

What are the conditions under which sequential search of a list is preferred over binary search? Sequential Search is a preferred over binary search when the list is unordered

The class Element represents a single node that can be part of multiple elements on a hotplate and runs in its own thread. The constructor accepts the initial temperature and a hea


Define Spanning Tree A Spanning Tree of a connected graph is its linked acyclic sub graph (i.e., a tree) that having all the vertices of the graph.