问题描述
这是作业
我正在为我的 C++ 类实现一个链表类,而复制构造函数让我很困惑.
I'm working on implementing a linked list class for my C++ class, and the copy constructor has be very confusing for me.
链表由称为 Elems 的结构组成:
The linked list is comprised of structs called Elems:
struct Elem
{
int pri;
data info;
Elem * next;
};
Elem * head;
info 是一个单独的自定义类,存储在 Elem 中.
info is a separate, custom class that is stored in the Elem.
复制构造函数的签名是:
the signature for the copy constructor is:
linkedList::linkedList( const linkedList &v )
我遇到的问题主要是把我的逻辑写成代码.
The issue I am having is mostly taking my logic and actually writing it as code.
我的总体思路是:
- 将头部设置为 v.head (head = v.head)
- 将 Elem 的值设置为 v (pri = v.pri , info = v.info , next = v.next)
- 迭代,重复第 2 步.
这是总体思路吗?
任何帮助都会很棒.记住,这是家庭作业,所以请不要直接回答!
Any help would be great. Remember, this is homework, so no direct answers please!
感谢您的时间
====================================================================================================================================================================
====================================================================================================================================================================
感谢大家的时间!
我想我已经弄清楚了:
//Copy Constructor
LinkedList::LinkedList( const LinkedList &v )
{
Elem * p1 = 0;//current
Elem * p2 = 0;//next
if( v.head == 0 )
head = 0;
else
{
head = new Elem;
head -> pri = v.head -> pri;
head -> info = v.head -> info;
p1 = head;
p2 = v.head -> next;
}
while( p2 )
{
p1 -> next = new Elem;
p1 = p1 -> next;
p1 -> pri = p2 -> pri;
p1 -> info = p2 -> info;
p2 = p2 -> next;
}
p1 -> next = 0;
}
我很确定这行得通.我画了一些合乎逻辑的图片来提供帮助,我没有遇到任何问题.
I'm pretty sure that works. I drew some logical pictures to help, and I didn't run into any issues.
推荐答案
你必须小心第 1 步和第 2 步的一部分.第 1 步应该分配一个新节点并将其用作 head
.第 2 步中关于 next = v.next
的部分,除非您打算进行浅拷贝,否则是不正确的.
You have to be careful with Step 1 and part of Step 2. Step 1 should allocate a new node and use that as the head
. In Step 2, the part about next = v.next
, unless your intention is to make a shallow copy, is incorrect.
当你复制一个容器如链表时,你可能需要一个深复制,所以需要创建新节点并且只复制数据.新列表节点中的 next
和 prior
指针应该指向您为该列表专门创建的新节点,而不是来自列表的节点.原始清单.这些新节点将拥有原始列表中相应数据的副本,因此可以将新列表视为按值或深度复制.
When you copy a container such as a linked list, you probably want a deep copy, so new nodes need to be created and only the data copied over. The next
and prior
pointers in the nodes of the new list should refer to new nodes you create specifically for that list and not the nodes from the original list. These new nodes would have copies of the corresponding data from the original list, so that the new list can be considered a by value, or deep copy.
下面的图片描述了浅拷贝和深拷贝的区别:
Here is a picture depicting the differences between shallow and deep copying:
注意在图表的Deep Copy 部分,没有一个节点指向旧列表中的节点.有关浅拷贝和深拷贝之间区别的更多信息,请参阅关于对象复制的维基百科文章.
Notice how in the Deep Copy portion of the diagram, none of the nodes point to nodes in the old list. For more information about the difference between shallow and deep copies, see the Wikipedia article on object copying.
这篇关于为链表创建复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!