LinkList.h
#ifndef LINKLIST_H
#define LINKLIST_H#include<stdio.h>
#include<stdlib.h>
//链表结点
typedef struct LINKNODE {struct LINKNODE *next;
}LinkNode;//链表
typedef struct LINKLIST{LinkNode head; //放在第一个,防止有偏移量.//不是指针,所以不需要单独分配内存int size;
}LinkList;
//比较函数指针
typedef int(*COMPARENODE)(LinkNode*, LinkNode*);
//遍历函数指针
typedef void(*PRINTNODE)(LinkNode*);#define COMPARENODE_TRUE 1
#define COMPARENODE_FALSE 0//初始化链表
LinkList* Init_LinkList();//插入
void InsertByPos_LinkList(LinkList *list, int pos,LinkNode *data);//删除
void RemoveByPos_LinkList(LinkList *list, int pos);//查找
int FindByValue_LinkList(LinkList *list, LinkNode *data,COMPARENODE compare);//返回链表大小
int GetSize_LinkList(LinkList *list);//打印
void Print_LinkList(LinkList *list,PRINTNODE print);//释放链表内存
void FreeSpace_LinkList(LinkList *list);#endif
LinkList.c
#include"LinkList.h"
//初始化链表
LinkList* Init_LinkList()
{LinkList* list = (LinkList*)malloc(sizeof(LinkList));list->head.next = NULL;list->size = 0;return list;
}//插入
void InsertByPos_LinkList(LinkList *list, int pos, LinkNode *data)
{if (list == NULL){return;}if (data == NULL){return;}if (pos<0 || pos>=list->size){pos = list->size;}//查找插入位置LinkNode* pCurrent = &(list->head);for (int i = 0; i < pos; i++){pCurrent = pCurrent->next;}//插入新结点data->next = pCurrent->next;pCurrent->next = data;list->size++;
}
//删除
void RemoveByPos_LinkList(LinkList *list, int pos)
{if (list == NULL){return;}if (pos < 0 || pos >= list->size){return;}//辅助指针变量LinkNode* pCurrent = &(list->head);for (int i = 0; i < pos; i++){pCurrent = pCurrent->next;}//删除结点pCurrent->next = pCurrent->next->next;list->size--;}//查找
int FindByValue_LinkList(LinkList *list, LinkNode *data, COMPARENODE compare)
{if (list == NULL){return -1;}if (data == NULL){return -1;}//辅助指针变量LinkNode* pCurrent = list->head.next;int index=0;int flag = -1;while (pCurrent != NULL){if (compare(pCurrent, data) == 0){flag = index;break;}pCurrent= pCurrent->next;index++;}return flag;
}//返回链表大小
int GetSize_LinkList(LinkList *list)
{return list->size;
}//打印
void Print_LinkList(LinkList *list, PRINTNODE print)
{if (list == NULL){return;}//辅助指针变量LinkNode* pCurrent = list->head.next;while (pCurrent != NULL){print(pCurrent);pCurrent = pCurrent->next;}}//释放链表内存
void FreeSpace_LinkList(LinkList *list)
{if (list == NULL){return;}free(list);
}
LinkLIstme.c
#include"LinkList.h"
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
LinkList* Init_LinkList()
{LinkList *list = (LinkList *)malloc(sizeof(LinkList));list->size = 0;list->head.next = NULL;
}void InsertByPos_LinkList(LinkList *list, int pos, LinkNode *data)
{if (list == NULL){return;}if (pos<0 || pos>list->size){pos = list->size;}//辅助指针变量LinkNode* pCurrent = &(list->head);//查找插入结点的前一个结点for (int i = 0; i < pos; i++){pCurrent = pCurrent->next;}data->next = pCurrent->next;pCurrent->next = data;list->size++;
}void RemoveByPos_LinkList(LinkList *list, int pos)
{if (list == NULL){return;}if (pos<0 || pos>list->size){pos = list->size;}//辅助指针变量LinkNode* pCurrent = &(list->head);//查找删除结点的前一个结点for (int i = 0; i < pos; i++){pCurrent = pCurrent->next;}pCurrent->next = pCurrent->next->next;list->size--;
}int FindByValue_LinkList(LinkList *list, LinkNode *data, COMPARENODE compare)
{if (list == NULL){return;}int flag;int i = 0;//辅助指针变量LinkNode* pCurrent = &(list->head);//查找删除结点的前一个结点for (i = 0; i < list->size; i++){while (compare(pCurrent, data) == 0){flag = COMPARENODE_TRUE;break;}pCurrent = pCurrent->next;}return i;
}int GetSize_LinkList(LinkList *list)
{return list->size;
}//不会
void Print_LinkList(LinkList *list, PRINTNODE print)
{}void FreeSpace_LinkList(LinkList *list)
{free(list);
}
企业链表.c
#include"LinkList.h"
#include<string.h>
#include<stdio.h>
#include<stdlib.h>typedef struct PERSON {LinkNode node;char name[64];int age;
}Person;void MyPrint(LinkNode * data)
{Person* p = (Person*)data;printf("Name:%s age:%d \n", p->name, p->age);
}int MyCompare(LinkNode *node1, LinkNode *node2)
{Person* p1 = (Person*)node1;Person* p2 = (Person*)node2;if (strcmp(p1->name, p2->name) == 0 && p1->age == p2->age){return 0;}return -1;
}
int main()
{Person p1, p2, p3, p4, p5;strcpy(p1.name, "aaa");strcpy(p2.name, "bbb");strcpy(p3.name, "ccc");strcpy(p4.name, "ddd");strcpy(p5.name, "eee");p1.age = 10;p2.age = 20;p3.age = 30;p4.age = 40;p5.age = 50;LinkList *list = Init_LinkList();InsertByPos_LinkList(list, 0, (LinkNode *)&p1);InsertByPos_LinkList(list, 0, (LinkNode *)&p2);InsertByPos_LinkList(list, 0, (LinkNode *)&p3);InsertByPos_LinkList(list, 0, (LinkNode *)&p4);InsertByPos_LinkList(list, 0, (LinkNode *)&p5);//打印Print_LinkList(list, MyPrint);Person findP;strcpy(findP.name, "bbb");findP.age = 20;int pos = FindByValue_LinkList(list, (LinkNode*)&findP, MyCompare);printf("位置:%d\n", pos);//释放链表内存FreeSpace_LinkList(list);system("pause");return 0;
}
/运行结果/
/*
Name:eee age:50
Name:ddd age:40
Name:ccc age:30
Name:bbb age:20
Name:aaa age:10
位置:3
请按任意键继续. . .
*/
运行结果: