I am having trouble trying to delete all of the nodes in a linked list that contain the same integer. I am getting an EXC_BAD_ACCESS error on the commented line below when using the test node: [1, 2, 1, 3, 1, 5, 1, 2] and deleted all 2s to get out [1, 1, 3, 1, 5, 1]. Here is my code:
int List::RemoveAll(int v) {
if (IsEmpty())
return 0;
else if (HasOnlyOne()) {
if (h->data == v) {
delete h;
h = t = NULL;
return 1;
}
}
int numRemoved = 0;
Node *curNode = h->next, *prevNode = h;
while (curNode != NULL) {
if (prevNode->data == v) {
delete prevNode;
numRemoved++;
}
prevNode = curNode;
curNode = curNode->next; // EXC_BAD_ACCESS error here.
}
return numRemoved;
}
Thank you!