我一直在尝试实现双向链接列表的深层副本,但是我一直在遇到麻烦。我必须做几次不同的操作,但是最终出现地址错误。我只需要解释一下如何正确执行操作即可。
List.H
class List
{
public:
List();
~List();
List(const List& c);
List& operator= (const List& t);
private:
List *Next;
List *Prev;
Node *Head;
List.cpp
List::~List()
{
Node* move = Head;
while (move!=NULL)
{
Node *temp = move->Next;
delete move;
move = temp;
}
}
List::List(const List& c)
{
name = c.name;
if (c.Head == NULL) {
Head = NULL;
}
else {
Head = new Node(*c.Head);
Node* Current = Head;
Node* ObjHead = c.Head;
Node* CurrentObj = ObjHead;
while (Current->Next!=NULL) {
Current->Next = new Node (CurrentObj->Next->condiments);
}
}
}