Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链表复制构造函数_C++_Linked List_Copy Constructor - Fatal编程技术网

C++ 链表复制构造函数

C++ 链表复制构造函数,c++,linked-list,copy-constructor,C++,Linked List,Copy Constructor,我最初不得不使用STL创建自己的链表。现在,我要实现一个复制构造函数方法,我真的很难理解它。过几天我会对此进行测试,所以我真的很想弄清楚。(考试是闭卷的,所以需要真正的考试)。 该列表包含一个EmployeeNode指针*头。EmployeeNode包含一个Employee和一个指向下一个EmployeeNode的指针。Employee类包含名称和薪水 当试图复制第三个节点时,该方法似乎陷入了for循环。我想这是因为我覆盖了新节点,但我不知道如何解决这个问题 ListOfEmployee::Li

我最初不得不使用STL创建自己的链表。现在,我要实现一个复制构造函数方法,我真的很难理解它。过几天我会对此进行测试,所以我真的很想弄清楚。(考试是闭卷的,所以需要真正的考试)。 该列表包含一个EmployeeNode指针*头。EmployeeNode包含一个Employee和一个指向下一个EmployeeNode的指针。Employee类包含名称和薪水

当试图复制第三个节点时,该方法似乎陷入了for循环。我想这是因为我覆盖了新节点,但我不知道如何解决这个问题

ListOfEmployee::ListOfEmployee(const ListOfEmployee &obj)
{
    head = NULL;

    if(obj.head != NULL)
    {
        EmployeeNode *newNode  = new EmployeeNode("", 0);
        EmployeeNode *tempPtr;
        EmployeeNode *newPtr;

        //using the temp pointer to scroll through the list until it reaches the end
        for(tempPtr = obj.head; tempPtr->next !=NULL; tempPtr = tempPtr->next)
        {
            if(head == NULL)
            {
                cout<<"Attempts to initialize the head"<<endl;

                head = newNode; //assinging the new node to the head
                newNode->emp.name = tempPtr->emp.name;
                newNode->emp.salary = tempPtr->emp.salary;

                cout<<"Initializes the head"<<endl;
            }
            else
            {
                cout<<"Attempts to add a new node"<<endl;

                //using the temp pointer to scroll through the list until it reaches the end
                for(newPtr = head; newPtr->next !=NULL; newPtr = newPtr->next)
                {
                    cout<<"Looping through the list"<<endl;
                }

                //assiging the last place to the new node
                newPtr->next = newNode;
                newNode->emp.name = tempPtr->emp.name;
                newNode->emp.salary = tempPtr->emp.salary;

                cout<<"Adds a new node"<<endl;
            }
        }
    }
}
ListOfEmployee::ListOfEmployee(const ListOfEmployee&obj)
{
head=NULL;
if(obj.head!=NULL)
{
EmployeeNode*newNode=newemployeenode(“,0”);
EmployeeNode*tempPtr;
员工模式*newPtr;
//使用临时指针在列表中滚动,直到列表结束
for(temptr=obj.head;temptr->next!=NULL;temptr=temptr->next)
{
if(head==NULL)
{
工资;

cout在您的代码中,您将在
newPtr->next=newNode;中添加newNode;
您基本上是在使用以前分配的节点。您应该使用new创建一个新节点。类似于:

newPtr->next = new EmployeeNode("", 0);
newNode = newPtr->next;
newNode->emp.name = tempPtr->emp.name;
newNode->emp.salary = tempPtr->emp.salary;

另外,您还应该在代码中设置
newNode->next=NULL;

谢谢。现在看起来很明显!另外,newNode->next在EmployeeNode构造函数中被初始化为NULL。