资讯动态

C语言基础-单链表

发布时间:2026/8/21 22:13:23 来源:尧图企业网站定制
#// 动态创建一个链表动态申请内存模块化设计### 什么是链表//1.创建链表创建一个表头代表整个链表结构体指针-----通过动态内存申请转变成-----》结构体变量struct Node* createList(){ struct Node* headNode (struct Node*)malloc(sizeof(struct Node)); // headNode变成结构体变量 // 变量使用前要初始化 // headNode-data 1; headNode-next NULL; return headNode; };//2.创建节点struct Node* createNode(int data){ struct Node* newNode (struct Node*)malloc(sizeof(struct Node)); newNode-data data; newNode-next NULL; return newNode; };//3.从头插入节点从尾部插入、指定位置插入// 插入节点插入函数:参数是插入那个链表插入的数据是多少 void insertNodeByHead(struct Node* headNode, int data){ struct Node* newCode createNode(data); newCode-next headNode-next; // 为啥顺序反了就不行 headNode-next newCode; }//4.删除节点指定结点删除//5.打印遍历节点测试用// 打印遍历节点 void printList(struct Node* headNode) { struct Node* pMove headNode-next; while(pMove){ printf(%d \t,pMove-data); pMove pMove-next; } printf(链表节点遍历完成\n); }

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价