What do Push and Pop mean for Stacks?

2019-03-09 18:29发布

long story short my lecturer is crap, and was showing us infix to prefix stacks via an overhead projector and his bigass shadow was blocking everything so i missed the important stuff

he was referring to push and pop, push = 0 pop = x

he gave an example but i cant see how he gets his answer at all,

2*3/(2-1)+5*(4-1)

step 1 Reverse : )1-4(*5+)1-2(/3*2 ok i can see that

he then went on writing x's and o's operations and i got totally lost

answer 14-5*12-32*/+ then reversed again to get +/*23-21*5-41

if some one could explain to me the push pop so i could understand i would be very greatful, i have looked online but alot stuff im finding seems to be a step above this, so i really need to get an understanding here first

9条回答
走好不送
2楼-- · 2019-03-09 19:26

A stack in principle is quite simple: imagine a rifle's clip - You can only access the topmost bullet - taking it out is called "pop", inserting a new one is called "push".
A very useful example for that is for applications that allow you to "undo".
Imagine you save each state of the application in a stack. e.g. the state of the application after every type the user makes.
Now when the user presses "undo" you just "pop" the previous state from the stack. For every action the user does - you "push" the new state to the stack (that's of course simplified).
About what your lecturer specifically was doing - in order to explain it some more information would be helpful..

查看更多
Root(大扎)
3楼-- · 2019-03-09 19:28

Simply:

  • pop: returns the item at the top then remove it from the stack

  • push: add an item onto the top of the stack.

查看更多
小情绪 Triste *
4楼-- · 2019-03-09 19:32

after all these good examples adam shankman still can't make sense of it. I think you should open up some code and try it. The second you try a myStack.Push(1) and myStack.Pop(1) you really should get the picture. But by the looks of it, even that will be a challenge for you!

查看更多
登录 后发表回答