Hello I have home work in c++: write function whose delete those elements who next element is even.
For example i have this list: 4 2 2, the result of this function mus be: 2. other example: 4 1 2 2, result: 4 2.
I write such function, but with this examples my function make errorin other way my function works correctly. Where can be error in this function?:
structure:
Code:
struct elem
{
int n;
elem *next;
};
function:
Code:
void remake(elem *&first)
{
elem *curr=first;
elem *prev=NULL;
elem *pprev=NULL;
while(curr!=NULL)
{
pprev=prev;
prev=curr;
curr=curr->next;
if(curr!=NULL)
{
if(curr->n%2==0)
{
if(pprev==NULL)
{
delete prev;
first=curr;
prev=curr;
}
else
{
pprev->next=curr;
delete prev;
prev=pprev;
}
}
}
}
};