【问题标题】:How to delete a node in a linked list, based off a string如何根据字符串删除链表中的节点
【发布时间】:2014-12-06 07:51:38
【问题描述】:

正如标题所暗示的那样。删除链表中的第一个节点就像一个魅力。是那些其他的节点!好的,这是我的代码。我认为我正在正确删除,但我没有正确链接。

结构体和类型定义如下所示:

typedef struct List *ListP;
typedef struct Entry *EntryP;
typedef char *ListItemP;

struct List
{
    int sizeL;
    struct Entry *head;
};

struct Entry
{
    ListItemP content;
    struct Entry *next;
    int sizeL;
};

removeItemList 函数在 main.c 中的工作方式是传递一个列表指针 (ListP thisList) 和一个字符串 (ListItemP thisItem)。传递参数后,该函数会搜索与列表中的节点相同的字符串(通过 strcmp()),并在找到时将其删除。 NewEntry 初始化了 Entry 结构,并有一个将字符传递给 newEntry->content 的输入。无论如何,这是功能:

void removeItemList(ListP thisList, ListItemP thisItem)
{
    EntryP temp = newEntry(0x00);
    EntryP temp2 = newEntry(0x00);

    temp->next = thisList->head;
    temp2->next = thisList->head;
    if (strcmp(temp->next->content, thisItem) == 0)
    {
        thisList->head = temp->next->next;
    }
    else {
         while(temp->next != 0x00)
         {
            if(strcmp(temp->next->content,thisItem) == 0) {
                break;
            }
            temp->next = temp->next->next;
            temp->sizeL++;
         }
        if (temp->next == 0x00) {
        }
        else {
            int i = 0;
            for(i = 0; i < temp->sizeL - 1 ; i++)
            {
                temp2->next = temp2->next->next;
                printf("%s \n", temp2->content);
            }
            temp2->next = temp->next->next;
            free(temp2->next);
        }
    }
    thisList->sizeL--;
}

我认为被删除节点之前的节点最终被指向 null。不过,我无法弄清楚如何解决这个问题。我想这就是我在这里的原因!任何事情都将不胜感激,非常感谢!

编辑:更新代码,加上显示列表代码

更新了 removeItemList()

void removeItemList(ListP thisList, ListItemP thisItem)
{
    EntryP current = newEntry(0x00);
    EntryP prev = newEntry(0x00);

    prev = 0x00;
    current = thisList->head;

    if (strcmp(current->content, thisItem) == 0)
    {
        thisList->head = current->next->next;
    }
    else {

        while (current->next != 0x00)
        {
            prev = current;
            current = current->next;
            if (strcmp(current->content, thisItem) == 0)
            {   
                prev->next = current->next;
                free(current);
            }
        }
    }
}

显示列表():

void displayList(ListP thisList)
{   
    EntryP temp = newEntry(0x00);
    temp->next = thisList->head;
    while(temp->next != 0x00)
    {
       printf("%s \n", temp->next->content);
       temp->next = temp->next->next;
    }
    free(temp);
}

如果我只使用 printf() 语句,我可以正常访问所有内容,并且该节点似乎已删除。但是,如果我尝试使用 displayList() 打印它们,则在打印节点后会在删除节点之前出现段错误。所以看起来好像我没有正确链接节点。

【问题讨论】:

    标签: c pointers linked-list nodes


    【解决方案1】:

    为了删除任何中间节点,维护两个指向列表的指针,prev 和 cur。将 prev 初始化为 null 并将 cur 初始化为列表的头部。现在遍历列表,直到遇到要删除的节点。在移动到分析中的下一个节点之前,将 prev 重新分配给 cur 并将 cur 分配给 cur->next。 到达所需节点时,执行

    prev->next = cur->next;
    free(cur);
    
    return;// If necessary.
    

    整个伪代码:

    prev = null;
    cur = list->head;
    
    while(cur!=null)
    {
       if(//This is the node to be deleted)
       {
            prev->next = cur->next;
            free(cur);
            return;
       }
    
       prev = cur;
       cur = cur->next;
    
    }
    //Node not found case
    

    现在将此方案集成到您的代码中,您应该没问题。在此之前,只需明确检查第一个节点,因为这不适合上述用例。

    更新:我注意到您的函数 DisplayList() 中存在一些错误。为什么在遍历期间使用 temp->next 进行分析?对我来说似乎是一种迂回的方法。只需用温度分析即可。

    void displayList(ListP thisList)
    {   
        EntryP temp = thisList->head;
        while(temp!= 0x00)
        {
          printf("%s \n", temp->content);
          temp = temp->next;
        }
    }
    

    为什么在列表显示期间释放临时节点?如果您只想显示列表内容,那么释放节点没有任何意义。对我来说。

    【讨论】:

    • 所以,如果我使用简单的 printf 语句,我的列表中的所有内容都在那里。但是,如果我使用类似的功能访问它
    • 请发布您的完整查询,以便我尝试帮助您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2017-02-10
    相关资源
    最近更新 更多