answersLogoWhite

0

I HOPE THIS WILL HELP U OUT...

ALGO GOES LIKE THIS: Reverrse prreffiix and evalluatte iitt

reverse given prefix expression;

scan the reversed prefix expression;

for each symbol in reversed prefix

if operand then

push its value onto stack S;

if operator then {

pop operand1;

pop operand2;

compute result = operand1 op operand2;

push result back onto stack S;

}

return value at top of stack;

AND EXAMPLE IS:Prefix: */-abc-+def Reversed: fed+-cba-/*

ch action stack

f push f

e push f e

d push f e d

+ pop op1 f e

pop op2 f

calc &push f (d+e)

- pop op1 f

pop op2

calc & push ((d+e)-f)

c push ((d+e)-f) c

b push ((d+e)-f) c b

a push ((d+e)-f) c b a

- pop op1 ((d+e)-f) c b

pop op2 ((d+e)-f) c

calc & push ((d+e)-f) c (a-b)

ch action stack

/ pop op1 ((d+e)-f) c

pop op2 ((d+e)-f)

calc & push ((d+e)-f) ((a-b)/c)

* pop op1 ((d+e)-f)

pop op2

calc & push ((a-b)/c)*((d+e)-f)

User Avatar

Wiki User

14y ago

Still curious? Ask our experts.

Chat with our AI personalities

JudyJudy
Simplicity is my specialty.
Chat with Judy
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran

Add your answer:

Earn +20 pts
Q: What is an algorithm to evaluate prefix expression using stack with example?
Write your answer...
Submit
Still have questions?
magnify glass
imp