自我总结:企业级链表就是相当于给自定义结构体(数据),加上“挂钩“(指针)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Linknode{
struct Linknode *next;
}Linknode;
typedef struct Linklist{
struct Linknode header;
int m_size;
}Linklist;
//链表初始化,创建链表
Linklist * Creat_Linklist()
{
Linklist * list = (Linklist *)malloc(sizeof(Linklist));
list->header.next = NULL;
list->m_size = 0;
return list;
}
//插入数据,根据位置插入
int Insert_Linklist(Linklist * list, int pos, Linknode* data)
{
if (NULL == list || NULL == data)
{
return -1;
}
//位置太小插入到 第一个位置
if (pos < 1)
{
pos = 1;
}
//位置太大插入到 尾部
if (pos>list->m_size)
{
pos = list->m_size + 1;
}
//找到插入位置的前驱结点
Linknode * pre = &list->header;
for (int i = 1; i < pos; i++)
{
pre = pre->next;
}
data->next = pre->next;
pre->next = data;
list->m_size++;
return 1;
}
//删除数据,根据位置删除
int Remove_Linklist(Linklist * list, int pos)
{
if (NULL == list||pos<1||pos>list->m_size)
{
return -1;
}
//找到delete位置的前驱结点
Linknode * pre = &list->header;
for (int i = 1; i < pos; pos++)
{
pre = pre->next;
}
pre->next = pre->next->next;
list->m_size--;
return 1;
}
//查找数据,根据数据内容,返回时链表中的第几个元素,从1开始计数 ,没找到返回-1
//Compare_Function 用户提供 结点元素比较的回调函数
int Find_Linklist(Linklist * list, Linknode* data, int (*compare_function)(Linknode*, Linknode*))
{
if (NULL == list || NULL == data || NULL == compare_function)
{
return -1;
}
Linknode * node = list->header.next;
int flag = 0;//是否找到的标志
int index = 1;
while (node != NULL)
{
if (compare_function(node, data) == 1)
{
flag = 1;
break;
}
node = node->next;
index++;//返回时链表中的第几个元素
}
return flag ? index : -1;
}
//遍历链表
//Foreach_Function 用户提供的遍历链表元素的回调函数
int Foreach_Linklist(Linklist * list, void(*Foreach_Function)(Linknode*))
{
if (NULL == list || NULL == Foreach_Function)
{
return 1;
}
Linknode * node = list->header.next;
while (node != NULL)
{
Foreach_Function(node);
node = node->next;
}
return 1;
}
//清空数据 链表仍然可用
int Clear_Linklist(Linklist * list)
{
if (NULL == list)
{
return -1;
}
list->m_size = 0;
list->header.next= NULL;
return 1;
}
//销毁链表 链表不可用
int Destroy_Linklist(Linklist * list)
{
if (NULL == list)
{
return -1;
}
free(list);
list = NULL;
return 1;
}
Linknode *Find_Link(Linklist * list, int pos)
{
//if (NULL == list)
//{
// return;
//}
//if (pos < 1||pos>list->m_size)
//{
// return;
//}
Linknode * node = &list->header;
for (int i = 1; i < pos; i++)
{
node = node->next;
}
return node->next;
}
/*-----------------------------------------------------------------------------------------*/
//用户使用 企业级链表,只需要在自己定义的类型中将LinkNode 作为第一个成员即可使用
//这样就将 具体的业务和底层链表算法 进行分离了
/*-----------------------------------------------------------------------------------------*/
typedef struct student{
Linknode node;
char name[10];
int age;
}student;
//用户自定义比较函数,回调
int compare_function(Linknode* node1, Linknode* node2)
{
student * stu1 = (student *)node1;
student * stu2 = (student *)node2;
if (stu1->age == stu2->age && (0 == strcmp(stu1->name, stu2->name)))
{
return 1;
}
return 0;
}
//用户自定义遍历函数,其实就是打印
void Foreach_Function(Linknode* node)
{
student * stu = (student *)node;
printf("姓名是:%s 年龄是:%d\n", stu->name, stu->age);
return;
}
int main()
{
student s1, s2, s3, s4, s5;
s1.age = 15;
s2.age = 16;
s3.age = 17;
s4.age = 18;
s5.age = 19;
strcpy(s1.name, "aaa");
strcpy(s2.name, "bbb");
strcpy(s3.name, "ccc");
strcpy(s4.name, "ddd");
strcpy(s5.name, "eee");
Linklist * list = Creat_Linklist();
Insert_Linklist(list, 1, (Linknode*)&s1);
Insert_Linklist(list, 2, (Linknode*)&s2);
Insert_Linklist(list, 3, (Linknode*)&s3);
Insert_Linklist(list, 4, (Linknode*)&s4);
Insert_Linklist(list, 5, (Linknode*)&s5);
printf("插入5条数据后遍历:\n");
Foreach_Linklist(list, Foreach_Function);
//查找
student s6;
s6.age = 19;
strcpy(s6.name, "eee");
int index = Find_Linklist(list, (Linknode*)&s6, compare_function);
printf("查找的元素name=eee,age=15,是链表中第%d个元素\n", index);
Remove_Linklist(list, 1);
printf("删除第1个结点后遍历:\n");
Foreach_Linklist(list, Foreach_Function);
Linknode* node =Find_Link(list, 1);
student * list1 = (student*)node;
printf("%s,%d\n", list1->name, list1->age);
return 0;
}