samedi 29 octobre 2016

Removing all nodes with certain value inside doubly linked list

I'm trying to implement a doubly linked list method to remove all nodes with certain value.

This is the code that I have:

void remove(const Object & x) {
        Node* current = head;
        while (current->next != nullptr) {
            if (current == head && current->data == x) {
                Node* victim = current;
                victim->next->prev = nullptr;
                current = current->next;
                delete victim;
            } else if (current->data == x) {
                Node* victim = current;
                Node* prevNode = current->prev;
                Node* nextNode = current->next;
                prevNode->next = nextNode;
                nextNode->prev = prevNode;
                current = current->next;
                delete victim;
            } else {
                current = current->next;
            }
        }
    }

I think that head doesn't point to anything now? And I'm not sure where to go from here

Aucun commentaire:

Enregistrer un commentaire