im still confuse in RPN will i use insert at bottom of stack or front and then reverse it?
cause my project say's that insert at the bottom of the stack..somebody help me pls

Recommended Answers

All 3 Replies

RPN uses a Last In First Out (LIFO) stack. That is, the last variable pushed on the stack is the first variable popped off the stack.

For example, the following expression:

5 4 3 + *

Assuming our stack is initially empty. The first item is an operand (5), so push it onto a stack. Then we encounter the next operand 4 and push it on the stack as well. 4 now becomes the new top of the stack. Then we do the same with 3 which now becomes the newest top of the stack

Our stack now looks like this 5 4 3 <=== top of stack. The number 3 is at the top

Next we encounter the + (plus sign) operator. We must apply it to the two previously encountered operands which are on the top of the stack. We now pop 3 and 4 off the top of the stack and add them since the + (plus) operator is next in the expression to get 7. So we now push the result 7 back on top of the stack.

Our stack now looks like this 5 7 <=== top of stack. The number 7 is at the top

We now have to address our last operand, the * (multiply sign). Now we must pop the top top operands 5 and 7 off the stack and multiply them to get a result of 35.

Finally, we push 35 back on the stack.

Our stack now looks like this 35 <=== top of stack. The number 35 is at the top

i mean.the codes in stack will i use insert at bottom?? so whenever i enter 5 first it will go to top and 4 to bottom and 3 to bottom

i mean.the codes in stack will i use insert at bottom?? so whenever i enter 5 first it will go to top and 4 to bottom and 3 to bottom

Nope. Nothing goes to the bottom. Everything goes into the stack from the top and everything is removed from the stack at the top. Think in terms of Last In First Out (LIFO)

In your above example 5 will be added (pushed) to the top of the stack. Then 4 will be pushed to the top of the stack and finally 3 will be pushed to the top of the stack.

In order to get the 5 value, we must remove (pop) the 3 value and remove (pop) the 4 value and then finally remove (pop) the 5 value off the top of the stack.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.