gpt4 book ai didi

c - 从结构中删除项目

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:16 25 4
gpt4 key购买 nike

我有以下链表:

struct scoreentry_node {
struct scoreentry_node *next;
int score;
char name[1];
}
;

typedef struct scoreentry_node *score_entry;

我正在尝试编写一个函数来删除所有包含特定名称的节点。这是我到目前为止所知道的,但我不确定我是对的:

score_entry disqualify(score_entry a, char* name)
{
score_entry tmp = a;
while (tmp != NULL){
if (strcmp(tmp->name, name) == 0)
{
score_entry trash = tmp;
tmp = tmp->next;
free(trash);
}
else { tmp = tmp->next; }
}
return a;
}

它给我堆错误..有什么建议吗?

最佳答案

score_entry disqualify(score_entry a, char* name)
{
score_entry new_front = a, tmp;
// delete "wrong" entries from the front
while (new_front != NULL){
if (strcmp(new_front->name, name) == 0)
{
score_entry trash = new_front;
new_front = new_front->next;
free(trash);
}
else
{
// first list entry is valid
// delete "wrong" entries from inside the list
tmp = new_front;
while ( tmp->next != NULL )
{
if ( strcmp(tmp->next->name,name)==0 )
{
score_entry trash = tmp->next;
tmp->next = tmp->next->next;
free(trash);
} else
{
tmp = tmp->next;
}
}
}
}
return new_front;
}

你还应该获得一些与常见数据结构相关的书籍 - 你似乎对这些东西很感兴趣,它可能对你有很大帮助。

关于c - 从结构中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9917432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com