//remove nodes containing odd values from a sequence of nodes
//Pre: cur is a reference to a Node
//Post: the sequence of nodes starting with cur has had all nodes containing
// odd values removed
//Return: a reference to the modified sequence of nodes.
Node *removeOdds(Node *cur){
Node *temp;
if (cur == NULL){
return NULL;
}
elseif(cur->data%2 != 0){
cout << "is odd" << endl;
temp = cur->next;
delete cur;
cur = temp;
temp->next = removeOdds(cur->next);
//return cur;
//return temp;
}
else{
cout << "is even" << endl;
cur->next = removeOdds(cur->next);
return cur;
}
}
This code always crashes... i have some commented out lines that i have attempted but to no avail. ANY help would be appreciated.
Kind of! In this case I want this function to return a reference to the head of the single linked list
1 2 3 4
struct Node {
int data;
Node* next;
};
so that i can reprint it to the console and show that i have deleted the nodes containing odd data. This is my testing code (the other function calls work as they are supposed to)
1 2 3 4 5 6 7 8 9
cout << "testing with a sequence of mixed even/odd numbers:" << endl;
int input3[] = {2,7,5,32,40,1};
head = arrayToNodeSequence(input3, 6);
cout << "full sequence of nodes:" << endl;
printNodes(head);
head = removeOdds(head);////////////////////
cout << "now with odds removed: (only the even values should remain)" << endl;
printNodes(head);
deallocateNodeSequence(head);
cur already points to what cur->next would've been on function entry, and it may have the value of nullptr, so dereferencing it would cause undefined behavior.
Line 17 should be return removeOdds(cur); or alternately replace cur with temp since they both point to the same thing.
How I would've written it:
1 2 3 4 5 6 7 8 9 10 11 12 13
Node* removeOdds(Node* first) {
if (!first) returnnullptr; // done
if (first->data % 2 == 0) // even
{
first->next = removeOdds(first->next);
return first;
}
auto next = first->next; // odd
delete first;
return removeOdds(next);
}