My program is a basic C interface that allows the user to enter, print forwards, print backwards and delete MP3 records from a list. The list is implemented as a doubly linked list of MP3 structures in the C language.
我的程序是一个基本的C接口,允许用户从列表中输入,打印,向前打印和删除MP3记录。该列表被实现为C语言中的MP3结构的双重链接列表。
All my functions except Delete work fine. Delete takes in a pointer to the head node of the list and a character string signifying which artist's records you would like deleted. In my main I record the user input and have verified it is being recorded correctly. Then I pass the user input and the head reference into the following function to delete said MP3 records. However, my program builds and executes fine but does not actually delete any records when the delete function is called. Any help is appreciated.
我的所有功能除了删除工作正常。删除接受指向列表头节点的指针和一个字符串,表示您要删除哪个艺术家的记录。在我的主要内容中,我记录了用户输入并验证了它是否正确记录。然后我将用户输入和头部引用传递给以下函数以删除所述MP3记录。但是,我的程序构建并执行正常,但在调用delete函数时实际上并不删除任何记录。任何帮助表示赞赏。
For clarity, I have looked at multiple questions on stack regarding deleting nodes from a DLL, however they all are regarding simple DLL holding integer values and don't seem to translate well to my scenario. I apologize if this is considered a duplicate question, if so, please point me to the question I am duplicating. Thanks again for any and all assistance. Below is my function
为清楚起见,我已经查看了关于从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 个解决方案
#1
0
There are some issues in the code, but the following two are the most critical ones:
代码中存在一些问题,但以下两个是最关键的问题:
1.) use strcmp
to compare strings. head_ref->artist == artist
compares the pointers, not the contents, and it's usually unlikely that you pass in the same pointer to which the DLL elements point to.
1.)使用strcmp来比较字符串。 head_ref-> artist == artist比较指针,而不是内容,并且通常不太可能传入DLL元素指向的相同指针。
2.) if the head is deleted, you need to pass back the "new" head to the caller of deleteMP3
; otherwise the variable passed to deleteMP3
will still hold a pointer to the (deleted) node. So change void deleteMP3(struct MP3* head_ref, char* artist)
to struct MP3 *deleteMP3(struct MP3* head_ref, char* artist)
and return the actual head of the DLL (whether changed or not).
2.)如果删除头,则需要将“new”头传回deleteMP3的调用者;否则传递给deleteMP3的变量仍将保存指向(已删除)节点的指针。因此,将void deleteMP3(struct MP3 * head_ref,char * artist)更改为struct MP3 * deleteMP3(struct MP3 * head_ref,char * artist)并返回DLL的实际头部(无论是否更改)。
#1
0
There are some issues in the code, but the following two are the most critical ones:
代码中存在一些问题,但以下两个是最关键的问题:
1.) use strcmp
to compare strings. head_ref->artist == artist
compares the pointers, not the contents, and it's usually unlikely that you pass in the same pointer to which the DLL elements point to.
1.)使用strcmp来比较字符串。 head_ref-> artist == artist比较指针,而不是内容,并且通常不太可能传入DLL元素指向的相同指针。
2.) if the head is deleted, you need to pass back the "new" head to the caller of deleteMP3
; otherwise the variable passed to deleteMP3
will still hold a pointer to the (deleted) node. So change void deleteMP3(struct MP3* head_ref, char* artist)
to struct MP3 *deleteMP3(struct MP3* head_ref, char* artist)
and return the actual head of the DLL (whether changed or not).
2.)如果删除头,则需要将“new”头传回deleteMP3的调用者;否则传递给deleteMP3的变量仍将保存指向(已删除)节点的指针。因此,将void deleteMP3(struct MP3 * head_ref,char * artist)更改为struct MP3 * deleteMP3(struct MP3 * head_ref,char * artist)并返回DLL的实际头部(无论是否更改)。