samedi 29 octobre 2016

Linked list deep copy constructor

I am implementing a linked list class' copy constructor which will make a deep copy. This is the code that i have:

List( const List & rhs ) {
        Node* rhsFront = rhs.header->next;
        Node* prev = header;
        while (rhsFront) {
            prev->next = new Node(rhsFront->data, nullptr);
            rhsFront = rhsFront->next;
            prev = prev->next;
        }
}

However, it crashes at this line:

prev->next = new Node(rhsFront->data, nullptr);

What did I do wrong?

Aucun commentaire:

Enregistrer un commentaire