Search code examples
cstructlinked-listdoubly-linked-listfunction-definition

Deleting first occurrence in a doubly linked list


I have an assignment to do.. the task is to delete the first odd number only I wrote the code but honestly I have zero confidence about it.. if you guys can help me I would be pleased. The function deletes the first node that has an odd value, so the function searches for the first node that has an odd value and deletes it.

typedef struct Node {
int data;
struct Node *next;
struct Node *previous;
 }Node;

int DeleteFirstODD(Node **front) {
int oddnum;
Node *temp = *front;


if (*front == NULL) //Checking if the list is empty
    return;


while (temp != NULL && temp->data % 2 == 0)
    temp = temp->next;

if (temp == NULL)
    return -1;

else if (temp == *front) { //if odd num founded @ the begining of the doubly 
  linked list!
    oddnum = (*front)->data;
    *front = (*front)->next;
    (*front)->previous = NULL;
    free(temp);
}
else if (temp->next == NULL) { //if odd num founded @ the end
    oddnum = temp->data;
    temp->previous->next = NULL;
    free(temp);

}
else { // if the odd somewhere in the middle
    temp->previous->next = NULL;
    temp->next->previous = NULL;
    free(temp);
}


  return oddnum;
   }

Solution

  • For starters this typedef declaration

    typedef struct Node {
    int data;
    Node *next;
    Node *previous;
     }Node;
    

    is incorrect. You have to write

    typedef struct Node {
        int data;
        struct Node *next;
        struct Node *previous;
     } Node;
    

    If a function has a return type that is not void then you may not use the return statement without an expression like in your function

    return;
    

    The function could return integer value 1 in the case when a node with an odd value was deleted or 0 otherwise.

    The code within the function is incorrect. For example this loop

    while (ptr != NULL && ptr % 2 != 0) {
    
        oddnum = ptr->data; 
        ptr = ptr->next;
    }
    

    does not make a sense.

    The function can be defined the following way

    int DeleteFirstODD( Node **front ) 
    {
        while ( *front && ( *front )->data % 2 == 0 )
        {
            front = &( *front )->next;
        }
    
        int success = *front != NULL;
    
        if ( success )
        {
            Node *current = *front;
    
            if ( current->next )
            {
                current->next->previous = current->previous;
            }
    
            *front = current->next;
    
            free( current );
        }
    
        return success;
    }
    

    Here is a demonstrative program.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node 
    {
        int data;
        struct Node *next;
        struct Node *previous;
    } Node;
    
    int push_front( Node **head, int data )
    {
        Node *new_node = malloc( sizeof( Node ) );
        int success = new_node != NULL;
        
        if ( success )
        {
            new_node->data     = data;
            new_node->next     = *head;
            if ( *head ) ( *head )->previous = new_node;
            new_node->previous = NULL; 
        
            *head = new_node;
        }
        
        return success;
    }
    
    void display( const Node *head )
    {
        for ( ; head; head= head->next )
        {
            printf( "%d -> ", head->data );
        }
        puts( "null" );
    }
    
    int DeleteFirstODD( Node **front ) 
    {
        while ( *front && ( *front )->data % 2 == 0 )
        {
            front = &( *front )->next;
        }
    
        int success = *front != NULL;
    
        if ( success )
        {
            Node *current = *front;
    
            if ( current->next )
            {
                current->next->previous = current->previous;
            }
    
            *front = current->next;
    
            free( current );
        }
    
        return success;
    }
    
    int main(void) 
    {
        Node *head = NULL;
        const int N = 10;
        
        for ( int i = N; i != 0; i-- )
        {
            push_front( &head, i );
        }
        
        display( head );
        
        while ( DeleteFirstODD( &head ) )
        {
            display( head );
        }
        
        return 0;
    }
    

    The program output is

    1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
    2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
    2 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
    2 -> 4 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
    2 -> 4 -> 6 -> 8 -> 9 -> 10 -> null
    2 -> 4 -> 6 -> 8 -> 10 -> null