数据结构之__链表
首先,定义数诀结构listg.h
1 #ifndef listg_h 2 #define listg_h 3 #include <stdlib.h> 4 #include <stdio.h> 5 6 typedef int DataType; 7 8 typedef struct node_{ 9 DataType data; 10 struct node_ *next; 11 } Node; 12 13 typedef struct list_{ 14 Node *head; 15 Node *tail; 16 Node *current; 17 } List; 18 19 void initList(List *); 20 void addHead(List *, DataType); 21 void addTail(List *, DataType); 22 void delNode(List *, DataType); 23 Node *getNode(List *, DataType); 24 int getLength(List *); 25 void dispList(List *); 26 27 #endif
接着,实现头文件定义的函数listg.c
1 #include "listg.h" 2 3 void initList(List *list){ 4 list->head = NULL; 5 list->tail = NULL; 6 list->current = NULL; 7 8 return; 9 } 10 11 void addHead(List *list, DataType data){ 12 //1、创建一个节点 13 Node *node = (Node *)malloc(sizeof(Node)); 14 node->data = data; 15 node->next = NULL; 16 17 //2、插入节点准备 18 if(list->head == NULL){ 19 list->tail = node; 20 }else{ 21 node->next = list->head; 22 //3、插入节点 23 } 24 list->head = node; 25 26 return; 27 } 28 29 void addTail(List *list, DataType data){ 30 //1 31 Node *node = (Node *)malloc(sizeof(Node)); 32 node->data = data; 33 node->next = NULL; 34 35 //2 36 if(list->head == NULL){ 37 list->head = node; 38 }else{ 39 list->tail->next = node; 40 } 41 42 //3 43 list->tail = node; 44 45 return; 46 } 47 48 void delNode(List *list, DataType data){ 49 Node *prev = list->head; 50 Node *node = prev->next; 51 52 while(node != NULL){ 53 if(node->data == data){ 54 prev->next = prev->next->next; 55 }else{ 56 prev->next = node; 57 } 58 } 59 } 60 61 Node *getNode(List *list, DataType data){ 62 Node *node = (Node *)malloc(sizeof(Node)); 63 node = list->head; 64 65 while(node != NULL){ 66 if(data ==node->data){ 67 return node; 68 }else{ 69 node = node->next; 70 } 71 } 72 73 return NULL; 74 } 75 76 int getLength(List *list){ 77 Node *node = (Node*)malloc(sizeof(Node)); 78 node = list->head; 79 int i = 0; 80 while(node != NULL){ 81 node = node->next; 82 i++; 83 } 84 85 return i; 86 } 87 88 void dispList(List *list){ 89 Node *node = (Node *)malloc(sizeof(Node)); 90 node = list->head; 91 int i = 0; 92 while(node != NULL){ 93 printf("the %dth node: %d\n", i + 1, node->data); 94 node = node->next; 95 i++; 96 } 97 printf("display finished\n"); 98 99 return; 100 }
最后,测试文件testListg.c
1 #include "listg.h" 2 3 int main(int argc, char **argv) 4 { 5 List *list = (List *)malloc(sizeof(List)); 6 initList(list); 7 addHead(list, 4); 8 addHead(list, 6); 9 addHead(list, 8); 10 addHead(list, 10); 11 dispList(list); 12 printf("the list: %d\n", getLength(list)); 13 14 return 0; 15 }
测试结果没有什么问题
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-11-02 python3编程基础之一:操作
2019-11-02 python3编程基础之一:量的表示
2019-11-02 python3编程基础之一:标识符
2019-11-02 python3编程基础之一:关键字