c - 根据其内容从双向链表中删除结构(C 语言)

标签 c dll doubly-linked-list

我的程序是一个基本的 C 接口(interface),允许用户输入、向前打印、向后打印以及从列表中删除 MP3 记录。该列表在 C 语言中实现为 MP3 结构的双向链表。

除了删除之外,我的所有功能都工作正常。删除接收一个指向列表头节点的指针和一个字符串,表示您要删除哪个艺术家的记录。在我的 main 中,我记录了用户输入并验证了它是否被正确记录。然后我将用户输入和头引用传递到以下函数中以删除所述 MP3 记录。但是,我的程序构建并执行得很好,但在调用删除函数时实际上并没有删除任何记录。任何帮助表示赞赏。

为了清楚起见,我已经查看了有关从 DLL 中删除节点的堆栈上的多个问题,但是它们都与保存整数值的简单 DLL 相关,并且似乎不能很好地转换为我的场景。如果这被认为是重复的问题,我深表歉意,如果是这样,请指出我正在重复的问题。再次感谢您提供的所有帮助。下面是我的功能

void deleteMP3(struct MP3* head_ref, char* artist)
{
    //Declaring a temp struct to hold the node that needs to be deleted
    struct MP3* temp;

    //Check if the head node contains the artist to be deleted
    if(head_ref->artist == artist)
    {
        //Set temp to the current head ref so it can be deleted
        temp = head_ref;

        //Set head_ref to the next node in the list
        head_ref = head_ref->next;

        //Free the memory associated with the MP3 to be deleted
        free(temp->artist);
        free(temp->title);
        free(temp->date);
        free(temp);
    }

    //Traverse the list checking each MP3's artist field
    while(head_ref != NULL)
    {
        //Check the artist of the current MP3 against the input. Delete it if it needs to be deleted
        if(head_ref->artist == artist)
        {
            //Set temp to the current MP3
            temp = head_ref;

            //Check if the MP3 is the last MP3. If not, change the field of the next node in the list
            if(head_ref->next != NULL)
            {
                //Sets the previous field of the next node in the list to the previous field of the node to be deleted
                head_ref->next->prev = head_ref->prev;
            }

            //Change the next pointer of the previous MP3 in the list to the MP3 following the one to be deleted
            head_ref->prev->next = head_ref->next;

            //Free the memory           
            free(temp->artist);
            free(temp->title);
            free(temp->date);
            free(temp);
        }

        //Traverse forward
        head_ref = head_ref->next;
    }
}

最佳答案

代码中存在一些问题,但以下两个是最关键的:

1.) 使用strcmp 比较字符串。 head_ref->artist == Artist 比较指针,而不是内容,并且通常不太可能传入 DLL 元素指向的同一指针。

2.) 如果头被删除,则需要将"new"头传回给deleteMP3的调用者;否则,传递给deleteMP3的变量仍将保存指向(已删除)节点的指针。因此,将 void deleteMP3(struct MP3* head_ref, char*artist) 更改为 struct MP3 *deleteMP3(struct MP3* head_ref, char*artist) 并返回实际的头DLL(无论是否更改)。

关于c - 根据其内容从双向链表中删除结构(C 语言),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52228297/

相关文章:

c - 如何在 C 中拆分 HTTP header ?

algorithm - 黑客排名 : Inserting a Node Into a Sorted Doubly Linked List - Kotlin

Java排序双向链表添加方法

delphi - ShareMem/与 Delphi DLL 进行字符串交换

c++ - 如何重构现在驻留在静态库中的全局记录器?

c++ - DLL 之间的交互

java - 为什么我的迭代器 foreach 循环永远不会进入/执行?

c - 强制 RaspberryPi 上的传感器休眠

c - 从内核获取基地址寄存器的宏是如何工作的

c - 简单的for循环无限运行