C语言链表删除节点报错求助:食物卡路里列表功能实现问题
解决链表删除功能的编译报错与逻辑问题
咱们先逐个拆解你遇到的编译错误,再修正代码里的其他逻辑漏洞:
错误1:'struct info' has no member named 'current'
你在deleteNode函数里写了temp->current == y,但你的struct info结构体里根本没有current这个成员变量。这里是典型的逻辑错误——你想用y表示要删除的条目序号,却错误地把序号当成了结构体的字段。正确做法是遍历链表时计数,找到第y个节点再执行删除操作。
错误2:dereferencing pointer to incomplete type(printList函数)
这个错误有两个触发原因:
- 你定义
printList时用了struct list *info作为参数,但整个代码里从未定义过struct list类型,应该改成struct info *info(和你的链表节点结构体保持一致)。 - 在
main函数里调用printList()时没有传递链表头指针,必须传入first指针,也就是printList(first);。
其他需要修正的逻辑问题
除了编译错误,代码还有几个隐性bug:
- 重复创建第一个节点:你在循环外已经执行了
first = create(); current = first;,但循环内i=0时又重新创建了first,导致第一个节点内存泄漏,且覆盖了原指针。 - deleteNode调用参数错误:main里调用
deleteNode(y);不符合函数定义,应该传入链表头的指针地址&first和要删除的序号y,即deleteNode(&first, y);。 - 字符串读取冗余符号:
add_info函数里scanf("%s",&s->name);的&是多余的,s->name本身就是字符数组的首地址,直接写scanf("%s", s->name);即可。
修正后的完整代码
#include <stdio.h> #include <stdlib.h> struct info { int calories; char name[100]; struct info *next; }; void add_info(struct info *s); struct info *create(void); void printList(struct info *head); void deleteNode(struct info **head, int position); int main() { struct info *first = NULL; struct info *current = NULL; struct info *new_node = NULL; int x, i, y; printf("How many entries do you want? "); scanf("%d", &x); // 创建第一个节点 first = create(); current = first; add_info(current); // 创建剩余节点 for(i = 1; i < x; i++) { new_node = create(); current->next = new_node; current = new_node; add_info(current); } current->next = NULL; // 打印初始列表 printf("\nInitial list:\n"); printList(first); // 选择要删除的条目 printf("\n\nWhich entry would you like to remove? (Enter 1-based index) "); scanf("%d", &y); // 删除指定节点 deleteNode(&first, y); // 打印删除后的列表 printf("\nThe list after deletion is:\n"); printList(first); return 0; } void add_info(struct info *s) { printf("Insert number of calories: "); scanf("%d", &s->calories); printf("Insert name of the food: "); scanf("%s", s->name); // 移除冗余的&符号 s->next = NULL; } struct info *create(void) { struct info *initial = (struct info *)malloc(sizeof(struct info)); if (initial == NULL) { printf("Memory error"); exit(1); } return initial; } void deleteNode(struct info **head, int position) { if (*head == NULL) { printf("List is empty, nothing to delete.\n"); return; } struct info* temp = *head; struct info* prev = NULL; // 处理删除第一个节点的情况 if (position == 1) { *head = temp->next; free(temp); return; } // 遍历找到目标节点 int count = 1; while (temp != NULL && count < position) { prev = temp; temp = temp->next; count++; } // 处理序号超出链表长度的情况 if (temp == NULL) { printf("Position %d does not exist in the list.\n", position); return; } // 跳过目标节点并释放内存 prev->next = temp->next; free(temp); } void printList(struct info *head) { struct info *current = head; if (current == NULL) { printf("List is empty.\n"); return; } while (current != NULL) { printf("Calories: %d\t Food Name: %s\n", current->calories, current->name); current = current->next; } }
代码说明
- 修复了所有编译错误,确保代码可正常编译运行。
- 调整链表创建逻辑,避免内存泄漏问题。
deleteNode函数新增了空列表、无效序号的判断,支持基于1-based序号删除节点。printList函数增加了空列表处理,打印格式更清晰。
内容的提问来源于stack exchange,提问作者Andrei
相关产品推荐
相关产品推荐

