实际上“实现堆栈”意味着什么?

发布于 2024-09-09 04:45:04 字数 318 浏览 3 评论 0原文

我的教授是否要求我绘制堆栈?他想让我画出它的实际效果吗?我觉得自己很愚蠢,但这不像任何人告诉过我的那样!感谢您的帮助。

哇你们真快。已经谢谢你了。完整的问题是:考虑两个堆栈,每个堆栈的大小为 n(即每个堆栈最多可以容纳 n 个元素)。如果两个堆栈中的元素数量之和为 n,则任何额外的 PUSH 操作都会导致溢出错误。 (注意:您的实现应该考虑到以下事实:元素 POP 的顺序应与 PUSH 的顺序相反)。

***我不是在寻求答案,我只是想知道...你认为他要我做什么?因为他还没有回复我的电子邮件,我需要在午夜之前完成。

Is my professor asking me to draw the stack? Does he want me to draw it in action? I feel stupid but it's not like anyone ever told me! Thank you for your help.

Wow you guys are quick. Thank you already. The complete question is: Consider two stacks, each of size n (i.e., each one can hold a maximum of n elements). If the sum of the number of elements in the two stacks is n, then any additional PUSH operation should result in an overflow error. (Note: Your implementation should take care of the fact that elements
should POP opposite to the order in which they are PUSHed).

***I'm not asking for the answer I'm just wondering... what do you think he's asking me to do? Cause he still hasn't answered my email and I need it done by midnight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

醉殇 2024-09-16 04:45:04

“实施”通常意味着编写,纯粹而简单。您的教育者希望您编写能够完成作业所说的代码。

固定大小 (n) 的堆栈可以轻松实现为具有当前堆栈深度的数组,但您的分配有一个额外的扭曲,因为您只允许有 n两个堆栈上的 code> 元素组合在一起,而不是每个堆栈上。

因为它是家庭作业,并且无论如何,您还没有指定语言):

# Create the two stacks, each of size sz.
init_stack (sz):
    allocate stack1 as array[1 to sz] of integer
    allocate stack2 as array[1 to sz] of integer
    set stack1sz to 0
    set stack2sz to 0
    set maxsz to sz

我将按如下方式实现它(仅是伪代码,

# Push the value val onto stack stk.
push_stack (stk,val):
    if stk is not equal to 1 or 2:
        return error
    if stack1sz + stack2sz is equal to maxsz:
        return error
    if stk is 1:
        add 1 to stack1sz
        set element stack1sz of stack1 to val
    else:
        add 1 to stack2sz
        set element stack2sz of stack2 to val

 

# Pop a value off stack stk.
pop_statkck (stk):
    if s is not equal to 1 or 2:
        return error
    if stk is 1:
        if stack1sz is 0:
            return error
        set val to element stack1sz of stack1
        subtract 1 from stack1sz
    else:
        if stack2sz is 0:
            return error
        set val to element stack2sz of stack2
        subtract 1 from stack2sz
    return val

变量 stack1stack2stack1szstack2szmaxsz 应在以下位置声明:这样它们就可以在函数调用之间生存(即不是局部变量)。所有其他的都是暂时的。

如果您在将其转换为特定语言时遇到问题,请发表评论,我将提供有关查找内容的指导。

"Implement" generally means write, pure and simple. Your educator wants you to write code that can do what the assignment says.

Stacks of fixed size (n) can be easily implemented as an array with a current stack depth but you have an extra twist to your assignment inasmuch as you're only alowed to have n elements on both stacks combined rather than each stack.

I would implement it as follows (pseudo-code only since it's homework and, in any case, you haven't specified a language):

# Create the two stacks, each of size sz.
init_stack (sz):
    allocate stack1 as array[1 to sz] of integer
    allocate stack2 as array[1 to sz] of integer
    set stack1sz to 0
    set stack2sz to 0
    set maxsz to sz

 

# Push the value val onto stack stk.
push_stack (stk,val):
    if stk is not equal to 1 or 2:
        return error
    if stack1sz + stack2sz is equal to maxsz:
        return error
    if stk is 1:
        add 1 to stack1sz
        set element stack1sz of stack1 to val
    else:
        add 1 to stack2sz
        set element stack2sz of stack2 to val

 

# Pop a value off stack stk.
pop_statkck (stk):
    if s is not equal to 1 or 2:
        return error
    if stk is 1:
        if stack1sz is 0:
            return error
        set val to element stack1sz of stack1
        subtract 1 from stack1sz
    else:
        if stack2sz is 0:
            return error
        set val to element stack2sz of stack2
        subtract 1 from stack2sz
    return val

The variables stack1, stack2, stack1sz, stack2sz and maxsz should be declared in such a way that they survive in between function calls (i.e., not locals). All others are transitory.

If you have troubles converting that to a specific language, leave a comment and I'll provide pointers on what to look for.

挽心 2024-09-16 04:45:04

根据你的教授的描述,他几乎肯定会要求你手写一个堆栈。

也就是说,编写(编码)您的堆栈抽象数据类型、其相关操作 - 至少压入和弹出,以及某种驱动程序来测试您的堆栈是否按预期工作并遵循规范(溢出错误等) )。

你了解堆栈吗?如果有的话,您是否参加过有关堆栈的讲座/实验室?您的教科书或最喜欢的网站对它们有何评价?

至于在午夜之前完成这项作业,我当然会尽力手工写一个堆栈,并画出每一步发生的情况(也许作为一个单独的文档,或者如果这是电子提交,则嵌入评论中)。你可能不会得到很好的分数,因为你似乎不知道该怎么做,但你希望能学到一些东西,并表明你在这项作业中付出了一点努力。

Given your professor's description, he is almost certainly asking you to write a stack by hand.

That is, write (code) your Stack Abstract Data Type, its associated operations - push, and pop, at the very least, and some sort of driver to test that your stack works as expected and follows the specification (overflow errors and the like).

Do you know anything about stacks? Have you been to the lecture/lab on stacks if one was available? What does your textbook or favourite website say about them?

As for finishing this assignment by midnight, I would certainly try my best to write a stack by hand and to draw what is happening at each step (perhaps as a separate document, or embedded in the comments if this is electronic submission). You probably won't get a great mark since you don't seem to know what to do, but you will have hopefully learned something and shown that you put a little bit of effort into this assignment.

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