<legend id='JlxNZ'><style id='JlxNZ'><dir id='JlxNZ'><q id='JlxNZ'></q></dir></style></legend>
    1. <tfoot id='JlxNZ'></tfoot>
        <bdo id='JlxNZ'></bdo><ul id='JlxNZ'></ul>

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

        <i id='JlxNZ'><tr id='JlxNZ'><dt id='JlxNZ'><q id='JlxNZ'><span id='JlxNZ'><b id='JlxNZ'><form id='JlxNZ'><ins id='JlxNZ'></ins><ul id='JlxNZ'></ul><sub id='JlxNZ'></sub></form><legend id='JlxNZ'></legend><bdo id='JlxNZ'><pre id='JlxNZ'><center id='JlxNZ'></center></pre></bdo></b><th id='JlxNZ'></th></span></q></dt></tr></i><div id='JlxNZ'><tfoot id='JlxNZ'></tfoot><dl id='JlxNZ'><fieldset id='JlxNZ'></fieldset></dl></div>
      1. 在 C++ 中编写一个函数来复制链表

        时间:2023-10-18

        <tfoot id='5MrPP'></tfoot>
            <tbody id='5MrPP'></tbody>
          <legend id='5MrPP'><style id='5MrPP'><dir id='5MrPP'><q id='5MrPP'></q></dir></style></legend>
            <bdo id='5MrPP'></bdo><ul id='5MrPP'></ul>

            <small id='5MrPP'></small><noframes id='5MrPP'>

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

                1. 本文介绍了在 C++ 中编写一个函数来复制链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要实现一个辅助函数,名为 copyList,有一个参数,一个指向 ListNode 的指针.该函数需要返回一个指向原始链表副本第一个节点的指针.因此,换句话说,我需要在 C++ 中编写一个函数,它接受一个链表的头节点并复制整个链表,返回一个指向新头节点的指针.我需要帮助来实现这个功能,这就是我现在所拥有的.

                  I need to implement an auxilliary function, named copyList, having one parameter, a pointer to a ListNode. This function needs to return a pointer to the first node of a copy of original linked list. So, in other words, I need to code a function in C++ that takes a header node of a linked list and copies that entire linked list, returning a pointer to the new header node. I need help implementing this function and this is what I have right now.

                  Listnode *SortedList::copyList(Listnode *L) {
                  
                      Listnode *current = L;  //holds the current node
                  
                      Listnode *copy = new Listnode;
                      copy->next = NULL;
                  
                      //traverses the list
                      while (current != NULL) {
                         *(copy->student) = *(current->student);
                         *(copy->next) = *(current->next);
                  
                          copy = copy->next;
                          current = current->next;
                      }
                      return copy;
                  }
                  

                  此外,这是我正在使用的 Listnode 结构:

                  Also, this is the Listnode structure I am working with:

                  struct Listnode {    
                    Student *student;
                    Listnode *next;
                  };
                  

                  注意:我在使用此函数时遇到的另一个因素是返回指向局部变量的指针的想法.

                  Note: another factor I am running into with this function is the idea of returning a pointer to a local variable.

                  推荐答案

                  您需要问自己的第一个问题是复制语义是什么.特别是,您使用 Student* 作为节点内容.复制节点内容是什么意思?我们应该复制指针以便两个列表指向(共享)相同的学生实例,还是应该执行 深拷贝?

                  The first question you need to ask yourself is what the copy semantics are. In particular, you're using a Student* as node contents. What does copying node contents mean? Should we copy the pointer so that the two lists will point to (share) the same student instances, or should you perform a deep copy?

                  struct Listnode {    
                    Student *student; // a pointer?  shouldn't this be a `Student` object?
                    Listnode *next;
                  };
                  

                  您应该问自己的下一个问题是如何为第二个列表分配节点.目前,您只在副本中分配了 1 个节点.

                  The next question you should ask yourself is how you will allocate the nodes for the second list. Currently, you only allocate 1 node in the copy.

                  我认为你的代码应该看起来更像:

                  I think you code should look more like:

                  Listnode *SortedList::copyList(Listnode *L) {
                  
                      Listnode *current = L;
                  
                      // Assume the list contains at least 1 student.
                      Listnode *copy = new Listnode;
                      copy->student = new Student(*current->student);
                      copy->next = NULL;
                  
                      // Keep track of first element of the copy.
                      Listnode *const head = copy;
                  
                      // 1st element already copied.
                      current = current->next;
                  
                      while (current != NULL) {
                         // Allocate the next node and advance `copy` to the element being copied.
                         copy = copy->next = new Listnode;
                  
                         // Copy the node contents; don't share references to students.
                         copy->student = new Student(*current->student);
                  
                         // No next element (yet).
                         copy->next = NULL;
                  
                         // Advance 'current' to the next element
                         current = current->next;
                      }
                  
                      // Return pointer to first (not last) element.
                      return head;
                  }
                  

                  <小时>

                  如果您更喜欢在两个列表之间共享学生实例,可以使用


                  If you prefer sharing student instances between the two lists, you can use

                  copy->student = current->student;
                  

                  代替

                  copy->student = new Student(*current->student);
                  

                  这篇关于在 C++ 中编写一个函数来复制链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:随机插入/删除的综合向量与链表基准 下一篇:简单的 C++ 链表

                  相关文章

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

                3. <tfoot id='4zbet'></tfoot>

                    <bdo id='4zbet'></bdo><ul id='4zbet'></ul>

                  <small id='4zbet'></small><noframes id='4zbet'>

                    1. <legend id='4zbet'><style id='4zbet'><dir id='4zbet'><q id='4zbet'></q></dir></style></legend>