在我将其全部取下以便打印出来后,我很难重新填充堆栈。我正在使用节点实现,所以我认为这个事实让我感到困惑。如有任何建议,我们将不胜感激,谢谢。
这是我的原始堆栈::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/