4
\$\begingroup\$

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;
    }
}
\$\endgroup\$
1
\$\begingroup\$

It seems you can simplify this by quite a lot. I added explanation of the algorithm inside the code, that way it might be easier to follow:

void rem_odd(struct Node** head)
{
    struct Node* curr = *head;
    struct Node* l_even;

    // iterate until the first even element, or the end
    while (curr != NULL && curr->data % 2 != 0)
    {
        curr = curr->next;
    }

    // the new head is the first even element, or the end
    *head = curr;

    // if no more elements, we're done here
    if (curr == NULL) {
        return;
    }

    // the last even element is the current element, naturally
    l_even = curr;

    // we know the current element is even, and not null, so moving on
    curr = curr->next;

    // iterate until the end
    while (curr != NULL)
    {
        // is the current element even?
        if (curr->data % 2 == 0) {

            // -> yes, it's even, so the last even element should point to it
            l_even->next = curr;

            // ... and this is the new last-even-element now
            l_even = curr;
        }
        curr = curr->next;
    }

    // last even might still point to an odd valued element, so NULL it out
    l_even->next = NULL;
}

Notice that I added spaces around the operators. For example this is extremely hard to read:

while(curr!=NULL&&curr->data%2!=0)
\$\endgroup\$
1
\$\begingroup\$

The code is very simple and does not uses an last even valued node concept as used above.

It's written in Java below:

public Node remove(Node head)
    {
        Node buf=head;
        Node prev=null;

        if(buf==null)
        {
            return buf;
        }

        if(buf.next==null)
        {
            if(buf.data%2!=0)
            {
                return null;

            }
        }


        while(buf!=null)
        {
            if(buf.data%2!=0)
            { 
                if(prev==null)
                {
                    head=head.next;
                    buf=head;
                }
                else
                {
                    prev.next=buf.next;
                    buf=buf.next;
                }

            }
            else {
            prev=buf;
            buf=buf.next;
            }
        }
        return head;
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.