将堆栈的值分配给另一个堆栈 [英] Assign values of a stack to another stack

查看:296
本文介绍了将堆栈的值分配给另一个堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理这项作业。

I am working on this assignment.

我需要创建一个没有初始化它的临时堆栈。

I need to create a temporary stack without initializing it.

然后使用循环将堆栈1的项目推入此临时堆栈。

Then push the items of stack 1 into this temporary stack using a while loop.

然后,我需要使用另一个(嵌套?)循环来遍历临时堆栈,并将临时堆栈中的项目添加到堆栈2上。

Then I need to use another (nested?) loop to walk through the temp stack and add the items from temp stack onto stack 2.

然后,我需要设置栈1和2等于栈2保持不变。

Then I need to set stack 1 and 2 equal so stack 2 remains unchanged.

推荐答案

接口看起来有点偏。让我们从那里开始,看看是否让你超过了你的驼峰。

Your interfaces look a bit off. Let's start there and see if that gets you over your hump.

stack.top()项目,但不会删除它。这对于从一个堆栈转移到另一个堆栈似乎没有用。您已经有了isEmptyStack()来检查顶层元素是否存在。

stack.top() usually peeks at an item, but doesn't remove it. This doesn't seem useful for transfering from one stack to anther. You already have isEmptyStack() to check that the top element exists.

stack.pop()从堆栈的顶部项目。这听起来对传输有用。

stack.pop() usually takes the top item from a stack. This sounds useful for transferring.

stack.push(item)将项目放在堆栈顶部。这听起来对转移有用。

stack.push(item) places item onto the top of the stack. This sounds useful for transferring.

stack.push()看起来错了。推送什么?

stack.push() just seems wrong. Push what?

希望实现这些方法后,其余部分将从您提供的问题的英语描述开始有意义。

Hopefully, once you implement these methods, the rest will start to make sense from the english description of the problem you provided.

更新:这是你想要的:

|a  |   |     |   |   |     |   |   |     |   |c  |
|b  |   |     |b  |   |     |   |b  |     |   |b  |
|c  |   |     |c  |a  |     |c  |a  |     |   |a  |
1   tmp 2     1   tmp 2     1   tmp 2     1   tmp 2

|   |   |     |   |   |     |   |   |a
|   |b  |     |   |   |b    |   |   |b
|   |a  |c    |   |a  |c    |   |   |c
1   tmp 2     1   tmp 2     1   tmp 2 

pop,和IsEmptyStack,没有相互分配堆栈(这种做法失败了分配的目的),你能这样做吗?

Now, with just push, pop, and IsEmptyStack, with no assigning of stacks to one another (that sort of defeats the purpose of the assignment), can you do this?

这篇关于将堆栈的值分配给另一个堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆