0
void remove (const T &val) {
    node* placer  = head->next;
    node* prev_placer = head;
    while (placer) {
        if (placer->data == val) {
            prev_placer->next = placer->next;
            free(placer);           
        }
        prev_placer = placer;
        placer = placer->next;
    }
    if (head->data == val) {
        this->head = head->next;
        head->prev = nullptr;
    }
    if (tail->data = val) {
        this->tail = tail->prev;
        tail->next = nullptr;
    }
}

An eexample: If my list was [h, e, l, l, o], and I wanted to remove('l'), then my list == [h, e, o]. However, whenever I try to print my work to test it, I keep getting segmentation faults.

codeling
  • 11,056
  • 4
  • 42
  • 71

1 Answers1

0

In your main loop, you are using placer after it's been deleted. Also, you should not be using free in C++, use delete instead (provided that you have used new to create a node; or do you use malloc there? Then are you maybe trying to write c code? But then I think the nullptr keyword is C++-specific...).

A corrected version of the loop might look something like this:

while (placer) {
    if (placer->data == val) {
        prev_placer->next = placer->next;
        delete placer;
        placer = prev_placer->next;
    }
    else
    {
        prev_placer = placer;
        placer = placer->next;
    }
}

A general recommendation to avoid the unhealthy mixture between C and C++ (they are different) would be to pick up a good book on the subject.

codeling
  • 11,056
  • 4
  • 42
  • 71