【问题标题】:Deleting first occurrence in a doubly linked list删除双向链表中的第一个匹配项
【发布时间】:2023-03-25 23:00:02
【问题描述】:

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

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

【问题讨论】:

    标签: c struct linked-list doubly-linked-list function-definition


    【解决方案1】:

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

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

    不正确。你必须写

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

    如果函数的返回类型不是 void,那么如果没有函数中的表达式,则不能使用 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
    

    【讨论】:

    • 我已将该部分更改为以下部分: while (temp != NULL ) { if(temp % 2 != 0)oddnum = temp->data; //将奇数节点的数据存储到整数oddnum else temp = temp->next; } //这可能吗?
    • @it'sM 没关系。在这个表达式 temp % 2 != 0 中,您尝试使用指针执行算术运算符 %。
    • 非常感谢你!!!!!!我对我的代码进行了更改以使其变得简单,因为我的讲师没有给我空间来让我的代码发挥创造力..所以..现在的问题是当我更改节点值并假设节点一个值为 2 所以下一个节点值为 3 ,不,我们需要删除中间的节点,问题是当我运行代码时它的空黑色命令..你能帮我吗?
    • @it'sM 以这种主要方式更改问题中的代码是一个坏主意,因为这会使读者对问题和答案感到困惑。您可以使用更新后的代码提出新问题。
    • 啊哈,非常感谢!顺便说一句,我将使用您的代码作为我的作业的参考,因为我的教授就是这样的人:)
    猜你喜欢
    • 2014-10-13
    • 2016-05-31
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多