gpt4 book ai didi

堆栈 - 为什么是 PUSH 和 POP?

转载 作者:行者123 更新时间:2023-12-03 23:59:12 25 4
gpt4 key购买 nike

我想知道为什么我们使用术语“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com