Linked list copy constructor

GermaineJason

I originally had to create my own linked list with using the STL. Now, I am to implement a copy constructor method and I am having real difficulty understanding it. Have a test on this in a few days so I would really like to get it clear. (Test is closed book so need to really). The List contains a EmployeeNode pointer *head. The EmployeeNode contains an Employee and a pointer to the next EmployeeNode. The Employee class contains a name and salary.

The method seems to get caught in the for loop when trying to copy the 3rd node over. I think this is because I overwrite the newNode but I do not know how to solve this.

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;
            }
        }
    }
}
benipalj

In your code where you are adding newNode in newPtr->next = newNode; you are basically using previously allocated node. You should create a new node using new. Something like:

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

Also you should set newNode->next = NULL; in your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linked list copy constructor C++

From Dev

proper implementation of copy constructor for a linked list

From Dev

Doubly linked list copy constructor and program crash

From Dev

Copy Constructor in Linked List C++

From Dev

Linked List Copy Constructor | Undefined Behavior

From Dev

Using a copy() method for a linked list copy constructor and assignment operator

From Dev

Doubly Linked List Template Copy Constructor Assignment Operator

From Dev

implementing stack with a linked list in C++ , copy constructor

From Dev

Copy constructor traverse through linked list. implementation

From Dev

C++ Copy Constructor of Linked List's values, not addresses

From Dev

constructor for a linked list

From Dev

Deep copy of linked list

From Dev

Linked List Constructor with initial value

From Dev

C++ initialize list assignment in copy constructor and crashing in copy constructor

From Dev

Creating a class that has a linked list in the constructor

From Dev

C++ constructor that deep copies a linked list

From Dev

C++ Linked List - Constructor and Operator Overloading

From Dev

Linked List data-copy rotation

From Dev

Linked List Copy/Move semantic C++

From Dev

Constructor copy, list, pointers and template in C++

From Dev

Make a transformed copy of a List using ArrayList constructor

From Dev

copy constructor for a stack class that uses list class

From Dev

Doubly List Copy Constructor: How different is it from a Singly List Copy Constructor?

From Dev

Copy Constructor

From Dev

how to copy txt file (containing structs) to linked list c

From Dev

list initialization of aggregates: when can it invoke copy constructor?

From Dev

C++ Copy constructor gets called instead of initializer_list<>

From Dev

C struct copy in C++ constructor initialization list

From Dev

Move constructor bypasses copy constructor

Related Related

  1. 1

    Linked list copy constructor C++

  2. 2

    proper implementation of copy constructor for a linked list

  3. 3

    Doubly linked list copy constructor and program crash

  4. 4

    Copy Constructor in Linked List C++

  5. 5

    Linked List Copy Constructor | Undefined Behavior

  6. 6

    Using a copy() method for a linked list copy constructor and assignment operator

  7. 7

    Doubly Linked List Template Copy Constructor Assignment Operator

  8. 8

    implementing stack with a linked list in C++ , copy constructor

  9. 9

    Copy constructor traverse through linked list. implementation

  10. 10

    C++ Copy Constructor of Linked List's values, not addresses

  11. 11

    constructor for a linked list

  12. 12

    Deep copy of linked list

  13. 13

    Linked List Constructor with initial value

  14. 14

    C++ initialize list assignment in copy constructor and crashing in copy constructor

  15. 15

    Creating a class that has a linked list in the constructor

  16. 16

    C++ constructor that deep copies a linked list

  17. 17

    C++ Linked List - Constructor and Operator Overloading

  18. 18

    Linked List data-copy rotation

  19. 19

    Linked List Copy/Move semantic C++

  20. 20

    Constructor copy, list, pointers and template in C++

  21. 21

    Make a transformed copy of a List using ArrayList constructor

  22. 22

    copy constructor for a stack class that uses list class

  23. 23

    Doubly List Copy Constructor: How different is it from a Singly List Copy Constructor?

  24. 24

    Copy Constructor

  25. 25

    how to copy txt file (containing structs) to linked list c

  26. 26

    list initialization of aggregates: when can it invoke copy constructor?

  27. 27

    C++ Copy constructor gets called instead of initializer_list<>

  28. 28

    C struct copy in C++ constructor initialization list

  29. 29

    Move constructor bypasses copy constructor

HotTag

Archive