From ac374e89bf588c9ede1f63d3e63e998607237c4a Mon Sep 17 00:00:00 2001 From: LHY20 <3364451258@qq.com> Date: Sun, 20 Jul 2025 22:51:19 +0800 Subject: [PATCH] Add new linked list operations: delete and clear functions --- 翁凯C语言/13/链表的删除.c | 129 ++++++++++++++++++++++++++++++++++++++ 翁凯C语言/13/链表的搜索.c | 17 +++++ 翁凯C语言/13/链表的清除.c | 128 +++++++++++++++++++++++++++++++++++++ 3 files changed, 274 insertions(+) create mode 100644 翁凯C语言/13/链表的删除.c create mode 100644 翁凯C语言/13/链表的清除.c diff --git a/翁凯C语言/13/链表的删除.c b/翁凯C语言/13/链表的删除.c new file mode 100644 index 0000000..339c7b8 --- /dev/null +++ b/翁凯C语言/13/链表的删除.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#include +#endif + +/** + * 链表操作示例程序 + * + * 本程序演示了单向链表的基本操作: + * 1. 创建链表并添加节点 + * 2. 遍历并打印链表 + * 3. 在链表中查找特定值 + * 4. 从链表中删除特定值的节点 + */ + +typedef struct _node +{ + int value; + struct _node *next; +} Node; + +typedef struct _list +{ + Node *head; + Node *tail; +} List; + +void add(List *pList, int value) +{ + Node *p = (Node *)malloc(sizeof(Node)); + p->value = value; + p->next = NULL; + // 将节点插入链表 + Node *last = pList->head; + if (last != NULL) + { + while (last->next != NULL) + { + last = last->next; + } + // attach + last->next = p; + } + else + { + pList->head = p; + } +} + +void print(List *pList) +{ + Node *p; + for (p = pList->head; p; p = p->next) + { + printf("%d\t", p->value); + } + printf("\n"); +} + +int main() +{ + // 设置控制台编码为UTF-8 +#ifdef _WIN32 + system("chcp 65001 > nul"); // 设置控制台编码为UTF-8 + SetConsoleOutputCP(65001); // 设置控制台输出编码 + SetConsoleCP(65001); // 设置控制台输入编码 +#endif + + List list; + list.head = NULL; + list.tail = NULL; + int number; + do + { + printf("请输入一个整数:"); + scanf("%d", &number); + if (number != -1) + { + + add(&list, number); + } + } while (number != -1); + print(&list); + + scanf("%d", &number); + Node *p; + int isFound = 0; + for (p = list.head; p; p = p->next) + { + if (p->value == number) + { + printf("找到!\n"); + isFound = 1; + break; + } + } + if (!isFound) + { + printf("未找到!\n"); + } + + Node *q; + for (q = NULL, p = list.head; p; q = p, p = p->next) + { + if (p->value == number) + { + if (q == NULL) + { + list.head = p->next; + } + else + { + q->next = p->next; + } + free(p); + printf("删除成功!\n"); + + break; + } + } + + return 0; +} \ No newline at end of file diff --git a/翁凯C语言/13/链表的搜索.c b/翁凯C语言/13/链表的搜索.c index f16c879..3688008 100644 --- a/翁凯C语言/13/链表的搜索.c +++ b/翁凯C语言/13/链表的搜索.c @@ -81,5 +81,22 @@ int main() } while (number!=-1); print(&list); + scanf("%d", &number); + Node *p; + int isFound = 0; + for (p=list.head; p; p=p->next) + { + if (p->value==number) + { + printf("找到!\n"); + isFound = 1; + break; + } + } + if (!isFound) + { + printf("未找到!\n"); + } + return 0; } \ No newline at end of file diff --git a/翁凯C语言/13/链表的清除.c b/翁凯C语言/13/链表的清除.c new file mode 100644 index 0000000..af4f2da --- /dev/null +++ b/翁凯C语言/13/链表的清除.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#include +#include +#endif + +// !链表 +/* + 1. +*/ + +typedef struct _node{ + int value; + struct _node *next; +} Node; + +typedef struct _list{ + Node *head; + Node *tail; +} List; + +void add(List* pList, int value) +{ + Node *p=(Node*)malloc(sizeof(Node)); + p->value = value; + p->next = NULL; + // 将节点插入链表 + Node *last = pList->head; + if (last!=NULL) + { + while (last->next!=NULL) + { + last = last->next; + } + // attach + last->next = p; + } + else + { + pList->head = p; + } +} + +void print(List* pList) +{ + Node *p; + for (p=pList->head; p; p=p->next) + { + printf("%d\t", p->value); + } + printf("\n"); +} + +int main() +{ + // 设置控制台编码为UTF-8 +#ifdef _WIN32 + system("chcp 65001 > nul"); // 设置控制台编码为UTF-8 + SetConsoleOutputCP(65001); // 设置控制台输出编码 + SetConsoleCP(65001); // 设置控制台输入编码 +#endif + + List list; + list.head = NULL; + list.tail = NULL; + int number; + do { + printf("请输入一个整数:"); + scanf("%d", &number); + if (number!=-1) + { + + add(&list, number); + + } + } while (number!=-1); + print(&list); + + scanf("%d", &number); + Node *p; + int isFound = 0; + for (p=list.head; p; p=p->next) + { + if (p->value==number) + { + printf("找到!\n"); + isFound = 1; + break; + } + } + if (!isFound) + { + printf("未找到!\n"); + } + + Node *q; + for (q = NULL, p = list.head; p; q=p, p = p->next) + { + if (p->value == number) + { + if (q==NULL) + { + list.head = p->next; + } + else + { + q->next = p->next; + } + free(p); + printf("删除成功!\n"); + + break; + } + } + + for (p=list.head; p; p=p) + { + q=p->next; + free(p); + } + + return 0; +} \ No newline at end of file