//本头文件采用中英文两种语言注释,意在大家都能看懂 //This head file consists of the two language of Chinese and English /********************************************************************** ** **--------------------------file infomation---------------------------- **file name:list.h ** **last modified date: ** **descriptioin:对于链表的各种操作------any operation of the list ** **created by:Ethan Wei ***********************************************************************/ /********************************************************************** 头文件------head file **********************************************************************/ #include <stdio.h> #include <stdlib.h> /********************************************************************** 宏定义------macro definition **********************************************************************/ #define Type int //链表元素类型 #define ROW 10 //打印链表是一行的个数 /************************************************************************* 结构体的定义-----------struct definition *************************************************************************/ typedef struct list //单链表数据结构------single list struct { Type data; //链表中的数据 struct list *next; //下一个指针 }_SList; typedef struct ulist //最后定义时的单链表数据结构 { _SList *head; //头节点 _SList *last; //链表的尾指针 int length; //链表的长度 }SList; /************************************************************************* 函数定义--------function definition *************************************************************************/ /************************************************************************* 函数名称:InitList 函数表述: 对单链表进行初始化 输入值: SList * 返回值: 初始化成功返回0,否则退出 *************************************************************************/ int InitList(SList *list) { list->length = 0; //初始化长度为0 list->head = (_SList *)malloc(sizeof(_SList)); if(list->head == NULL) { fprintf(stderr,"内存不足,申请头结点空间失败"); getchar(); exit(1); } list->head->next = NULL; //链表的尾指针置空 list->last = list->head; //尾指针指向头指针 return 0; } /************************************************************************* 函数名称:create 函数表述: 申请一个动态的链表空间 输入值: 返回值: 申请成功返回空间首地址,否则返回NULL *************************************************************************/ _SList* create() { _SList *newList; newList = (_SList*)malloc(sizeof(_SList)); if(newList == NULL) { fprintf(stderr,"申请新空间失败------addtoHead"); return NULL; } return newList; } /************************************************************************* 函数名称:findtoPosition 函数表述: 根据位置找到链表的元素 输入值: SList *,int position 返回值: 查找成功返回地址,否则返回NULL *************************************************************************/ _SList *findtoPosition(SList* list,int position) { _SList *curList; int i; if(position <= 0 ||position >list->length) { puts("查找位置不合法"); return NULL; } curList = list->head->next; //指向第一个元素 for(i = 1;i < position; i++) { curList = curList->next; } return curList; } /************************************************************************* 函数名称:findtoData 函数表述: 根据元素找到位置 输入值: SList *,Type data 返回值: 查找成功打印信息,返回0 *************************************************************************/ int findtoData(SList* list,Type data) { _SList *curList; int i; curList = list->head->next; //指向第一个元素 if(curList == NULL) { puts("链表为空"); return 1; } for(i = 1;i <= list->length;i ++) { if(curList->data == data) { printf("位置:%d 查找元素:%d/n",i,data); } curList = curList->next; } return 0; } /************************************************************************* 函数名称:InsertList 函数表述: 插入链表 输入值: SList *,int position,Type data 返回值: 插入成功返回0,否则返回1 *************************************************************************/ int InsertList(SList *list,int position,Type data) { int i ; _SList *curNode; _SList *newNode; curNode = list->head; //设置当前节点为头节点,往后遍历 if(position > list->length+1 || position <= 0) { printf("插入位置不合法"); return 1; } for(i = 1; i < position; i++) { curNode = curNode->next; } //循环结束,得到要插入的位置前一个节点 newNode = create(); newNode->data = data; if(curNode->next != NULL) { newNode->next = curNode->next; curNode->next = newNode; //将节点链接上 } else if(curNode->next == NULL) { curNode->next = newNode; newNode->next = NULL; list->last = newNode; //更新尾部节点 } list->length += 1; //更新链表长度 return 0; } /************************************************************************* 函数名称:deltoPosition 函数表述: 删除链表,根据位置 输入值: SList *,int position 返回值: 删除成功返回0,否则返回1 *************************************************************************/ int deltoPosition(SList *list,int position) { _SList *curNode; //当前节点指针 _SList *delNode; //要删除的节点指针 int i; if(position > list->length || position <= 0) { printf("删除位置不合法/n"); return 1; } curNode = list->head; for(i = 1;i < position;i ++) { curNode = curNode->next; } //此时得到的节点是要删除节点的前面一个节点 delNode = curNode->next; //此时delNode 就是要删除的节点 if(delNode->next == NULL) //即删除最后一个节点 { curNode->next = NULL; free(delNode); list->last = curNode; //更新最后一个节点 list->length -= 1; //更新链表长度 } else { curNode->next = delNode->next; free(delNode); list->length -= 1; //更新链表长度 } return 0; } /************************************************************************* 函数名称:deltoData 函数表述: 删除链表,根据元素 输入值: SList *,Type data 返回值: 删除成功返回0,否则返回1 *************************************************************************/ int deltoData(SList *list,Type data) { _SList *curList; _SList *delList; int delFlag = 0; //是否删除成功 curList = list->head; //指向第一个元素 if(curList->next == NULL) { puts("链表为空"); return 1; } while(curList->next != NULL) { if(curList->next->data == data) { delList = curList->next; if(delList->next == NULL) //如果删除的节点为最后一个节点 { free(delList); curList->next = NULL; list->length -= 1; //更新链表长度 list->last = curList; //更新尾部节点 delFlag += 1; break; } else { curList->next = delList->next; free(delList); list->length -= 1; //更新链表长度 delFlag += 1; //记录删除次数 } } curList = curList->next; } if(delFlag != 0) { printf("删除成功,共删除%d个元素,删除元素为%d/n",delFlag,data); } return 0; } /************************************************************************* 函数名称:addtoHead 函数表述: 头插入节点 输入值: SList *list,Type data 返回值: 插入成功返回0,否则返回1 *************************************************************************/ int addtoHead(SList *list,Type data) { static char lastFlag = 0; //是否更新尾部节点的标记 _SList *newList; newList = create(); newList->data = data; newList->next = list->head->next; list->head->next = newList; list->length += 1; //更新长度 if(list->head->next != NULL) { lastFlag = 1; //第一次插入不是头插法 } if(lastFlag == 0) { list->last = newList; //更新末尾节点 lastFlag = 1; } return 0; } /************************************************************************* 函数名称:addtoTail 函数表述: 尾插入节点 输入值: SList *list,Type data 返回值: 插入成功返回0,否则返回1 *************************************************************************/ int addtoTail(SList *list,Type data) { _SList *newList; newList = create(); newList->data = data; list->last->next = newList; newList->next = NULL; list->length += 1; //更新长度 list->last = newList; //更新末尾节点 return 0; } /************************************************************************* 函数名称:Traversal 函数表述: 遍历链表 输入值: SList *list 返回值: *************************************************************************/ void Traversal(SList *list) { _SList *curList; int i = 0; curList = list->head; printf("链表长度:%d/n",list->length); printf("链表内容如下:"); for(i = 0;i < list->length;i++) { if(i % ROW == 0) { puts(""); } printf("%d ",curList->next->data); curList = curList->next; } puts(""); } /************************************************************************* 函数名称:destroyList 函数表述: 销毁链表 输入值: SList * 返回值: 删除成功返回0,否则返回1 *************************************************************************/ int destroyList(SList *list) { int nResult; while(list->head->next != NULL) { nResult = deltoPosition(list,1); } return nResult; }
list.h(链表头文件,原创)---希望大家测试一下,找出bug
最新推荐文章于 2025-12-11 12:03:51 发布
本文详细介绍了一个简单的链表数据结构及其核心操作,包括初始化、插入、删除等关键功能,并提供了完整的C语言实现。
1322

被折叠的 条评论
为什么被折叠?



