如果双向链表是奇数,删除中间和最后一个节点

deleting middle and last node if the doubly linked list is odd

我有一个任务要做..任务是只删除双向链表中第一次出现的奇数,我必须考虑所有特殊情况,例如:if odd @ beginning ,中间或最后。到目前为止,我的代码工作正常我检查了它,它只删除前面(如果它很奇怪),但在其他情况下,例如删除最后一个/中间..我无法为他们获得输出.基本上 运行 命令没有显示任何内容:(

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

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

你错了。此代码段

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

当列表只包含一个节点时可以调用未定义的行为,因为在这条语句之后

    *front = (*front)->next;

指针front将等于NULL,您不能在以下语句中使用此空指针

    (*front)->previous = NULL;

函数可以通过以下方式定义。我假设如果在返回存储值时找到这样一个具有奇数值的节点。否则返回-1。

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

这是一个演示程序。

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

程序输出为

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