【问题标题】:why does my delete search linked list function not working?为什么我的删除搜索链表功能不起作用?
【发布时间】:2021-06-29 06:23:19
【问题描述】:

我在使用这个函数时遇到了这个问题,我想用给定的字符串删除所有节点,但是当我在我的程序上运行它时,它会从第一个 find char ex[] 中删除所有节点,我是 c 新手指导我

void delSearch(char ex[]){
    int del;
    struct file *temp = head;
    while(temp!=NULL){
        if(strcmp(temp->ex,ex)==0){
            file *temp2 = temp->next;
            file *temp3 = temp;
            temp->next=temp2;
            free(temp3);
            temp3 = NULL;
        }
        else
        {
        temp = temp->next;
        }
    }
}

【问题讨论】:

  • 如果您的问题与纯C代码有关,请不要标记C++。
  • temp->next=temp2; 等价于 temp->next=temp->next; 看起来有点多余
  • @Ollie 不清楚是需要删除给定字符串的第一个节点还是给定字符串的所有节点。
  • 您需要跟踪前一个节点。这样您就可以执行prev->next = temp->next; 之类的操作来从列表中删除temp 节点。您可能正在尝试使用temp->next=temp2; 来做到这一点。但这是错误的。它什么也不做,因为它只是 temp->next=temp->next(正如上面的评论中已经指出的那样)。
  • @Ollie 我问你一个问题不是吗?

标签: c linked-list singly-linked-list c-strings function-definition


【解决方案1】:

对于初学者来说,局部变量del

int del;

不在函数中使用。

该函数不会更改全局指针head,前提是它指向存储给定字符串的节点。

您还需要更新要删除的节点之前的节点的数据成员next。

函数如下所示。

void delSearch( const char ex[] )
{
    while ( head && strcmp( head->ex, ex ) == 0 )
    {
        struct file *tmp = head;
        head = head->next;
        free( tmp );
    }
    
    if ( head )
    {
        for ( struct file *current = head; current->next != NULL; )
        {
            if ( strcmp( current->next->ex, ex ) == 0 )
            {
                struct file *tmp = current->next;
                current->next = current->next->next;
                free( tmp );
            }
            else
            {
                current = current->next;
            }
        }
    }
}   

如果你想知道调用函数后删除了多少节点,可以通过以下方式更改函数。

size_t delSearch( const char ex[] )
{
    size_t total = 0;
    
    while ( head && strcmp( head->ex, ex ) == 0 )
    {
        struct file *tmp = head;
        head = head->next;
        free( tmp );
        ++total;
    }
    
    if ( head )
    {
        for ( struct file *current = head; current->next != NULL; )
        {
            if ( strcmp( current->next->ex, ex ) == 0 )
            {
                struct file *tmp = current->next;
                current->next = current->next->next;
                free( tmp );
                ++total;
            }
            else
            {
                current = current->next;
            }
        }
    }
    
    return total;
}   

另一种函数定义如下所示

size_t delSearch( const char ex[] )
{
    size_t total = 0;
    
    for ( struct file **current = &head; *current != NULL; )
    {
        if ( strcmp( ( *current )->ex, ex ) == 0 )
        {
            struct file *tmp = *current;
            *current = ( *current )->next;
            free( tmp );
            ++total;
        }
        else
        {
            current = &( *current )->next;
        }
    }
    
    return total;
}   

【讨论】:

  • 哇,真的很管用,非常感谢您的帮助,先生,我真的很感激!现在我知道我做错了什么
  • @Ollie 将答案标记为最佳答案即可。:)
  • 对不起,我该如何标记?我是stackoverflow的新手
  • @Ollie 您已将答案标记为最佳答案。:)
猜你喜欢
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
  • 2021-07-19
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多