C 语言实现单链表全功能操作:创建、增删查改与逆置测试
1. 下面是使用C语言实现单链表的各种操作的示例代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct LinkedList { struct Node* head; }; void initLinkedList(struct LinkedList* list) { list->head = NULL; } void destroyLinkedList(struct LinkedList* list) { struct Node* current = list->head; while (current != NULL) { struct Node* next = current->next; free(current); current = next; } } void insert(struct LinkedList* list, int position, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; if (position == 0) { newNode->next = list->head; list->head = newNode; } else { struct Node* current = list->head; for (int i = ; i < position - 1; i++) { current = current->next; } newNode->next = current->next; current->next = newNode; } } void remove(struct LinkedList* list, int position) { if (list->head == NULL) { return; } struct Node* target; if (position == 0) { target = list->head; list->head = target->next; } else { struct Node* current = list->head; for (int i = 0; i < position - 1; i++) { current = current->next; } target = current->next; current->next = target->next; } free(target); } int find(struct LinkedList* list, int data) { struct Node* current = list->head; int position = ; while (current != NULL) { if (current->data == data) { return position; } current = current->next; position++; } return -1; // Not found } int length(struct LinkedList* list) { struct Node* current = list->head; int length = 0; while (current != NULL) { length++; current = current->next; } return length; } int isEmpty(struct LinkedList* list) { return (list->head == NULL); } int get(struct LinkedList* list, int position) { struct Node* current = list->head; for (int i = 0; i < position; i++) { current = current->next; } return current->data; } void reverse(struct LinkedList* list) { struct Node* prev = NULL; struct Node* current = list->head; struct Node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } list->head = prev; } void printList(struct LinkedList* list) { struct Node* current = list->head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { struct LinkedList list; initLinkedList(&list); // 测试代码 insert(&list, 0, 25); insert(&list, 1, 21); insert(&list, 2, 46); insert(&list, 3, 90); insert(&list, 4, 12); insert(&list, 5, 98); insert(&list, 5, 2); printf("Length of the list: %d\n", length(&list)); printList(&list); insert(&list, 2, 43); printf("After inserting 43 at position 2: "); printList(&list); remove(&list, 3); printf("After removing element at position 3: "); printList(&list); reverse(&list); printf("Reversed list: "); printList(&list); destroyLinkedList(&list); return 0; } ``` 2. 按照要求进行单链表的测试: ```c int main() { struct LinkedList list; initLinkedList(&list); // 通过键盘输入数据建立一个单链表,并求表长并输出该线性表 int data; printf("Please enter elements (-1 to stop): "); while (scanf("%d", &data) && data != -1) { insert(&list, length(&list), data); } printf("Length of the list: %d\n", length(&list)); printList(&list); // 依次进行数据的插入、删除和查找,并在插入或删除数据后,再求表长并输出线性表 int position; printf("Insert element at which position: "); scanf("%d", &position); insert(&list, position - 1, 43); printf("After inserting 43 at position %d: ", position); printList(&list); printf("Length of the list: %d\n", length(&list)); printf("Remove element at which position: "); scanf("%d", &position); remove(&list, position - 1); printf("After removing element at position %d: ", position); printList(&list); printf("Length of the list: %d\n", length(&list)); printf("Find element: "); scanf("%d", &data); int foundPosition = find(&list, data); if (foundPosition != -1) { printf("%d found at position %d\n", data, foundPosition + 1); } else { printf("%d not found\n", data); } // 将最终的单链表整个逆置,并输出线性表 reverse(&list); printf("Reversed list: "); printList(&list); destroyLinkedList(&list); return 0; } ``` 这是一个完整的单链表实现,可以按照要求进行测试并进行相应操作。 ######[AI写代码神器 | 4796点数解答 | 2024-03-28 16:24:20]
- Python实现Q老师成绩管理任务:修改与查看学生成绩(字节豆包 | 191点数解答 | 2025-12-20 20:55:41)70
- Python 代码助力 Q 老师高效管理班级学生成绩:修改与查看一键搞定(字节豆包 | 326点数解答 | 2025-12-27 20:48:12)70
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- Java 创建 HTTP POST 请求实现文件上传:代码示例与注意要点 (字节豆包 | 300点数解答 | 2024-12-06 16:07:20)336
- C#工程师必知:数组、链表、哈希、队列、栈数据结构优缺点大揭秘! (百度文心 | 561点数解答 | 2023-11-09 17:56:30)260
- Java 实现仿 Windows 资源管理器:文件管理器功能全解析与代码示例(字节豆包 | 623点数解答 | 2025-01-01 14:59:04)246
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)310
- Golang开发必备:两种实现GET操作的神级方法揭秘!(百度文心 | 465点数解答 | 2023-11-09 18:11:57)244
- 仅一次交换,小写字母字符串能否变身周期字符串?(字节豆包 | 189点数解答 | 2024-10-13 15:08:54)313
- Python实现:O(n)复杂度判断小写字母串能否经一次交换成周期串(字节豆包 | 251点数解答 | 2024-10-13 15:10:03)176
- 巧用箭头函数和 map 方法:将数组元素轻松翻倍!(阿里通义 | 178点数解答 | 2024-10-29 09:35:26)181
- 用原生 JS 和 ECMA 高级语法打造带添加删除功能的品牌列表 (字节豆包 | 676点数解答 | 2024-11-18 17:41:17)185