不确定为什么节点仍然没有深度复制?头部和大小深得复制完美。单链接列表复制构造函数
似乎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