在单链表C++中实现复制构造函数
Implementing copy constructor in a single linked list C++
我有一个C++代码:
#include <iostream>
using namespace std;
struct Node;
typedef Node *NodePtr;
struct Node
{
int Item;
NodePtr Next;
};
class LinkedList
{
public:
LinkedList(); // default constructor
~LinkedList(); // destructor
void AddTail(int); // adds item to tail
private:
NodePtr Head;
};
LinkedList::LinkedList()
{
Head = NULL; //declare head as null
}
//Adding on tail
void LinkedList::AddTail(int Item)
{
NodePtr Crnt;
NodePtr node = new Node;
node->Item = Item;
node->Next = NULL;
//if head is in null declare the added node as head
if (Head == NULL)
{
Head = node;
}
else
{ //set the current to head, move the current node to current next node
Crnt = Head;
while (Crnt->Next != NULL)
{
Crnt = Crnt->Next;
}
//Add item to the tail of the linked list
Crnt->Next = node;
}
}
int main()
{
LinkedList la;
la.AddTail(30);
la.AddTail(60);
la.AddTail(90);
LinkedList lb;
return 0;
}
所以我的问题是如何实现一个复制构造函数(假设在对象 lb 上)来深度复制列表参数并添加代码来测试空列表和非空列表上的复制构造函数?
提前致谢。
试试这个(https://ideone.com/9lywXc 使用您原来发布的代码)
LinkedList::LinkedList(const LinkedList& other):Head(nullptr)
{
cout << "copy constructor called:\n";
if(other.Head == nullptr) return;
NodePtr dummyHead = new Node;
NodePtr curr = dummyHead;
NodePtr othcurr = other.Head;
for(; othcurr!=nullptr; othcurr = othcurr->Next)
{
curr->Next = new Node;
curr = curr->Next;
cout << (curr->Item = othcurr->Item) << ",";
curr->Next = nullptr;
}
Head = dummyHead->Next;
delete dummyHead;
}
int main()
{
LinkedList la;
la.AddTail(30);
la.AddTail(60);
la.AddTail(90);
LinkedList lb(la);
return 0;
}
输出:
复制构造函数调用:
30,60,90,
编程的重要规则之一是不要重复自己 (DRY)。如果你有一个添加功能并且你知道它有效,请继续将它用于与添加相关的工作。这意味着保持愚蠢和多才多艺符合您的最大利益。
应用 DRY 哲学,假设 AddTail
方法正常工作,复制构造函数非常简单:为源列表中的每个节点调用 AddTail
。
LinkedList::LinkedList(const LinkedList & src):Head(nullptr)
{
NodePtr node = src.Head;
while (node != nullptr)
{
AddTail(node->Item);
node = node->Next;
}
}
并且拥有一个有效的复制构造函数使得赋值运算符,感谢 Copy and Swap Idiom,也简单得可笑:
LinkedList & LinkedList::operator=(LinkedList src)
// pass by reference performs the copy
{
std::swap(Head, src.Head); // now just swap the head of the copy
// for the head of the source
return *this;
} // destructor fires for src and cleans up all the nodes that were on this list
要完成 The Rule of Three 三重奏,我们需要一个析构函数。这像
复制构造函数是 DRY 的应用:一遍又一遍地调用节点删除函数,直到列表为空。删除函数几乎是任何链表的必然要求,所以这里我假设有一个叫做 Remove
.
LinkedList::~LinkedList()
{
while (Head != nullptr)
{
NodePtr temp = Head;
Remove(Head);
delete temp;
}
}
所以现在基于链接列表无法运行的两个函数,我们已经实现了基本维护所需的所有其他函数。您所需要的一切都经过测试且没有错误 Add
和 Remove
功能,其余的几乎免费。
而且因为 AddTail
函数击中了我的宠物豌豆之一......这里有一个技巧可以大大降低函数的复杂性:
void LinkedList::AddTail(int Item)
{
NodePtr *Crnt = &Head; // instead of pointing where Head points, point at
// Head now we don't care if it is head or any
// other node's Next. They are all abstracted to
// the same thing: A pointer to where the next
// node can be found
while (*Crnt != NULL) // keep looking until end of list
{
Crnt = &(*Crnt)->Next; // by pointing at the next Next pointer
}
//Add item to the tail of the linked list
NodePtr node = new Node;
node->Item = Item;
node->Next = NULL;
*Crnt = node; // Now just plop the new node over top of the terminating NULL
}
我未实现的 Remove
函数使用相同的指针到指针技巧。
这是对 user4581301 先前答案的改进,因此复制构造函数在输入列表大小上为 O(n)。在封装列表中使用一个额外的指针跟踪尾部 class:
class LinkedList
{
public:
LinkedList():Head(nullptr),Tail(nullptr){}
LinkedList(const LinkedList& other);
~LinkedList() = default; // destructor
void AddTail(int); // adds item to tail
private:
NodePtr Head;
//KEEP TRACK OF TAIL POINTER WITH EXTRA MEMBER
NodePtr Tail;
};
//via user4581301 response
LinkedList::LinkedList(const LinkedList & src):Head(nullptr),Tail(nullptr)
{
NodePtr node = src.Head;
while (node != nullptr)
{
AddTail(node->Item);
node = node->Next;
}
}
//Adding on tail
void LinkedList::AddTail(int Item)
{
NodePtr np = new Node{Item,nullptr};
if(Tail == nullptr && Head == nullptr)
Head = Tail = np;
else
{
Tail->Next = np;
Tail = Tail->Next;
}
}
关于测试:
您可以添加功能来吐出列表的内容。或者您可以将其子类化为测试扩展。或者你可以像这样打破封装,使用 operator<<() :
class LinkedList
{
public:
LinkedList():Head(nullptr),Tail(nullptr){}
LinkedList(const LinkedList& other);
~LinkedList() = default; // destructor
void AddTail(int); // adds item to tail
//breaks encapsulation but make some nice sugar to look inside
friend ostream& operator<<(ostream& s, LinkedList& l)
{
s << "list contents: ";
NodePtr c = l.Head;
for(; c!=nullptr;c=c->Next)
s << c->Item << " ";
s << endl;
return s;
}
private:
NodePtr Head;
NodePtr Tail;
};
所以
int main()
{
LinkedList la;
la.AddTail(30);
la.AddTail(60);
la.AddTail(90);
LinkedList lb(la);
cout << lb;
return 0;
}
吐出:
列表内容:30 60 90
我有一个C++代码:
#include <iostream>
using namespace std;
struct Node;
typedef Node *NodePtr;
struct Node
{
int Item;
NodePtr Next;
};
class LinkedList
{
public:
LinkedList(); // default constructor
~LinkedList(); // destructor
void AddTail(int); // adds item to tail
private:
NodePtr Head;
};
LinkedList::LinkedList()
{
Head = NULL; //declare head as null
}
//Adding on tail
void LinkedList::AddTail(int Item)
{
NodePtr Crnt;
NodePtr node = new Node;
node->Item = Item;
node->Next = NULL;
//if head is in null declare the added node as head
if (Head == NULL)
{
Head = node;
}
else
{ //set the current to head, move the current node to current next node
Crnt = Head;
while (Crnt->Next != NULL)
{
Crnt = Crnt->Next;
}
//Add item to the tail of the linked list
Crnt->Next = node;
}
}
int main()
{
LinkedList la;
la.AddTail(30);
la.AddTail(60);
la.AddTail(90);
LinkedList lb;
return 0;
}
所以我的问题是如何实现一个复制构造函数(假设在对象 lb 上)来深度复制列表参数并添加代码来测试空列表和非空列表上的复制构造函数? 提前致谢。
试试这个(https://ideone.com/9lywXc 使用您原来发布的代码)
LinkedList::LinkedList(const LinkedList& other):Head(nullptr)
{
cout << "copy constructor called:\n";
if(other.Head == nullptr) return;
NodePtr dummyHead = new Node;
NodePtr curr = dummyHead;
NodePtr othcurr = other.Head;
for(; othcurr!=nullptr; othcurr = othcurr->Next)
{
curr->Next = new Node;
curr = curr->Next;
cout << (curr->Item = othcurr->Item) << ",";
curr->Next = nullptr;
}
Head = dummyHead->Next;
delete dummyHead;
}
int main()
{
LinkedList la;
la.AddTail(30);
la.AddTail(60);
la.AddTail(90);
LinkedList lb(la);
return 0;
}
输出:
复制构造函数调用:
30,60,90,
编程的重要规则之一是不要重复自己 (DRY)。如果你有一个添加功能并且你知道它有效,请继续将它用于与添加相关的工作。这意味着保持愚蠢和多才多艺符合您的最大利益。
应用 DRY 哲学,假设 AddTail
方法正常工作,复制构造函数非常简单:为源列表中的每个节点调用 AddTail
。
LinkedList::LinkedList(const LinkedList & src):Head(nullptr)
{
NodePtr node = src.Head;
while (node != nullptr)
{
AddTail(node->Item);
node = node->Next;
}
}
并且拥有一个有效的复制构造函数使得赋值运算符,感谢 Copy and Swap Idiom,也简单得可笑:
LinkedList & LinkedList::operator=(LinkedList src)
// pass by reference performs the copy
{
std::swap(Head, src.Head); // now just swap the head of the copy
// for the head of the source
return *this;
} // destructor fires for src and cleans up all the nodes that were on this list
要完成 The Rule of Three 三重奏,我们需要一个析构函数。这像
复制构造函数是 DRY 的应用:一遍又一遍地调用节点删除函数,直到列表为空。删除函数几乎是任何链表的必然要求,所以这里我假设有一个叫做 Remove
.
LinkedList::~LinkedList()
{
while (Head != nullptr)
{
NodePtr temp = Head;
Remove(Head);
delete temp;
}
}
所以现在基于链接列表无法运行的两个函数,我们已经实现了基本维护所需的所有其他函数。您所需要的一切都经过测试且没有错误 Add
和 Remove
功能,其余的几乎免费。
而且因为 AddTail
函数击中了我的宠物豌豆之一......这里有一个技巧可以大大降低函数的复杂性:
void LinkedList::AddTail(int Item)
{
NodePtr *Crnt = &Head; // instead of pointing where Head points, point at
// Head now we don't care if it is head or any
// other node's Next. They are all abstracted to
// the same thing: A pointer to where the next
// node can be found
while (*Crnt != NULL) // keep looking until end of list
{
Crnt = &(*Crnt)->Next; // by pointing at the next Next pointer
}
//Add item to the tail of the linked list
NodePtr node = new Node;
node->Item = Item;
node->Next = NULL;
*Crnt = node; // Now just plop the new node over top of the terminating NULL
}
我未实现的 Remove
函数使用相同的指针到指针技巧。
这是对 user4581301 先前答案的改进,因此复制构造函数在输入列表大小上为 O(n)。在封装列表中使用一个额外的指针跟踪尾部 class:
class LinkedList
{
public:
LinkedList():Head(nullptr),Tail(nullptr){}
LinkedList(const LinkedList& other);
~LinkedList() = default; // destructor
void AddTail(int); // adds item to tail
private:
NodePtr Head;
//KEEP TRACK OF TAIL POINTER WITH EXTRA MEMBER
NodePtr Tail;
};
//via user4581301 response
LinkedList::LinkedList(const LinkedList & src):Head(nullptr),Tail(nullptr)
{
NodePtr node = src.Head;
while (node != nullptr)
{
AddTail(node->Item);
node = node->Next;
}
}
//Adding on tail
void LinkedList::AddTail(int Item)
{
NodePtr np = new Node{Item,nullptr};
if(Tail == nullptr && Head == nullptr)
Head = Tail = np;
else
{
Tail->Next = np;
Tail = Tail->Next;
}
}
关于测试: 您可以添加功能来吐出列表的内容。或者您可以将其子类化为测试扩展。或者你可以像这样打破封装,使用 operator<<() :
class LinkedList
{
public:
LinkedList():Head(nullptr),Tail(nullptr){}
LinkedList(const LinkedList& other);
~LinkedList() = default; // destructor
void AddTail(int); // adds item to tail
//breaks encapsulation but make some nice sugar to look inside
friend ostream& operator<<(ostream& s, LinkedList& l)
{
s << "list contents: ";
NodePtr c = l.Head;
for(; c!=nullptr;c=c->Next)
s << c->Item << " ";
s << endl;
return s;
}
private:
NodePtr Head;
NodePtr Tail;
};
所以
int main()
{
LinkedList la;
la.AddTail(30);
la.AddTail(60);
la.AddTail(90);
LinkedList lb(la);
cout << lb;
return 0;
}
吐出: 列表内容:30 60 90