Linked list copy constructor C++

Tangleman

I am really confused here as to why this copy constructor is not working! I am creating an iter pointer that points to the same ListNode as head, but when I copy stuff from s to it, head and iter are not connected!

In other words when printing head, only the first character is in there, but if I were to iterate through iter, the rest of the list is in there. Why isn't iter and head pointing to the same objects?!

NOTE: This is a linked list being used to implement a class called MyString.

struct ListNode {
    char info;
    ListNode *next;
    ListNode () : info('0'), next(0) {}
    ListNode (char c) : info (c), next(0) {}
};

class MyString {
    private:
    ListNode *head;

    MyString::MyString(const MyString & s) {
        if (s.head == 0)
            head = 0;
        else {
            head = new ListNode (s.head -> info);
            ++NumAllocations;
            ListNode *iter = head;
            for (ListNode *ptr = s.head -> next; ptr != 0; ptr = ptr ->next) {
                iter = iter -> next;
                iter = new ListNode (ptr -> info);
                ++NumAllocations;
            }
        }
    }
}
cdbitesky

You don't appear to be attaching the list to the head anywhere.

Try this.

MyString::MyString( const MyString & s ) {
    if ( s.head == 0)
        head = 0;
    else {
        head = new ListNode (s.head -> info);
        ++ NumAllocations;
        ListNode *iter = head;
        for (ListNode *ptr = s.head -> next; ptr != 0; ptr = ptr ->next) {
            iter -> next = new ListNode (ptr -> info);
            iter = iter -> next;
            ++ NumAllocations;
        }
        printList(head);
    }
}

Notice the attachment of iter->next. You were just creating a new node and doing nothing with it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy Constructor in Linked List C++

From Dev

Linked list copy constructor

From Dev

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

From Dev

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

From Dev

proper implementation of copy constructor for a linked list

From Dev

Doubly linked list copy constructor and program crash

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

Copy constructor traverse through linked list. implementation

From Dev

C++ constructor that deep copies a linked list

From Dev

C++ Linked List - Constructor and Operator Overloading

From Dev

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

From Dev

Linked List Copy/Move semantic C++

From Dev

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

From Dev

constructor for a linked list

From Dev

Deep copy of linked list

From Dev

C struct copy in C++ constructor initialization list

From Dev

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

From Dev

Linked List Constructor with initial value

From Dev

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

From Dev

Copy Constructor in C++

From Dev

Copy Constructor in C++

From Dev

a linked list of linked lists in C

From Dev

Creating a class that has a linked list in the constructor

From Dev

constructor and copy constructor behaviour in c++

From Dev

Why is this C++ constructor called twice at the same memory location in this implementation of a linked list?

From Dev

Linked List in C++

From Dev

Implementing a Linked List C

Related Related

  1. 1

    Copy Constructor in Linked List C++

  2. 2

    Linked list copy constructor

  3. 3

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

  4. 4

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

  5. 5

    proper implementation of copy constructor for a linked list

  6. 6

    Doubly linked list copy constructor and program crash

  7. 7

    Linked List Copy Constructor | Undefined Behavior

  8. 8

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

  9. 9

    Doubly Linked List Template Copy Constructor Assignment Operator

  10. 10

    Copy constructor traverse through linked list. implementation

  11. 11

    C++ constructor that deep copies a linked list

  12. 12

    C++ Linked List - Constructor and Operator Overloading

  13. 13

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

  14. 14

    Linked List Copy/Move semantic C++

  15. 15

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

  16. 16

    constructor for a linked list

  17. 17

    Deep copy of linked list

  18. 18

    C struct copy in C++ constructor initialization list

  19. 19

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

  20. 20

    Linked List Constructor with initial value

  21. 21

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

  22. 22

    Copy Constructor in C++

  23. 23

    Copy Constructor in C++

  24. 24

    a linked list of linked lists in C

  25. 25

    Creating a class that has a linked list in the constructor

  26. 26

    constructor and copy constructor behaviour in c++

  27. 27

    Why is this C++ constructor called twice at the same memory location in this implementation of a linked list?

  28. 28

    Linked List in C++

  29. 29

    Implementing a Linked List C

HotTag

Archive