代码实现形式
首先我暂时没有做根据值删除,因为我的回调函数比较出现问题,之后在更新
#include #include //链表小节点typedef struct CIRCLElinkNODE{struct CIRCLElinkNODE* next;}CirclelinkNode;//链表结点typedef struct CIRCLElinkLIST{CirclelinkNode head;int size;}CircleList;//编写针对链表结构体操作的API函数#define CIRCLElinklink_TRUE 1;#define CIRCLElinklink_FALSE 0;//遍历函数指针typedef void(*PRINTNODE)(CirclelinkNode *);//比较函数指针typedef int(*COMPARENODE)(CirclelinkNode*, CirclelinkNode*);//初始化链表CircleList *Init_linkList();//插入void Insert_CirclelinkList(CircleList *list,int pos,CirclelinkNode *data);//获得第一个值CirclelinkNode *Front_CirclelinkLint(CircleList *list);//根据位置删除void Remove_CirclelinkList(CircleList *list,int pos);//根据值删除//void Remove_CirclelinkList(CircleList *list,CirclelinkNode *data,COMPARENODE compare);//判断链表是否为空int IsEmpty_CirclelinkList(CircleList *list);//根据值查找//int Find_CirclelinkList(CircleList *list,CirclelinkNode *data,COMPARENODE compare);//根据位置查找int Findvalue_CirclelinkList(CircleList *list,int pos);//返回链表大小int Size_CirclelinkList(CircleList *list);//打印void Print_CirclelinkList(CircleList *list,PRINTNODE print);//释放链表内存void PressSpace_CirclelinkList(CircleList *list);
实现代码
#include "CirclelinkList.h"//初始化链表CircleList *Init_linkList(){CircleList *list=(CircleList *)malloc(sizeof(CircleList));list->size=0;//循环链表,这样可以减少代码量,定义最后一个结点直接指向第一个结点list->head.next=&(list->head);return list;}//插入void Insert_CirclelinkList(CircleList *list,int pos,CirclelinkNode *data){CirclelinkNode *pCurent=&(list->head);if(list==NULL){return;}if(data==NULL){return;}if(pos<0||pos>list->size){pos=list->size;}for(int i=0;inext;}//一定不要操作原指针data->next=pCurent->next;pCurent->next=data;list->size++;}//获得第一个元素CirclelinkNode *Front_CirclelinkLint(CircleList *list){return list->head.next;}//删除void Remove_CirclelinkList(CircleList *list,int pos){if(list==NULL){return;}if(pos<0||pos>list->size){return;}CirclelinkNode *pCurent=&(list->head);for(int i=0;inext;}pCurent->next=pCurent->next->next;list->size--;}//根据值删除//void Remove_CirclelinkList(CircleList *list,CirclelinkNode *data,COMPARENODE compare)//{//if(list==NULL) //{//return;//}//if(data==NULL)//{//return;//}////从值查找,所以我把位置定在next//CirclelinkNode *pPrew=&list->head;//CirclelinkNode *pCurent=list->head.next;//for(int i=0;isize;i++)//{////pCurent=pCurent->next;//if(compare(pCurent,data)==CIRCLElinklink_TRUE)//{//pPrew->next=pCurent->next;//break;//}//pPrew=pCurent;//pCurent=pPrew->next;//}////list->size--;//}//判断链表是否为空int IsEmpty_CirclelinkList(CircleList *list){if(list->size==0){//是空return CIRCLElinklink_TRUE;}else{//非空return CIRCLElinklink_FALSE;}}//根据值查找//int Find_CirclelinkList(CircleList *list,CirclelinkNode *data,COMPARENODE compare)//{//if(list==NULL)//{//return -1;//}//if(data==NULL)//{//return -1;//}//CirclelinkNode *pCurrent=list->head.next;//int flag=-1;//for(int i=0;isize;i++)//{//if(compare(pCurrent,data)==CIRCLElinklink_TRUE)//{//flag=i;//break;//}//pCurrent=pCurrent->next;//}//return flag;//}//根据位置查找int Findvalue_CirclelinkList(CircleList *list,int pos){return 0;}//返回链表大小int Size_CirclelinkList(CircleList *list){return list->size;}//打印void Print_CirclelinkList(CircleList *list,PRINTNODE print){if(list==NULL){return;}CirclelinkNode *pCurrent=list->head.next;//打印俩次,循环链表for(int i=0;isize*3;i++){if(pCurrent==&list->head){printf("---------------------n");pCurrent=pCurrent->next;}print(pCurrent);pCurrent=pCurrent->next;}}//释放链表内存void PressSpace_CirclelinkList(CircleList *list){if(list==NULL){return;}free(list);}
我是采用企业链表格式写的,可以节约代码量