修复单链表程序退出功能
- 修复case 7退出选项无法正常退出程序的问题 - 在退出前添加freeList()调用释放链表内存,避免内存泄漏 - 使用return 0正常退出程序,替换原来的break语句 - 修正编译命令注释中的文件名引用 - 程序现在可以通过选择7正常退出
This commit is contained in:
+25
-24
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user