不確定爲什麼節點仍然沒有深度複製?頭部和大小深得複製完美。單鏈接列表複製構造函數
似乎NewNode-> Data和NewNode-> Next不是深度複製。不知道NewNode是否仍然指向那個 - >數據和那 - >下一步。
創建節點時,是否需要傳遞實際值?
在此先感謝您的答覆。非常感激。
傳入的列表(SLList & that)。
節點是結構。節點不使用尾巴。只有下一個和我的情況大小。
Node* That = that.Head;
//If not null then deep copy.
if (That != nullptr)
{
Head = that.Head;
Size = that.Size;
//Interate through the list until the end nullptr
while (That != nullptr)
{
Node* NewNode = new Node;
NewNode->Data = That->Data;
NewNode->Next = That->Next;
That = That->Next;
}
}
//Set the head to null if list passed in is empty.
else if (That == nullptr)
{
Head = nullptr;
Size = 0;
}
是不是這樣,既然您創建while循環內的NewNode指針,你離開後,指針被刪除? – Foitn
您可能想看到我更正的代碼。 – v78
有人因爲不喜歡這個問題而連續低估每一個答案?我不喜歡代碼風格,但這不是一個不回答問題的理由 – CashCow