Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacks - why PUSH and POP?

Tags:

stack

I was wondering why we use the terms "push" and "pop" for adding/removing items from stacks? Is there some physical metaphor that caused those terms to be common?

The only suggestion I have is something like a spring-loaded magazine for a handgun, where rounds are "pushed" into it and can be "popped" out, but that seems a little unlikely.

A second stack trivia question: Why do most CPUs implement the call stack as growing downwards in memory, rather than upwards?

like image 498
Roddy Avatar asked Jan 07 '09 13:01

Roddy


People also ask

Why do we use push and pop?

PUSH vs POPPUSH is used when you want to add more entries to a stack while POP is used to remove entries from it. A stack is so named because it places the individual data entries just like a stack of books. The first one goes to the bottom and you can only add or remove items at the top of the stack.

Why pop is used in stack?

pop() method in Java is used to pop an element from the stack. The element is popped from the top of the stack and is removed from the same. Parameters: The method does not take any parameters. Return Value: This method returns the element present at the top of the stack and then removes it.

What is stack why it is known as LIFO write algorithm of push and pop operation on stack?

LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation.


1 Answers

Re your "second trivial question": I've seen considerable inconsistency in defining what "up" and "down" mean! From early days, some manufacturers and authors drew memory diagrams with low addresses at the top of the page (presumably mimicking the order in which a page is read), while others put high addresses at the top of the page (presumably mimicking graph paper coordinates or the floors in a building).

Of course the concept of a stack (and the concept of addressable memory as well) is independent of such visual metaphors. One can implement a stack which "grows" in either direction. In fact, I've often seen the trick below (in bare-metal level implementations) used to share a region of memory between two stacks:

+---+---+--------   -------+--+--+--+
|   |   |   ->   ...   <-  |  |  |  |
+---+---+--------   -------+--+--+--+
^                                   ^
Stack 1      both stacks      Stack 2
base        "grow" toward        base
              the middle

So my answer is that stacks conceptually never grow either "downward" or "upward" but simply grow from their base. An individual stack may be implemented in either direction (or in neither direction, if it's using a linked representation with garbage collection, in which case the elements may be anywhere in nodespace).

like image 159
joel.neely Avatar answered Oct 30 '22 05:10

joel.neely



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!