1

I have an assignment to do.. the task is to delete only the first occurrence of an odd value in a doubly linked list, and I have to take in consideration all the special cases such as: if odd @ beginning, middle or last. so far, my code works fine i checked it out, its work with deleting the front only (if its odd), but in the other cases such as deleting the last/ middle.. I couldn't manage to get an output for them. basically the running command shows me nothing :(

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 = temp->next;
    temp->next->previous = temp->previous;
    oddnum = temp->data;
    free(temp);
}


return oddnum;
  }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    What did you do when you found out the code does not work for those cases? Did you then go ahead and debug it? Run your program in a debugger and step thru it line by line examining the state as it runs. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) and [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – kaylum Nov 23 '20 at 19:40
  • for the first case (when the first node = 1) it works perfectly fine.. but when i test the other two cases the program prints the list before deleting, where i wanted to have before and after list so i can see the difference among them. –  Nov 23 '20 at 19:50
  • 1
    I understand. But testing is not the same as debugging. Just printing out the list before and after is a good first step but there is much more debugging that can be done. Debugging means tracing the problem down to every line of code and every change in variable value to understand where the problem lies. – kaylum Nov 23 '20 at 19:52

1 Answers1

0

my code works fine i checked it out, its work with deleting the front only

You are wrong. This code snippet

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);
}

can invoke undefined behavior when the list contains only one node because after this statement

    *front = (*front)->next;

the pointer front will be equal to NULL and you may not use this null pointer in the following statement

    (*front)->previous = NULL;

The function can be defined the following way. I suppose that if such a node with an odd value is found when the stored value is returned. Otherwise -1 is returned.

int DeleteFirstODD( Node **front ) 
{
    int oddnum = -1;

    Node *temp = *front;

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

    if ( temp != NULL )
    {
        oddnum = temp->data;
        
        if ( temp == *front ) 
        { 
            if ( temp->next != NULL )
            {
                temp->next->previous = temp->previous;
            }
            *front = temp->next;
        }
        else if ( temp->next == NULL ) 
        { 
            temp->previous->next = temp->next;
        }
        else 
        {
            temp->previous->next = temp->next;
            temp->next->previous = temp->previous;
        }

        free( temp );
    }

    return oddnum;
}

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 ) 
{
    int oddnum = -1;

    Node *temp = *front;

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

    if ( temp != NULL )
    {
        oddnum = temp->data;
        
        if ( temp == *front ) 
        { 
            if ( temp->next != NULL )
            {
                temp->next->previous = temp->previous;
            }
            *front = temp->next;
        }
        else if ( temp->next == NULL ) 
        {
            temp->previous->next = temp->next;
        }
        else 
        {
            temp->previous->next = temp->next;
            temp->next->previous = temp->previous;
        }

        free( temp );
    }

    return oddnum;
}

int main(void) 
{
    Node *head = NULL;
    const int N = 10;
    
    for ( int i = N; i != 0; i-- )
    {
        push_front( &head, i );
    }
    
    display( head );

    for ( int num; ( num = DeleteFirstODD( &head ) ) != -1; )
    {
        printf( "The value of the deleted node is %d\n", num );
        display( head );
    }
    
    return 0;
}

The program output is

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
The value of the deleted node is 1
2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
The value of the deleted node is 3
2 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
The value of the deleted node is 5
2 -> 4 -> 6 -> 7 -> 8 -> 9 -> 10 -> null
The value of the deleted node is 7
2 -> 4 -> 6 -> 8 -> 9 -> 10 -> null
The value of the deleted node is 9
2 -> 4 -> 6 -> 8 -> 10 -> null
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I tried your code in a multiple programs and mine also, in the last else statement it wont work as expected, i tried to debug the code both of our codes has the same issue :( i still idk if this is the compiler whose working weird. –  Nov 23 '20 at 20:10
  • @it'sM See the demonstrative program. – Vlad from Moscow Nov 23 '20 at 20:40
  • @it'sM It seems you was using my function push_front from my previous answer to you similar question that had a missed statement.:) – Vlad from Moscow Nov 23 '20 at 20:48