What do Push and Pop mean for Stacks?

后端 未结 9 1085
臣服心动
臣服心动 2021-01-31 18:35

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 importan

相关标签:
9条回答
  • 2021-01-31 18:39

    The algorithm to go from infix to prefix expressions is:

    -reverse input
    
    TOS = top of stack
    If next symbol is:
     - an operand -> output it
     - an operator ->
            while TOS is an operator of higher priority -> pop and output TOS
            push symbol
     - a closing parenthesis -> push it
     - an opening parenthesis -> pop and output TOS until TOS is matching
            parenthesis, then pop and discard TOS.
    
    -reverse output
    

    So your example goes something like (x PUSH, o POP):

    2*3/(2-1)+5*(4-1)
    )1-4(*5+)1-2(/3*2
    
    Next
    Symbol  Stack           Output
    )       x )
    1         )             1
    -       x )-            1
    4         )-            14
    (       o )             14-
            o               14-
    *       x *             14-
    5         *             14-5
    +       o               14-5*
            x +             14-5*
    )       x +)            14-5*
    1         +)            14-5*1
    -       x +)-           14-5*1
    2         +)-           14-5*12
    (       o +)            14-5*12-
            o +             14-5*12-
    /       x +/            14-5*12-
    3         +/            14-5*12-3
    *       x +/*           14-5*12-3
    2         +/*           14-5*12-32
            o +/            14-5*12-32*
            o +             14-5*12-32*/
            o               14-5*12-32*/+
    
    +/*23-21*5-41
    
    0 讨论(0)
  • 2021-01-31 18:48

    Simply:

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

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

    0 讨论(0)
  • 2021-01-31 18:53

    Hopefully this will help you visualize a Stack, and how it works.

    Empty Stack:

    |     |
    |     |
    |     |
    -------
    

    After Pushing A, you get:

    |     |
    |     |
    |  A  |
    -------
    

    After Pushing B, you get:

    |     |
    |  B  |
    |  A  |
    -------
    

    After Popping, you get:

    |     |
    |     |
    |  A  |
    -------
    

    After Pushing C, you get:

    |     |
    |  C  |
    |  A  |
    -------
    

    After Popping, you get:

    |     |
    |     |
    |  A  |
    -------
    

    After Popping, you get:

    |     |
    |     |
    |     |
    -------
    
    0 讨论(0)
  • 2021-01-31 18:53

    The rifle clip analogy posted by Oren A is pretty good, but I'll try another one and try to anticipate what the instructor was trying to get across.

    A stack, as it's name suggests is an arrangement of "things" that has:

    • A top
    • A bottom
    • An ordering in between the top and bottom (e.g. second from the top, 3rd from the bottom).

    (think of it as a literal stack of books on your desk and you can only take something from the top)

    Pushing something on the stack means "placing it on top". Popping something from the stack means "taking the top 'thing'" off the stack.

    A simple usage is for reversing the order of words. Say I want to reverse the word: "popcorn". I push each letter from left to right (all 7 letters), and then pop 7 letters and they'll end up in reverse order. It looks like this was what he was doing with those expressions.

    push(p) push(o) push(p) push(c) push(o) push(r) push(n)

    after pushing the entire word, the stack looks like:

       |  n   |  <- top
       |  r   |
       |  o   |
       |  c   |
       |  p   |
       |  o   |
       |  p   |  <- bottom (first "thing" pushed on an empty stack)
        ======
    

    when I pop() seven times, I get the letters in this order:

    n,r,o,c,p,o,p

    conversion of infix/postfix/prefix is a pathological example in computer science when teaching stacks:

    Infix to Postfix conversion.

    Post fix conversion to an infix expression is pretty straight forward:

    (scan expression from left to right)

    1. For every number (operand) push it on the stack.
    2. Every time you encounter an operator (+,-,/,*) pop twice from the stack and place the operator between them. Push that on the stack:

    So if we have 53+2* we can convert that to infix in the following steps:

    1. Push 5.
    2. Push 3.
    3. Encountered +: pop 3, pop 5, push 5+3 on stack (be consistent with ordering of 5 and 3)
    4. Push 2.
    5. Encountered *: pop 2, pop (5+3), push (2 * (5+3)).

    *When you reach the end of the expression, if it was formed correctly you stack should only contain one item.

    By introducing 'x' and 'o' he may have been using them as temporary holders for the left and right operands of an infix expression: x + o, x - o, etc. (or order of x,o reversed).

    There's a nice write up on wikipedia as well. I've left my answer as a wiki incase I've botched up any ordering of expressions.

    0 讨论(0)
  • 2021-01-31 18:55

    Ok. As the other answerers explained, a stack is a last-in, first-out data structure. You add an element to the top of the stack with a Push operation. You take an element off the top with a Pop operation. The elements are removed in reverse order to the order they were put inserted (hence Last In, First Out). For example, if you push the elments 1,2,3 in that order, the number 3 will be at the top of the stack. A Pop operation will remove it (it was the last in) and leave 2 at the top of the stack.

    Regarding the rest of the lecture, the lecturer tried to describe a stack-based machine that evaluates arithmetic expressions. The machine operates by continuously popping 3 elements from the top of the stack. The first two elements are operands and the third is an operator (+, -, *, /). It then applies this operator on the operands, and pushes the result onto the stack. The process continues until there is only one element on the stack, which is the value of the expression.

    So, suppose we begin by pushing the values "+/*23-21*5-41" in left-to-right order onto the stack. We then pop 3 elements from the top. The last in is first out, which means the first 3 element are "1", "4", and "-" in that order. We push the number 3 (the result of 4-1) onto the stack, then pop the three topmost elements: 3, 5, *. Push the result, 15, onto the stack, and so on.

    0 讨论(0)
  • 2021-01-31 18:56
    • push = add to the stack
    • pop = remove from the stack
    0 讨论(0)
提交回复
热议问题