linklist链表
写底层库不能有warning告警
LinkList.h
#ifndef _MYLINKLIST_H_ #define _MYLINKLIST_H_ typedef void LinkList; /* typedef struct _tag_LinkListNode LinkListNode; struct _tag_LinkListNode { LinkListNode* next; }; */ // create destroy clear length // insert delete get typedef struct _tag_LinkListNode { struct _tag_LinkListNode* next; }LinkListNode;//这个是业务节点,这里是嵌套定义 LinkList* LinkList_Create(); void LinkList_Destroy(LinkList* list); void LinkList_Clear(LinkList* list); int LinkList_Length(LinkList* list); int LinkList_Insert(LinkList* list, LinkListNode* node, int pos); LinkListNode* LinkList_Get(LinkList* list, int pos); LinkListNode* LinkList_Delete(LinkList* list, int pos); #endif
LinkList.c
#include "stdlib.h" #include "stdio.h" #include "string.h" #include "linklist.h" typedef struct _tag_LinkList { LinkListNode header; int length; }TLinkList;//这个是头节点 LinkList* LinkList_Create() { TLinkList *tList = (TLinkList *)malloc(sizeof(TLinkList)); if (tList == NULL) { return NULL; } tList->header.next = NULL; tList->length = 0; return tList; } void LinkList_Destroy(LinkList* list) { if (list != NULL) { free(list); } return ; } void LinkList_Clear(LinkList* list) { TLinkList *tList = list; if (tList == NULL) { return ; } tList->length = 0; tList->header.next = NULL; return ; } int LinkList_Length(LinkList* list) { TLinkList *tList = list; if (tList == NULL) { return -1; } return tList->length; } int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) { int i = 0; TLinkList *tList = (TLinkList *)list; LinkListNode *current = NULL; if (tList == NULL || node == NULL || pos<0) { return -1; } current = &tList->header; //current = (LinkListNode *)list; for (i=0; (i<pos)&¤t->next!=NULL; i++ ) { current = current->next; } //新节点链接后续链表 node->next = current->next; //前面链表链接node current->next = node; tList->length ++; return 0; } LinkListNode* LinkList_Get(LinkList* list, int pos) { int i = 0; TLinkList *tList = (TLinkList *)list; LinkListNode *current = NULL; LinkListNode *ret = NULL; if (list==NULL || pos<0 || pos>=tList->length) { return NULL; } current = &tList->header; for (i=0; i<pos; i++) { current = current->next; // 0 -1 } ret = current->next; return ret; } LinkListNode* LinkList_Delete(LinkList* list, int pos) { int i = 0; TLinkList *tList = (TLinkList *)list; LinkListNode *current = NULL; LinkListNode *ret = NULL; if (list==NULL || pos<0 || pos>=tList->length) { return NULL; } //没有初始化环境 current = &tList->header; for (i=0; i<pos; i++) { current = current->next; } ret = current->next; current->next = ret->next; tList->length --; return ret; }
主叫函数
上面在进行强制类型转换时,就会自动将teacher结构体里面的LinkLIstNode的地址取出来
当取出业务节点的时候,强制类型转换为Teacher,从而可以得到这个业务节点其它数据域,其实就是强制类型转换决定了,这个地址内存块的长度,也就是类型决定长度
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗