The function remove_odd
is to remove odd valued nodes (surprise!!) from the linked list head. The code is very long and cryptic but it seems to work.
Please help me reduce the lines of code and are there any cases that I might have missed.
void rem_odd(struct Node** head)
{
struct Node* curr=*head;
struct Node* l_even=NULL; //l_even stores the last even number node address
while(curr!=NULL&&curr->data%2!=0)
{
*head=curr->next;
if(curr->next==NULL)
return;
curr=curr->next;
}
l_even=curr;
while(curr!=NULL)
{
if(curr->next==NULL)
return;
while(curr!=NULL&&curr->next->data%2!=0)
{
if (curr->data%2!=0)
{
printf("if ");
if(curr->next==NULL)
{
l_even->next=NULL;
return;
}
curr=curr->next;
l_even->next=curr;
if(curr->next==NULL)
{
l_even->next=NULL;
return;
}
continue;
}
curr=curr->next;
if(curr->data %2!=0 &&curr->next==NULL)
{
l_even->next=NULL;
return;
}
}
if(curr->next==NULL)
{
l_even->next=NULL;
}
else
{
l_even->next=curr->next;
l_even=curr->next;
}
curr=curr->next;
}
}