【问题标题】:Copy Constructor is not working for linked list?复制构造函数不适用于链表?
【发布时间】:2015-05-13 02:16:12
【问题描述】:

下面是带有节点结构的类,链表拷贝构造函数,还有我的主文件。它正在打印第一个列表中的数字,并且仅将第一个数字 (15) 复制到第二个列表中。它调用两个列表的析构函数并且程序正确关闭。无论我多么努力,我都无法弄清楚这个复制构造函数,它真的开始困扰我了。

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;

// Declare a class for the list
class LinkedList
{
private:
    // Declaring a structure for the list
    struct ListNode
    {
        // Integer value in the node and pointer to the next node
        int value = NULL;
        struct ListNode* next = nullptr;
    };

    // List head pointer
    ListNode* head = nullptr;
public:
    // Constructor
    LinkedList() { head = nullptr; };

    // Copy constructor
    LinkedList(const LinkedList &);

    // Destructor
    virtual ~LinkedList();

    // Linked list operations
    void appendNode(int);
    void insertNode(int);
    void deleteNode(int);
    void printList() const;
    void reverseList() const;
    int searchList(const int) const;
};

#endif // LINKEDLIST_H

LinkedList::LinkedList(const LinkedList& listObj)
{
    ListNode* newNode = nullptr; // Create new node
    ListNode* copyPtr = nullptr; // Point to original object

    copyPtr = listObj.head;

    newNode = new ListNode;
    this->head = newNode;
    head->value = copyPtr->value;
    copyPtr = copyPtr->next;

    while(!copyPtr)
    {
        newNode->next = new ListNode;
        newNode = newNode->next;
        newNode->value = copyPtr->value;
        copyPtr = copyPtr->next;
    }
}

#include "LinkedList.h"
#include <iostream>
using namespace std;

int main()
{
    LinkedList list1;
    list1.appendNode(15);
    list1.appendNode(20);
    list1.appendNode(25);

    list1.printList();

    LinkedList list2 = list1;

    list2.printList();

    return 0;
}

【问题讨论】:

    标签: c++ c++11 linked-list copy-constructor


    【解决方案1】:

    您的循环条件不正确。您想在 下一个节点时循环。你正在做相反的事情:

    while(!copyPtr)
    

    应该是:

    while (copyPtr) 
    

    还要注意,如果您从中复制的列表为空,您的复制构造函数将不正确。在进行任何检查之前,您取消引用 listObj.head

    【讨论】:

      【解决方案2】:

      我发现你的代码有两个问题:

      1. 你不检查listObj是不是nullptr
      2. 在 while 循环结束时,不要将 newNode-&gt;next 设置为 nullptr

      【讨论】:

      • 为什么我要在 while 循环结束时使用newNode-&gt;next = nullptr
      【解决方案3】:

      谢谢大家的帮助!更改为 while(copyPtr) 使其工作。这么愚蠢的错误。我真的很感激。

      LinkedList::LinkedList(const LinkedList& listObj)
      {
          ListNode* newNode = nullptr; // Create new node
          ListNode* copyPtr = nullptr; // Point to original object
      
          if(!listObj.head)
              return;
          else
          {
              copyPtr = listObj.head;
      
              newNode = new ListNode;
              this->head = newNode;
              head->value = copyPtr->value;
              copyPtr = copyPtr->next;
      
              while(copyPtr)
              {
                  newNode->next = new ListNode;
                  newNode = newNode->next;
                  newNode->value = copyPtr->value;
                  copyPtr = copyPtr->next;
                  newNode->next = nullptr;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多