From 5e874ceb8048f9d6ebf0cfd6564d78efe47e0eb5 Mon Sep 17 00:00:00 2001 From: LHY0125 <3364451258@qq.com> Date: Thu, 16 Oct 2025 20:41:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=95=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E9=80=80=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复case 7退出选项无法正常退出程序的问题 - 在退出前添加freeList()调用释放链表内存,避免内存泄漏 - 使用return 0正常退出程序,替换原来的break语句 - 修正编译命令注释中的文件名引用 - 程序现在可以通过选择7正常退出 --- 数据结构/单链表的增删改查.c | 49 +++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/数据结构/单链表的增删改查.c b/数据结构/单链表的增删改查.c index ce2ace6..dd8c472 100644 --- a/数据结构/单链表的增删改查.c +++ b/数据结构/单链表的增删改查.c @@ -7,8 +7,8 @@ /** * @brief 将指令复制到PowerShell - * gcc "数据结构\链表插入.c" -o 数据结构\output\链表插入.exe - .\数据结构\output\链表插入.exe + * gcc "数据结构\单链表的增删改查.c" -o 数据结构\output\单链表的增删改查.exe + .\数据结构\output\单链表的增删改查.exe */ /*代码说明: @@ -64,6 +64,27 @@ void insertAtHead(struct ListNode **head, int value) *head = newNode; } +// 在链表尾部插入新节点的函数(尾插法) +void insertAtEnd(struct ListNode **head, int value) +{ + // 创建新节点并将其插入到链表尾部 + struct ListNode *newNode = createNode(value); + // 如果链表为空,新节点直接成为头节点 + if (*head == NULL) + { + *head = newNode; + return; + } + // 否则,遍历到链表尾部 + struct ListNode *current = *head; + while (current->next != NULL) + { + current = current->next; + } + // 将新节点连接到链表尾部 + current->next = newNode; +} + // 删除第一个值为value的节点,返回是否删除成功 int deleteByValue(struct ListNode **head, int value) { @@ -121,27 +142,6 @@ int deleteAtPosition(struct ListNode **head, int pos) return 1; } -// 在链表尾部插入新节点的函数(尾插法) -void insertAtEnd(struct ListNode **head, int value) -{ - // 创建新节点并将其插入到链表尾部 - struct ListNode *newNode = createNode(value); - // 如果链表为空,新节点直接成为头节点 - if (*head == NULL) - { - *head = newNode; - return; - } - // 否则,遍历到链表尾部 - struct ListNode *current = *head; - while (current->next != NULL) - { - current = current->next; - } - // 将新节点连接到链表尾部 - current->next = newNode; -} - // 打印链表 void printList(struct ListNode *head) { @@ -326,7 +326,8 @@ int main() break; case 7: printf("程序结束\n"); - break; + freeList(head); // 退出前释放链表内存 + return 0; default: printf("无效选项,请输入 1-7\n"); }