LinkNode.h
#ifndef _LINKNODE_H_
#define _LINKNODE_H_
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//链表结点
struct LinkNode
{
void * data; //数据域
struct LinkNode * next; //指针域
};
//链表结构体
struct LList
{
struct LinkNode pHeader; //头节点
int m_Size; //链表长度
};
typedef void * LinkList;
//初始化链表
LinkList init_LinkList();
//插入结点
void insert_LinkList(LinkList list, int pos, void * data);
//遍历链表
void foreach_LinkList(LinkList list, void(*myPrint)(void *));
//删除节点
void removeByPos_LinkList(LinkList list, int pos);//按位置删除
void removeByValue_LinkList(LinkList list, void *data, int(*myCompare)(void *, void*));//按数据删除
//清空链表
void clear_LinkList(LinkList list);
//链表长度
int size_LinkList(LinkList list);
//销毁链表
void free_LinkList(LinkList list);
struct Person
{
char name[64];
int age;
};
void myPrintPerson(void * data);
int myComparePerson(void *data1, void *data2);
#endif
LinkNode.c
#include "LinkNode.h"
//初始化链表
LinkList init_LinkList()
{
struct LList * myList = malloc(sizeof(struct LList));
if (myList == NULL)
{
return NULL;
}
//初始化链表结构体
myList->pHeader.data = NULL;
myList->pHeader.next = NULL;
myList->m_Size = 0;
return myList;
}
//插入结点
void insert_LinkList(LinkList list, int pos, void * data)
{
if (list == NULL)
{
return;
}
if (data == NULL)
{
return;
}
struct LList * myList = list;
if (pos < 0 || pos >myList->m_Size)
{
//无效位置进行尾插
pos = myList->m_Size;
}
//创建临时节点
struct LinkNode * pCurrent = &myList->pHeader;
//通过循环 找到 插入位置的前驱节点
for (int i = 0; i < pos; i++)
{
pCurrent = pCurrent->next;
}
//创建出新节点
struct LinkNode * newNode = malloc(sizeof(struct LinkNode));
newNode->data = data;
newNode->next = NULL;
//将新节点 插入到 链表中
newNode->next = pCurrent->next;
pCurrent->next = newNode;
myList->m_Size++; //更新链表的长度
}
//遍历链表
void foreach_LinkList(LinkList list, void(*myPrint)(void *))
{
if (list == NULL)
{
return;
}
struct LList * myList = list;
struct LinkNode * pCurrent = myList->pHeader.next; //找到第一个有数据的节点
for (int i = 0; i < myList->m_Size; i++)
{
myPrint(pCurrent->data);
pCurrent = pCurrent->next;
}
return;
}
//删除节点
void removeByPos_LinkList(LinkList list, int pos)//按照指定位置删除
{
if (list == NULL)
return;
struct LList *mylist = list;
if (pos < 0 || pos > mylist->m_Size - 1)
return;
//找到删除节点的前驱节点
struct LinkNode *pCurrent = &mylist->pHeader;
for (int i = 0; i < pos; ++i)
{
pCurrent = pCurrent->next;
}
//缓存中 待删除的节点
struct LinkNode *pDel = pCurrent->next;
//建立关系
pCurrent->next = pDel->next;
//释放待删除节点
free(pDel);
//更新长度
mylist->m_Size--;
return;
}
void removeByValue_LinkList(LinkList list, void *data, int(*myCompare)(void *, void*))//按照指定数据删除
{
if (list == NULL || data == NULL)
return;
struct LList * mylist = list;
//创建辅助指针
struct LinkNode *pPrev = &mylist->pHeader;
struct LinkNode *pCurrent = pPrev->next;
for (int i = 0; i < mylist->m_Size; ++i)
{
//if (pCurrent->data == data)//交给用户比对
if (myCompare(pCurrent->data, data))
{
//更改指针指向
pPrev->next = pCurrent->next;
//释放待删除节点
free(pCurrent);
pCurrent = NULL;
//更新长度
mylist->m_Size--;
break;
}
pPrev = pCurrent;
pCurrent = pCurrent->next;
}
}
//清空链表
void clear_LinkList(LinkList list)
{
if (list == NULL)
return;
struct LList *mylist = list;
struct LinkNode *pCurrent = mylist->pHeader.next;
for (int i = 0; i < mylist->m_Size; ++i)
{
//先记住待删除节点的后一个节点
struct LinkNode *pNext = pCurrent->next;
free(pCurrent);
pCurrent = pNext;
}
mylist->pHeader.next = NULL;
mylist->m_Size = 0;
return;
}
//链表长度
int size_LinkList(LinkList list)
{
if (list == NULL)
return -1;
struct LList *mylist = list;
return mylist->m_Size;
}
//销毁链表
void free_LinkList(LinkList list)
{
if (list == NULL)
return;
clear_LinkList(list);
free(list);
list = NULL;
return;
}
void myPrintPerson(void * data)
{
struct Person * p = data;
printf("姓名: %s 年龄: %d\n", p->name, p->age);
return;
}
int myComparePerson(void *data1, void *data2)
{
struct Person *p1 = data1;
struct Person *p2 = data2;
return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age;
}
main.c
#include "LinkNode.h"
void test01()
{
//初始化链表
LinkList list = init_LinkList();
//插入数据
struct Person p1 = { "亚瑟", 18 };
struct Person p2 = { "王昭君", 28 };
struct Person p3 = { "赵云", 38 };
struct Person p4 = { "张飞", 19 };
struct Person p5 = { "关羽", 20 };
struct Person p6 = { "宫本", 17 };
insert_LinkList(list, 0, &p1);
insert_LinkList(list, 0, &p2);
insert_LinkList(list, 1, &p3);
insert_LinkList(list, 0, &p4);
insert_LinkList(list, 1, &p5);
insert_LinkList(list, 100, &p6);
foreach_LinkList(list, myPrintPerson);
//删除数据后
printf("删除数据后:\n");
removeByPos_LinkList(list, 2);
foreach_LinkList(list, myPrintPerson);
//删除数据后
printf("删除数据后:\n");
removeByValue_LinkList(list, &p6, myComparePerson);
foreach_LinkList(list, myPrintPerson);
//数据清空后
printf("链表长度:%d\n", size_LinkList(list));
printf("数据清空后:\n");
clear_LinkList(list);
printf("链表长度:%d\n", size_LinkList(list));
//销毁链表
free_LinkList(list);
list = NULL;
return;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}