单向链表
1、
LinkList init_LinkLIst()
2、
void insert_LinkList(LinkList list, int pos, void* data)
3、
void foreach_LinkList(LinkList list,void(*myForeach)(void*))
4、
void removeByPos_LinkList(LinkList list, int pos)
5、
void removeByValue_LinkList(LinkList list,void * data,int(*myCompare)(void*,void*))
6、
void clear_LinkList(LinkList list)
7、
void destroy_LinkList(LinkList list)
8、
int size_LinkList(LinkList list)
1、typedef void* LinkList; //这一步是为了防止用户直接操作数据
//在后面使用时,可以转成目标数据结构
struct LList* myList = list;
2、在初始化函数中,要给相应的数据赋初值
3、在插入和删除节点中,不要忘记改变m_size的值
4、在删除节点中,是利用两个辅助指针,来进行链表重新连接
5、注意回调函数的使用
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
struct LinkNode
{
void* data;
struct LinkNode* next;
};
struct LList
{
struct LinkNode pHeader;
int m_size;
};
typedef void* LinkList;
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->next = NULL;
newNode->data = data;
newNode->next = pCurrent->next;
pCurrent->next = newNode;
myList->m_size++;
}
void foreach_LinkList(LinkList list,void(*myForeach)(void*))
{
if (list == NULL)
{
return;
}
struct LList* myList = list;
struct LinkNode* pCurrent = myList->pHeader.next;
for (int i = 0; i < myList->m_size; i++)
{
myForeach(pCurrent->data);
pCurrent = pCurrent->next;
}
}
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);
pDel = NULL;
myList->m_size--;
}
void removeByValue_LinkList(LinkList list,void * data,int(*myCompare)(void*,void*))
{
if (list == NULL)
{
return;
}
if (data == NULL)
{
return;
}
struct LList* myList = list;
struct LinkNode* pPrev = &myList->pHeader;
struct LinkNode* pCurrent = myList->pHeader.next;
for (int i = 0; i < myList->m_size; i++)
{
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* pCrrent = myList->pHeader.next;
for (int i = 0; i < myList->m_size; i++)
{
struct LinkNode* pNext = pCrrent->next;
free(pCrrent);
pCrrent = pNext;
}
myList->pHeader.next = NULL;
myList->m_size = 0;
}
int size_LinkList(LinkList list)
{
if (list == NULL)
{
return -1;
}
struct LList* myList = list;
return myList->m_size;
}
void destroy_LinkList(LinkList list)
{
if (list == NULL)
{
return;
}
clear_LinkList(list);
free(list);
list = NULL;
}
struct Person
{
char name[64];
int age;
};
void myPrintPerson(void* data)
{
struct Person* p = data;
printf("姓名:%s 年龄:%d\n", p->name, p->age);
}
void myComparePerson(void* data1, void* data2)
{
struct Person* p1 = data1;
struct Person* p2 = data2;
return strcmp(p1->name, p2->name) && p1->age == p2->age;
}
void test01()
{
struct Person p1 = { "亚瑟", 18 };
struct Person p2 = { "妲己", 20 };
struct Person p3 = { "安琪拉", 19 };
struct Person p4 = { "凯", 21 };
struct Person p5 = { "孙悟空", 999 };
struct Person p6 = { "李白", 999 };
LinkList mylist = init_LinkLIst();
printf("链表的长度为:%d\n", size_LinkList(mylist));
insert_LinkList(mylist, 0, &p1);
insert_LinkList(mylist, 0, &p2);
insert_LinkList(mylist, -1, &p3);
insert_LinkList(mylist, 0, &p4);
insert_LinkList(mylist, 1, &p5);
insert_LinkList(mylist, 0, &p6);
foreach_LinkList(mylist, myPrintPerson);
printf("链表的长度为:%d\n", size_LinkList(mylist));
removeByPos_LinkList(mylist, 4);
printf("------------------\n");
foreach_LinkList(mylist, myPrintPerson);
printf("链表的长度为:%d\n", size_LinkList(mylist));
struct Person p = { "孙悟空",999 };
removeByValue_LinkList(mylist, &p, myComparePerson);
printf("------------------\n");
foreach_LinkList(mylist, myPrintPerson);
printf("链表的长度为:%d\n", size_LinkList(mylist));
clear_LinkList(mylist);
printf("链表长度为:%d\n", size_LinkList(mylist));
destroy_LinkList(mylist);
mylist = NULL;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
测试截图
|