C++ 如何为链表创建副本构造函数?
我完成了这个程序中的每一个函数,我大部分都得到了概念,但是链表的复制构造函数让我感到困惑。我看了关于这个问题的其他答案,但我不知道如何将其应用到我的情况中 我有三个文件,一个包含main()的test.cpp、一个IntList.cpp和一个IntList.h test.cpp和IntList.h是由我的教授提供的,因此可以安全地假设那里没有错误。我只需要写IntList.cppC++ 如何为链表创建副本构造函数?,c++,class,linked-list,copy-constructor,singly-linked-list,C++,Class,Linked List,Copy Constructor,Singly Linked List,我完成了这个程序中的每一个函数,我大部分都得到了概念,但是链表的复制构造函数让我感到困惑。我看了关于这个问题的其他答案,但我不知道如何将其应用到我的情况中 我有三个文件,一个包含main()的test.cpp、一个IntList.cpp和一个IntList.h test.cpp和IntList.h是由我的教授提供的,因此可以安全地假设那里没有错误。我只需要写IntList.cpp #include <iostream> #include <cstdlib> #includ
#include <iostream>
#include <cstdlib>
#include "IntList.h"
using namespace std;
IntList::IntList()
{
head = NULL;
}
IntList::IntList(const IntList &)
{
???
}
// Specification file for the IntList class
#ifndef INTLIST_H
#define INTLIST_H
class IntList
{
private:
// Declare a structure for the list
struct ListNode
{
int value;
struct ListNode *next;
};
ListNode *head; // List head pointer
public:
// Constructor
IntList();
// Copy constructor
IntList(const IntList &);
// Destructor
~IntList();
// List operations
void appendNode(int val);
void removeByVal(int val);
void displayList();
void insertByPos(int val, int pos);
void removeByPos(int pos);
int search(int val);
};
#endif /* INTLIST_H_ */
编辑:
我正在读你们的评论,但它只是不适合我
我试图重写代码,但仍然没有意义。这是我的尝试,我觉得我只是不明白这应该是什么样子
IntList::IntList(const IntList &list) // maybe I name it list so I can refer to list.head?
{
ListNode *nodePtr;
nodePtr = list.head;
if (nodePtr == NULL) // If the head of list is empty then theres no list to copy
{
return;
}
while (nodePtr->next != 0) // Trying to iterate through the link
{
nodePtr = nodePtr->next;
}
ListNode *newNode;
nodePtr->next = newNode;
// ??? Confused again.
这是我的displayList()函数
这些对我来说很有意义,我很快就完成了。我不知道如何在复制构造函数中实现这些想法。你们能帮我弄清楚我没有得到什么吗?给你们
IntList::IntList( const IntList &list ) : head( nullptr )
{
ListNode **new_node = &this->head;
for ( auto current = list.head; current != nullptr; current = current->next )
{
*new_node = new ListNode { current->value, nullptr };
new_node = &( *new_node )->next;
}
}
如果您很难理解如何处理指向指针的指针,那么我可以建议另一个不使用指向指针的指针的构造函数定义
IntList::IntList( const IntList &list ) : head( nullptr )
{
if ( list.head != nullptr )
{
this->head = new ListNode { list.head->value, nullptr };
for ( auto new_node = this->head, current = list.head->next;
current != nullptr;
new_node = new_node->next, current = current->next )
{
new_node->next = new ListNode { current->value, nullptr };
}
}
}
因为您已经实现了
displayList
,所以您知道如何迭代列表。由于已经实现了appendNode
,因此您知道如何添加节点。因此,仔细查看作为参数传递给复制构造函数的列表,并将具有相同值的节点插入到该列表中。还记得链接列表最初是如何构建的吗?一次一个节点。枚举该列表,对于每个节点,为您的复制目标创建一个列表,在您的情况下,您可以枚举源,对于每个节点,在复制目标上激发appendNode
(此)。与你的说法相反,我发现几乎不可能相信简单的搜索没有任何用处。我添加了一个编辑,试图更好地解释我的困惑。我想我没有得到什么需要用复制构造函数做的不同,什么可以保持与我以前做过的相同@Igortandtniki添加了一个编辑,试图更好地解释我的困惑。我在这里找到了复制构造函数的其他示例,但让我困惑的是,对于我的.h文件,我只有*head和*next指针可以使用。我可以做一些像list.head->next这样的事情,然后重复一下吗?我不明白这个概念,我的课本和网上的例子似乎都没有缓解它。我相信这件事很简单,我知道@谁感谢你!这确实有点帮助
IntList::IntList( const IntList &list ) : head( nullptr )
{
ListNode **new_node = &this->head;
for ( auto current = list.head; current != nullptr; current = current->next )
{
*new_node = new ListNode { current->value, nullptr };
new_node = &( *new_node )->next;
}
}
IntList::IntList( const IntList &list ) : head( nullptr )
{
if ( list.head != nullptr )
{
this->head = new ListNode { list.head->value, nullptr };
for ( auto new_node = this->head, current = list.head->next;
current != nullptr;
new_node = new_node->next, current = current->next )
{
new_node->next = new ListNode { current->value, nullptr };
}
}
}