链表类的拷贝构造函数需要把整个链表复制一遍!
(有借鉴网友们的文章)
CMyLinkList::CMyLinkList(const CMyLinkList & aList)
{
//cout << "调用拷贝构造函数" << endl;
m_pHeader = new Node();
m_pHeader->next = NULL;
Node* ptr1 = m_pHeader;
Node* ptr2 = aList.m_pHeader->next;
while (ptr2)
{
ptr1->next = new Node();
ptr1 = ptr1->next;
ptr1->data = ptr2->data;
ptr1->next = NULL;
ptr2 = ptr2->next;
}
}
CMyLinkList::~CMyLinkList()
{
cout << "调用析构函数" << endl;
Node* ptrTemp1 = this->m_pHeader;
Node* ptrTemp2 = this->m_pHeader;
while (ptrTemp1->next)
{
ptrTemp2 = ptrTemp1->next;
delete ptrTemp1;
ptrTemp1 = ptrTemp2;
}
delete ptrTemp1;
ptrTemp1 = NULL;
ptrTemp2 = NULL;
m_pHeader = NULL;
}