为链表创建一个拷贝构造函数 [英] Creating a copy constructor for a linked list

查看:740
本文介绍了为链表创建一个拷贝构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是家庭作业



我正在为我的C ++类实现一个链表类,并且复制构造函数非常对我来说很困惑。



链接列表由Elems的结构组成:

  struct Elem 
{
int pri;
data info;
Elem * next;
};
Elem * head;

info是一个单独的自定义类,存储在Elem中。



复制构造函数的签名是:

  linkedList :: linkedList(const linkedList& v )

我遇到的问题大多是我的逻辑,实际上是写作代码。 >

我的一般想法是:


  1. 将head设为v.head .head)

  2. 将Elem的值设置为v's(pri = v.pri,info = v.info,next = v.next)

  3. 重复第2步。

这是一般建议吗?



任何帮助将是伟大的。 记住,这是家庭作业,所以无法直接回答。



感谢您的时间<

======================================= ==================================================== ==================================================== =======================



感谢您的时间大家!



我想我想出了:

  //复制构造函数
LinkedList :: LinkedList(const LinkedList& v)
{
Elem * p1 = 0; // current
Elem * p2 = 0; // next
$ b b if(v.head == 0)
head = 0;

else
{
head = new Elem;
head - > pri = v.head - > pri;
head - > info = v.head - >信息;

p1 = head;
p2 = v.head - >下一个;
}

while(p2)
{
p1 - > next = new Elem;
p1 = p1 - >下一个;
p1 - > pri = p2 - > pri;
p1 - > info = p2 - >信息;

p2 = p2 - >下一个;
}
p1 - > next = 0;
}

我画了一些逻辑图片来帮助,我没有遇到任何问题。

解决方案

1和步骤2的一部分。步骤1应该分配一个新节点并将其作为。在第2步,关于 next = v.next 的部分不正确,除非你的意图是做一个浅的副本。



当复制诸如链接列表的容器时,您可能需要一个深层副本,因此需要创建新的节点,并且只复制数据。新列表节点中的个之前的指针应该指向您创建的新节点 / em>,而不是原始列表中的节点。这些新节点将具有来自原始列表的相应数据的副本,使得新列表可以被视为按值或深度复制。



这里是图片描述浅层和深层复制之间的差异:





请注意,在图的深层复制部分中,没有一个节点指向旧列表中的节点。有关浅层和深层副本之间区别的详细信息,请参阅有关对象复制的维基百科文章。


This is homework

I'm working on implementing a linked list class for my C++ class, and the copy constructor has be very confusing for me.

The linked list is comprised of structs called Elems:

struct Elem 
    {
        int pri;
        data info;
        Elem * next;
    };
    Elem * head;

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.

My general idea is to:

  1. Set head to v.head (head = v.head)
  2. Set the Elem's values to v's (pri = v.pri , info = v.info , next = v.next)
  3. Iterate through, repeating step 2.

Is this the general idea?

Any help would be great. Remember, this is homework, so no direct answers please!

Thank you for your time

====================================================================================================================================================================

Thanks for your time everybody!

I think I have it figured out:

//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.

解决方案

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.

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:

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.

这篇关于为链表创建一个拷贝构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆