c++ - 使用节点实现重新填充堆栈

标签 c++ stack implementation nodes fill

在我将其全部取下以便打印出来后,我很难重新填充堆栈。我正在使用节点实现,所以我认为这个事实让我感到困惑。如有任何建议,我们将不胜感激,谢谢。

这是我的原始堆栈::print()

<pre><code>// Function to print Gumball info field (color and counter) void Stack::print() { Node *p; Type x; while(top != NULL) { p = top; x = p -> getinfo(); cout << " " << x.color << " " << " " << x.counter << endl << endl; top = p -> getnext(); } return; } </code></pre> <p>This is my stack with my attempt to loop and store in temp. It compiles but still is not working as suppose to <code></code></p><code> </code><pre><code>void Stack::print() { Node *p,*q; Type x,x_temp; while(top != NULL) { p = top; x = p -> getinfo(); cout << " " << x.color << " " << " " << x.counter << endl << endl; x_temp = x; q = top; top = p -> getnext(); } while(top != NULL) { q -> setinfo(x_temp); q -> setnext(top); top = q; } return; } </code></pre>

最佳答案

如果你想打印堆栈的内容,然后以相同的顺序将所有值推回,我建议在打印后将值插入不同的堆栈。然后,一旦原始堆栈为空,将第二个堆栈中的所有值弹出回原始堆栈。

关于c++ - 使用节点实现重新填充堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7975858/

相关文章:

c++ - 使用自定义分配器及其替代方案重载基本类型

c++ - 如何将从文本文件中读取的元素推送和弹出到 C++ 中的数组并以相反的顺序输出堆栈?

c++ - std::vector 不保留在实现中实例化的推回对象

java - 球体上距离另一点最近的点

c++ - 自定义 QWIzard 中的按钮?

c++ - 从标题中删除 map 和设置

c++ - C++03 中的函数返回类型推导

language-agnostic - 什么是堆栈溢出?

c++数据结构保持按值排序并操作FIFO

c++ - 我们如何在 C++ 实现文件中包含结构?