索引0在哪里进行堆栈,顶部或底部?

发布于 2025-01-22 04:26:31 字数 336 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2025-01-29 04:26:31

使用数组实现的堆栈将从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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文