堆栈 - 为什么是 PUSH 和 POP?

标签 stack

我想知道为什么我们使用术语“push”和“pop”来从堆栈中添加/删除项目?是否有一些物理隐喻导致这些术语变得常见?

我唯一的建议是类似于 spring-loaded magazine for a handgun ,其中回合被“插入”并可以“弹出”,但这似乎不太可能。

第二个堆栈小问题:为什么大多数 CPU 实现调用堆栈随着增长向下在内存中,而不是向上?

最佳答案

对于你的第二个问题,维基百科有一篇关于控制堆栈的CS哲学的文章:

http://en.wikipedia.org/wiki/LIFO

对于第一个,也在维基百科上:

A frequently used metaphor is the idea of a stack of plates in a spring loaded cafeteria stack. In such a stack, only the top plate is visible and accessible to the user, all other plates remain hidden. As new plates are added, each new plate becomes the top of the stack, hiding each plate below, pushing the stack of plates down. As the top plate is removed from the stack, they can be used, the plates pop back up, and the second plate becomes the top of the stack. Two important principles are illustrated by this metaphor: the Last In First Out principle is one; the second is that the contents of the stack are hidden. Only the top plate is visible, so to see what is on the third plate, the first and second plates will have to be removed. This can also be written as FILO-First In Last Out, i.e. the record inserted first will be popped out at last.

关于堆栈 - 为什么是 PUSH 和 POP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/420315/

相关文章:

java - 将 Main 中发生的另一个类文件中启动的数组对象添加到堆栈中

c# - 将数字分成定义的堆栈

c++ - 线程安全堆栈互斥体在忙碌时被销毁

javascript - clearInterval() 如何清除 setInterval() 中的 timerID?

c++ - 我无法从中得到输出

c++ 为什么 .ignore() 函数不能正常工作?

c# - 具有一定堆栈大小的进程?

.net - .NET 堆栈与 Windows 堆栈

bash:在脚本末尾 pushd 后不需要调用 popd 吗?

c++ - 为什么C++ "fill"可以初始化可变大小的数组?