C++ 复制构造函数不适用于链表?

C++ 复制构造函数不适用于链表?,c++,c++11,linked-list,copy-constructor,C++,C++11,Linked List,Copy Constructor,下面是具有节点结构、链表副本构造函数和我的主文件的类。它正在打印第一个列表中的数字,并且只将第一个数字(15)复制到第二个列表中。它调用两个列表的析构函数,程序将正确关闭。无论我怎么努力,我都无法理解这个复制构造函数,它真的开始困扰我了 #ifndef LINKEDLIST_H #define LINKEDLIST_H #include <iostream> using namespace std; // Declare a class for the list class Lin

下面是具有节点结构、链表副本构造函数和我的主文件的类。它正在打印第一个列表中的数字,并且只将第一个数字(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;
}
\ifndef LINKEDLIST\u H
#定义链接列表
#包括
使用名称空间std;
//为列表声明一个类
类链接列表
{
私人:
//声明列表的结构
结构列表节点
{
//节点中的整数值和指向下一个节点的指针
int值=NULL;
struct ListNode*next=nullptr;
};
//列表头指针
ListNode*head=nullptr;
公众:
//建造师
LinkedList(){head=nullptr;};
//复制构造函数
LinkedList(const LinkedList&);
//析构函数
虚拟~LinkedList();
//链表操作
节点无效(int);
void insertNode(int);
无效删除节点(int);
void printList()常量;
无效反向列表()常量;
int搜索列表(const int)const;
};
#endif//LINKEDLIST\u H
LinkedList::LinkedList(常量LinkedList&listObj)
{
ListNode*newNode=nullptr;//创建新节点
ListNode*copyPtr=nullptr;//指向原始对象
copyPtr=listObj.head;
newNode=新的ListNode;
此->头部=新节点;
head->value=copyPtr->value;
copyPtr=copyPtr->next;
而(!copyPtr)
{
新建节点->下一步=新建列表节点;
newNode=newNode->next;
newNode->value=copyPtr->value;
copyPtr=copyPtr->next;
}
}
#包括“LinkedList.h”
#包括
使用名称空间std;
int main()
{
LinkedList列表1;
列表1.追加节点(15);
列表1.追加节点(20);
列表1.追加节点(25);
list1.printList();
LinkedList list2=list1;
list2.printList();
返回0;
}

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

while(!copyPtr)
应该是:

while (copyPtr) 

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

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

while(!copyPtr)
应该是:

while (copyPtr) 

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

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

  • 您不会检查
    listObj
    是否为
    nullptr
  • 在while循环结束时,您不会将
    newNode->next
    设置为
    nullptr

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

  • 您不会检查
    listObj
    是否为
    nullptr
  • 在while循环结束时,您不会将
    newNode->next
    设置为
    nullptr

  • 谢谢你们的帮助!更改为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;
            }
        }
    }
    

    谢谢你们的帮助!更改为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;
            }
        }
    }
    

    为什么要在while循环结束时执行
    newNode->next=nullptr
    ?为什么要在while循环结束时执行
    newNode->next=nullptr