C语言:链表插入、删除、打印功能完整实现及交互程序
以下是使用 C 语言实现的链表练习程序: ```c #include <stdio.h> #include <stdlib.h> typedef struct { int value; } datastruct_t; typedef struct _list { datastruct_t* pstdata; struct _list* nextptr; } listnode_t; // 创建新节点 listnode_t* createNode(int value) { listnode_t* newNode = (listnode_t*)malloc(sizeof(listnode_t)); if (newNode == NULL) { printf("Memory allocation failed!\n"); return NULL; } newNode->pstdata = (datastruct_t*)malloc(sizeof(datastruct_t)); if (newNode->pstdata == NULL) { printf("Memory allocation failed!\n"); free(newNode); return NULL; } newNode->pstdata->value = value; newNode->nextptr = NULL; return newNode; } // 插入节点(按从小到大顺序) void insertNode(listnode_t head, int value) { listnode_t* newNode = createNode(value); if (*head == NULL || value <= (*head)->pstdata->value) { newNode->nextptr = *head; *head = newNode; return; } listnode_t* current = *head; while (current->nextptr!= NULL && current->nextptr->pstdata->value < value) { current = current->nextptr; } newNode->nextptr = current->nextptr; current->nextptr = newNode; } // 删除节点 void deleteNode(listnode_t head, int value) { listnode_t* current = *head; listnode_t* prev = NULL; while (current!= NULL) { if (current->pstdata->value == value) { if (prev == NULL) { *head = current->nextptr; } else { prev->nextptr = current->nextptr; } free(current->pstdata); free(current); return; } prev = current; current = current->nextptr; } printf("Element not found in the list!\n"); } // 打印链表 void printList(listnode_t* head) { listnode_t* current = head; while (current!= NULL) { printf("%d ", current->pstdata->value); current = current->nextptr; } printf("\n"); } int main() { listnode_t* head = NULL; int choice; printf("please enter your choice:\n"); printf("1 is to insert an element to the list.\n"); printf("2 is to delete an element from the list.\n"); printf("3 is to quit.\n"); while (1) { printf("? "); scanf("%d", &choice); if (choice == 1) { int value; printf("enter an int to insert: "); scanf("%d", &value); insertNode(&head, value); } else if (choice == 2) { int value; printf("enter an int to be deleted: "); scanf("%d", &value); deleteNode(&head, value); } else if (choice == 3) { break; } else { printf("Invalid choice. Please try again.\n"); } printList(head); } // 释放链表内存 listnode_t* temp; while (head!= NULL) { temp = head; head = head->nextptr; free(temp->pstdata); free(temp); } return 0; } ``` ######[AI写代码神器 | 914点数解答 | 2024-08-26 05:59:38]
- Python 实现简易密码设置系统:多种功能交互与安全要点揭秘(字节豆包 | 286点数解答 | 2025-03-28 10:43:21)355
- C语言实现多功能密码设置系统:含代码及安全注意要点(字节豆包 | 414点数解答 | 2025-03-28 10:43:55)343
- C 语言实现:超简单密码设置系统,含代码与注意要点!(字节豆包 | 519点数解答 | 2025-03-28 12:42:02)384
- C语言实现密码管理系统:含功能代码与安全注意事项(字节豆包 | 409点数解答 | 2025-04-05 18:09:56)333
- C语言实现多功能密码系统:初始化、确认、设置、显示、重置与退出(字节豆包 | 454点数解答 | 2025-04-05 18:48:42)332
- C语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 413点数解答 | 2025-04-06 15:37:16)279
- C 语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 403点数解答 | 2025-04-06 15:37:54)292
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- C 语言实现单链表全功能操作:创建、增删查改与逆置测试( | 4796点数解答 | 2024-03-28 16:24:20)340
- C++实现顺序存储结构基本操作:初始化、插入、删除等全解析(GPT | 1743点数解答 | 2024-09-26 00:20:17)281
- C++实现单循环链表:初始化、插入、删除等操作及测试验证(GPT | 2102点数解答 | 2024-10-13 13:39:58)154
- Java 实现通用文件存储系统:含随机读写与空闲空间回收(字节豆包 | 686点数解答 | 2024-11-21 16:25:51)223