爲了將其打印出來,我已經很難重新填充堆棧了。我正在使用節點實現,所以我認爲這是令我困惑的事實。任何建議,將不勝感激,謝謝。使用節點實現的重新填充堆棧
這是我原來的堆棧::打印()
// 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;
}
This is my stack with my attempt to loop and store in temp. It compiles but still is not working as suppose to
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; }
'top'是一個成員變量嗎?如果是這樣,你爲什麼要在'print'中修改它? 'print'應該保持你的棧不變。 –
我試圖只修改它來打印堆棧,然後將所有東西都還原回原來的....基本上從頂部獲取信息,打印它然後移動到下一個口香糖。之後,我想將所有的東西從臨時堆棧移回原來的順序。 – 123me