我試圖在鏈表中打印節點(轉發方向)。 它的定義如下:在C++中打印鏈表(C++ 98)
struct Node {
string val;
Node* next;
Node* prev;
};
struct Stew {
Node* first;
Node* last;
};
凡燉有兩個特殊的指針,一個指向第一個元素,一個到最後。
我很積極,我所嘗試的是正確的,但實際上並非如此。
void print (const Stew& q, char direction) {
assert (!isEmpty(q));
{
Node* current = new Node;
current = q.first;
cout << current -> val;
while((current -> next) != NULL)
{
current = current -> next;
cout << (current -> val);
}
delete current;
}
我知道那裏有一個邏輯錯誤,但我似乎無法找到它。任何幫助,將不勝感激。
你有沒有考慮過調試它,或者甚至用框和箭頭畫一個圖來檢查你的操作是否正確? –
爲什麼要創建一個新節點來打印鏈表? – PaulMcKenzie
調試此問題的最佳方法是繪製圖片並指出問題的來源。 – KRUKUSA