gpt4 book ai didi

c++ - 在 C++ 中为 LinkedList 类创建复制构造函数时寻求帮助

转载 作者:太空狗 更新时间:2023-10-29 20:42:09 24 4
gpt4 key购买 nike

首先,我意识到 StackOverflow 上有多个关于此的主题。我在理解其中一些人的回答时遇到了一些麻烦。我创建这个是希望有人能帮助我理解这个过程。

如果这个问题看起来比较新手,我深表歉意,但我会尽力理解它。

我正在学习数据结构,并被要求根据提供的 header 创建一个 LinkedList 实现文件。

这是家庭作业,所以请不要输入“这里是确切的代码”类型的答案。欢迎提供伪代码、步骤和提示。

这是我目前正在处理的 header 部分:

typedef std::string ElementType;

class LinkedList
{
private:
class Node
{
public:
ElementType data;
Node * next;

Node( )
: data( ElementType( ) ), next( NULL )
{ }

Node( ElementType initData )
: data( initData ), next( NULL )
{ }
}; // end of Node class

typedef Node * NodePointer;

public:
LinkedList( );
/* Construct a List object

Precondition: none.
Postcondition: An empty List object has been constructed.
*/

LinkedList( const LinkedList &source );
/* Construct a copy of a List object.

Precondition: None.
Postcondition: A copy of source has been constructed.
*/

~LinkedList( );
/* Destroys a List object.

Precondition: None.
Postcondition: Any memory allocated to the List object has been freed.
*/

const LinkedList & operator=( const LinkedList &rightSide );
/* Assign a copy of a List object to the current object.


private:
NodePointer first;
int mySize;
};

到目前为止,我已经创建了析构函数,你能检查并确保它是正确的吗?

//Destructor
LinkedList::~LinkedList()
{
NodePointer ptr = first;

while(ptr != 0 ) {
NodePointer next = ptr->next;
delete ptr;
ptr = next;
}
first = 0;
}

现在这是我迷路的部分...创建复制构造函数的基本步骤是什么?我已经完成了简单的默认构造函数,但我对我应该在复制构造函数上做什么感到有点困惑。

我对重载 = 运算符也有点困惑,我认为它与复制构造函数非常相似。

编辑

我第一次尝试复制构造函数:

LinkedList::LinkedList(const LinkedList & source)
{
//create a ptr to our copy
Node * copy_node = source.first;
//where we will be inserting our copy
Node * insert_node = first;

while(copy_node != nullptr)
{
//insert our new copy node
insert_node = new Node(copy_node->data);
//move to our next node
copy_node = copy_node->next;

if(copy_node != nullptr) {
insert_node = insert_node->next;
} else {
insert_node = first;
}

//update size
mySize++;

}
}

总感觉少了点什么

最佳答案

What are the basic steps of creating the copy constructor? I've finished the default constructor which was simple, but I'm a bit confused with what I should be doing on the copy constructor.

很明显,您需要复制源代码。如果源是一个包含 N 个节点的列表,那么您需要构建另一个包含 N 个节点的列表,每个节点都是源中相应节点的拷贝。

因此遍历源节点并创建它们的拷贝。

I'm also slightly confused about overloading the = operator, I assume it will be very similar to the copy constructor.

是的,除非您需要先处理当前节点。然而,实现赋值的一种简单而安全的方法是 copy-and-swap ,因此定义一个正确的交换成员:

void swap(LinkedList& other)
{
std::swap(first, other.first);
std::swap(size, other.size);
}

然后用它来实现赋值:

LinkedList& operator=(const LinkedList& source)
{
LinkedList(source).swap(*this);
return *this;
}

这会创建一个临时文件,它是 source 的拷贝,然后将其与 *this 交换,因此 *this 的旧内容得到被临时对象销毁,*this 以复制的数据结束。

注意返回类型应该是非常量,从赋值运算符返回常量引用不是惯用的。

关于c++ - 在 C++ 中为 LinkedList 类创建复制构造函数时寻求帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19758092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com