索引0在哪里进行堆栈,顶部或底部?
我只是在学习堆栈,我不明白为什么以下问题的答案是上面的,我认为这是B [2,7]。我被“假设索引0位于堆栈的底部”的“假设”绊倒。
考虑以下代码段:
1 stack = Stack()
2 stack.pop()
3 stack.push(2)
4 stack.push(7)
5 stack.push(1)
6 stack.pop()
一旦执行上述摘要,堆栈中存储的内容是什么? (假设索引0是堆栈的底部。)
A。[2,7,1]
B. [2,7]
C. [2]
d。以上都不是。
I am just learning stacks and I don't understand why the answer to the question below is D None of the above, I thought it was B[2, 7]. I'm getting tripped up by the "Assume that index 0 is at the bottom of the stack".
Consider the following code snippet:
1 stack = Stack()
2 stack.pop()
3 stack.push(2)
4 stack.push(7)
5 stack.push(1)
6 stack.pop()
What is stored in the stack once the above snippet has been executed? (Assume that index 0 is the bottom of the stack.)
A. [2, 7, 1]
B. [2, 7]
C. [2]
D. None of the above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用数组实现的堆栈将从0开始。
在第2行stack.pop()是stackunderflow的情况。考虑到您正在尝试从一个空的堆栈中弹出()。
因此,如果抛出例外,则不会执行其他行。
A stack implemented using an array would start from 0.
At Line 2 stack.pop() is a case of StackUnderFlow. Considering you're trying to pop() from a stack that is empty.
So in case an exception is thrown, further lines would not be executed.