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; // 打印链表函数 void printlist(listnode_t* head) { listnode_t* current = head; while (current!= NULL) { printf("%d ", current->pstdata->value); current = current->nextptr; } printf("\n"); } // 按数字从大到小顺序插入节点 listnode_t* insertnode(listnode_t* head, int value) { listnode_t* newNode = (listnode_t*)malloc(sizeof(listnode_t)); newNode->pstdata = (datastruct_t*)malloc(sizeof(datastruct_t)); newNode->pstdata->value = value; listnode_t* current = head; listnode_t* prev = NULL; while (current!= NULL && current->pstdata->value > value) { prev = current; current = current->nextptr; } if (prev == NULL) { newNode->nextptr = head; head = newNode; } else { newNode->nextptr = current; prev->nextptr = newNode; } return head; } // 删除指定值的节点 listnode_t* deletenode(listnode_t* head, int value) { listnode_t* current = head; listnode_t* prev = NULL; while (current!= NULL && current->pstdata->value!= value) { prev = current; current = current->nextptr; } if (current == NULL) { printf("Value not found in the list.\n"); return head; } if (prev == NULL) { head = current->nextptr; } else { prev->nextptr = current->nextptr; } free(current->pstdata); free(current); return head; } int main() { listnode_t* head = NULL; int choice; printf("please enter your choice: 1 is to insert an element to the list. 2 is to delete an element from the list. 3 is to quit.\n"); do { printf("? "); scanf("%d", &choice); switch (choice) { case 1: { int value; printf("enter an int to insert: "); scanf("%d", &value); head = insertnode(head, value); printlist(head); break; } case 2: { int value; printf("enter an int to be deleted: "); scanf("%d", &value); head = deletenode(head, value); printlist(head); break; } case 3: break; default: printf("Invalid choice. Please try again.\n"); } } while (choice!= 3); // 释放链表内存 listnode_t* temp; while (head!= NULL) { temp = head; head = head->nextptr; free(temp->pstdata); free(temp); } return 0; } ``` ######[AI写代码神器 | 828点数解答 | 2024-08-26 06:23:19]
- C++ 实现:计算一排鹅与最高、最矮鹅的身高差及代码详解(字节豆包 | 549点数解答 | 2025-11-16 20:19:06)81
- 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
- Java代码实现从JSON数据中提取ID,涉及“数据”与“项目 - 知识库”结构(字节豆包 | 603点数解答 | 2025-04-15 22:48:51)234
- 编程揭秘:计算 𝑛! 在 𝑃 进制下末尾零个数的方法与代码实现(DeepSeek | 549点数解答 | 2026-01-11 17:49:54)46
- Python 实现:打印 -n 到 n 范围内所有整数的代码及详解(字节豆包 | 232点数解答 | 2025-12-07 12:30:06)75
- Python代码:轻松打印 0 到输入整数之间的所有整数(字节豆包 | 126点数解答 | 2025-12-12 18:21:54)39
- C++实现:输入整数打印 0 到该整数间所有整数及代码详解(字节豆包 | 228点数解答 | 2025-12-12 18:22:24)52
- Python实现:打印0到输入整数间所有整数的代码解析(字节豆包 | 264点数解答 | 2025-12-12 19:37:01)40