实现一个链表库,用户提供结构体地址和回调函数,实现在输入时的排序以及删除,添加,查找。
编译环境:vs2019,centos7gcc
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//相关头文件
初始化
//next域指向下一个,data域指向本地
struct linknode
{
struct linknode* next;
void* data;
};
struct linklist
{
struct linknode pheader;
int(*comp)(void* node1, void* node2);
int size;
};
//初始化
struct linklist* firstlist(int(*comp)(void* node1, void* node2))
{
struct linklist* mylist = (struct linklist*)malloc(sizeof(struct linklist));
mylist->pheader.next = NULL;
mylist->pheader.data = NULL;
mylist->comp = comp;
mylist->size = 0;
return mylist;
}
添加,不用考虑前插后插,用户只考虑输入,输入后自动排序
//添加
void addlist(struct linklist* mylist, void* data)
{
if (mylist == NULL || data == NULL)
{
return ;
}
struct linknode* node = (struct linknode*)malloc(sizeof(struct linknode));
struct linknode* temp = &mylist->pheader;
if (temp->next == NULL)
{
temp->next = node;
node->data = data;
node->next = NULL;
mylist->size++;
return;
}
while (1)
{
//temp = temp->next;
if (temp->next == NULL)
{
node->data = data;
node->next = temp->next;
temp->next = node;
mylist->size++;
return;
}
else if (mylist->comp(temp->next->data, data) > 0)
{
node->data = data;
node->next = temp->next;
temp->next = node;
mylist->size++;
return;
}
temp = temp->next;
}
遍历,查找,删除,删除只是删除相关节点,不进行置NULL
//遍历
void scanlist(struct linklist* mylist, void(*myprintf)(struct linknode*))
{
if (mylist == NULL || myprintf == NULL)
{
return;
}
struct linknode* temp = NULL;
temp = mylist->pheader.next;
while (temp)
{
myprintf(temp->data);
temp = temp->next;
}
return;
}
//查找
void* find(struct linklist* mylist, void* address)
{
if (mylist == NULL)
{
return;
}
struct linknode* temp = &mylist->pheader;
while (temp->next)
{
int(*comp)(void* node1,void* node2)=comp;
if (comp(temp->next->data, address) == 0)
{
return temp->next->data;
}
temp = temp->next;
}
}
//删除
void dele(struct linklist* mylist, void* data)
{
struct linknode* temp = NULL;
if (mylist == NULL)
{
return;
}
temp =&mylist->pheader;
while (temp->next)
{
if (temp->next->data == find(mylist, data))
{
temp->next = temp->next->next;
mylist->size--;
return;
}
temp = temp->next;
}
//temp = (struct linknode*)find(mylist, data);
}
用户自定义函数
typedef struct Student
{
char name[15];
int age;
}Student;
//打印回调函数
void myprintf(void* node)
{
if (node == NULL)
{
return;
}
Student* stu = (Student*)node;
printf("姓名%s年龄%d\n", stu->name, stu->age);
return;
}
//比较回调函数
int comp(void* node1, void* node2)
{
if (node1 == NULL || node2 == NULL)
{
return NULL;
}
Student* stu1 = NULL;
Student* stu2 = NULL;
stu1 = (Student*)node1;
stu2 = (Student*)node2;
if (stu1->age > stu2->age)
{
return 1;
}
if (stu1->age < stu2->age)
{
return -1;
}
if (strcmp(stu1->name, stu2->name) > 0)
{
return 1;
}
if (strcmp(stu1->name, stu2->name) < 0)
{
return -1;
}
return 0;
}
主函数
int main()
{
//自定义
Student s1, s2, s3, s4, s5;
s1.age = 11;
s2.age = 16;
s3.age = 13;
s4.age = 14;
s5.age = 10;
strcpy(s1.name, "xiaoming");
strcpy(s2.name, "xiaofang");
strcpy(s3.name, "xiaohong");
strcpy(s4.name, "xiaowang");
strcpy(s5.name, "xiaohuang");
//创建链表
struct linklist* mylist = firstlist(comp);
//插入
addlist(mylist, (void*)&s1);
addlist(mylist, (void*)&s2);
addlist(mylist, (void*)&s3);
addlist(mylist, (void*)&s4);
addlist(mylist, (void*)&s5);
scanlist(mylist, myprintf);
Student s6;
s6.age = 11;
strcpy(s6.name, "xiaoming");
//s y(s6.name, "xiaohuang");myprintf((void*)(find(mylist, (void*)&s6)));
dele(mylist, (void*)&s6);
scanlist(mylist, myprintf);
return 0;
}
gcc编译结果