<bdo id='PyLPZ'></bdo><ul id='PyLPZ'></ul>
<legend id='PyLPZ'><style id='PyLPZ'><dir id='PyLPZ'><q id='PyLPZ'></q></dir></style></legend>
  • <i id='PyLPZ'><tr id='PyLPZ'><dt id='PyLPZ'><q id='PyLPZ'><span id='PyLPZ'><b id='PyLPZ'><form id='PyLPZ'><ins id='PyLPZ'></ins><ul id='PyLPZ'></ul><sub id='PyLPZ'></sub></form><legend id='PyLPZ'></legend><bdo id='PyLPZ'><pre id='PyLPZ'><center id='PyLPZ'></center></pre></bdo></b><th id='PyLPZ'></th></span></q></dt></tr></i><div id='PyLPZ'><tfoot id='PyLPZ'></tfoot><dl id='PyLPZ'><fieldset id='PyLPZ'></fieldset></dl></div>
    <tfoot id='PyLPZ'></tfoot>

    <small id='PyLPZ'></small><noframes id='PyLPZ'>

      1. 为链表创建复制构造函数

        Creating a copy constructor for a linked list(为链表创建复制构造函数)

          <tfoot id='CI1Jj'></tfoot>

          <small id='CI1Jj'></small><noframes id='CI1Jj'>

          <i id='CI1Jj'><tr id='CI1Jj'><dt id='CI1Jj'><q id='CI1Jj'><span id='CI1Jj'><b id='CI1Jj'><form id='CI1Jj'><ins id='CI1Jj'></ins><ul id='CI1Jj'></ul><sub id='CI1Jj'></sub></form><legend id='CI1Jj'></legend><bdo id='CI1Jj'><pre id='CI1Jj'><center id='CI1Jj'></center></pre></bdo></b><th id='CI1Jj'></th></span></q></dt></tr></i><div id='CI1Jj'><tfoot id='CI1Jj'></tfoot><dl id='CI1Jj'><fieldset id='CI1Jj'></fieldset></dl></div>

            <legend id='CI1Jj'><style id='CI1Jj'><dir id='CI1Jj'><q id='CI1Jj'></q></dir></style></legend>
              <bdo id='CI1Jj'></bdo><ul id='CI1Jj'></ul>

                    <tbody id='CI1Jj'></tbody>
                  本文介绍了为链表创建复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是作业

                  我正在为我的 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.

                  我的总体思路是:

                  1. 将头部设置为 v.head (head = v.head)
                  2. 将 Elem 的值设置为 v (pri = v.pri , info = v.info , next = v.next)
                  3. 迭代,重复第 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.

                  当你复制一个容器如链表时,你可能需要一个深复制,所以需要创建新节点并且只复制数据.新列表节点中的 nextprior 指针应该指向您为该列表专门创建的新节点,而不是来自列表的节点.原始清单.这些新节点将拥有原始列表中相应数据的副本,因此可以将新列表视为按值或深度复制.

                  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.

                  这篇关于为链表创建复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

                1. <tfoot id='DLVfG'></tfoot>
                        <bdo id='DLVfG'></bdo><ul id='DLVfG'></ul>

                      • <i id='DLVfG'><tr id='DLVfG'><dt id='DLVfG'><q id='DLVfG'><span id='DLVfG'><b id='DLVfG'><form id='DLVfG'><ins id='DLVfG'></ins><ul id='DLVfG'></ul><sub id='DLVfG'></sub></form><legend id='DLVfG'></legend><bdo id='DLVfG'><pre id='DLVfG'><center id='DLVfG'></center></pre></bdo></b><th id='DLVfG'></th></span></q></dt></tr></i><div id='DLVfG'><tfoot id='DLVfG'></tfoot><dl id='DLVfG'><fieldset id='DLVfG'></fieldset></dl></div>
                      • <legend id='DLVfG'><style id='DLVfG'><dir id='DLVfG'><q id='DLVfG'></q></dir></style></legend>
                          <tbody id='DLVfG'></tbody>
                        1. <small id='DLVfG'></small><noframes id='DLVfG'>