删除双向链表中的第一个匹配项

Deleting first occurrence in a doubly linked list

我有一个任务要做..任务是删除第一个奇数只有我写了代码但老实说我对此没有信心..如果你们能帮助我我会很高兴。 该函数删除第一个具有奇数值的节点,因此该函数搜索第一个具有奇数值的节点并将其删除。

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

对于初学者来说,这个 typedef 声明

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

不正确。你必须写

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

如果一个函数的 return 类型不是 void 那么你不能像在函数

中那样使用没有表达式的 return 语句
return;

函数可以 return 整数值 1 在具有奇数值的节点被删除的情况下或 0 否则。

函数内的代码不正确。例如这个循环

while (ptr != NULL && ptr % 2 != 0) {

    oddnum = ptr->data; 
    ptr = ptr->next;
}

没有意义。

函数可以这样定义

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

这是一个演示程序。

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

程序输出为

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