LinkList.h
#ifndef LINKLIST_H
#define LINKLIST_H
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<string.h>//链表结点
typedef struct LINKNODE {void* data; //无类型指针,指向任何类型的数据struct LINKLIST* next;
}LinkNode;//链表
typedef struct LINKLIST {LinkNode* head;int size;//不需要容量
}LinkList;//打印函数指针
typedef void(*PRINTLINKNODE)(void*);//初始化链表
LinkList* Init_LinkList();//指定位置插入
void Insert_LinkList(LinkList *list,int pos,void* data);//删除指定位置的值
void Remove_LinkList(LinkList *list, int pos);//获得链表的长度
int Size_LinkList(LinkList *list);//查找
int Find_linkList(LinkList *list, void* data);//返回第一个结点
void* GetFront_LinkList(LinkList *list);//打印链表结点
void Print_LinkList(LinkList *list, PRINTLINKNODE print);//释放链表内存
void Free_LinkList(LinkList *list);#endif
LinkList.c
#include"LinkList.h"//初始化链表
LinkList* Init_LinkList()
{LinkList* list = (LinkList*)malloc(sizeof(LinkList));list->size = 0;//头结点 是不保存数据信息的list->head = (LinkNode*)malloc(sizeof(LinkNode));list->head->data = NULL;list->head->next = NULL;return list;
}//指定位置插入
void Insert_LinkList(LinkList *list, int pos, void* data)
{if (list == NULL){return;}if (data == NULL){return;}//友好处理if (pos<0 || pos>list->size){pos = list->size;}//创建新的结点LinkNode* newnode = (LinkNode*)malloc(sizeof(LinkNode));newnode->data = data;newnode ->next= NULL;//找结点//辅助指针变量LinkNode *pCurrent = list->head;for (int i = 0; i < pos; i++){pCurrent = pCurrent->next;}//新结点插入newnode->next = pCurrent->next;pCurrent->next = newnode;list->size++;
}//删除指定位置的值
void Remove_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;}//缓存删除的结点LinkNode* pDel = pCurrent->next;pCurrent->next = pDel->next;//释放删除结点的内存free(pDel);list->size--;}//获得链表的长度
int Size_LinkList(LinkList *list)
{return list->size;
}//查找
int Find_linkList(LinkList *list, void* data)
{if (list == NULL){return -1;}if (data == NULL){return -1;}//遍历查找LinkNode* pCurrent = list->head->next;//指向第一个有效数字int i = 0;while (pCurrent!=NULL){if (pCurrent->data == data){break;}i++;pCurrent = pCurrent->next;}return i;}//返回第一个结点
void* GetFront_LinkList(LinkList *list)
{LinkNode* node = list->head->next;return node->data;
}//打印链表结点
void Print_LinkList(LinkList *list, PRINTLINKNODE print)
{if (list == NULL){return;}//辅助指针变量LinkNode *pCurrent = list->head->next;while (pCurrent != NULL){print(pCurrent->data); //注意顺序pCurrent = pCurrent->next;}
}//释放链表内存
void Free_LinkList(LinkList *list)
{if (list == NULL){return;}//辅助指针变量LinkNode* pCurrent = list->head;while (pCurrent->next != NULL){//先缓存下一个结点,再删除LinkNode* pNext = pCurrent->next;free(pCurrent);pCurrent = pNext;}//释放链表内存list->size = 0;free(list);
}
单项链表.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"LinkList.h"typedef struct PERSON {char name[64];int age;int score;} Person;
void MyPrint(void* data) {Person* p = (Person*)data;printf("Name:%s Age %d Score %d\n",p->name, p->age, p->score);
}int main()
{//创建链表LinkList* list = Init_LinkList();//创建数据Person p1 = { "aaa", 18, 100 };Person p2 = { "bbb", 19, 99 };Person p3 = { "ccc", 8, 45 };Person p4 = { "ddd", 27, 22 };Person p5 = { "eee", 34, 12 };//数据插入链表Insert_LinkList(list, 0, &p1);Insert_LinkList(list, 0, &p2);Insert_LinkList(list, 0, &p3);Insert_LinkList(list, 0, &p4);Insert_LinkList(list, 0, &p5);//打印Print_LinkList(list,MyPrint);//删除p2Remove_LinkList(list, 3);//打印printf("删除第四个后打印结果:\n");Print_LinkList(list, MyPrint);//返回第一个结点printf("-------------查找结果返回第一个结点--------------\n");Person* p = (Person*)GetFront_LinkList(list);printf("Name:%s Age %d Score %d\n", p->name, p->age, p->score);//销毁列表Free_LinkList(list);system("pause");return 0;
}
///打印结果///
/*
Name:eee Age 34 Score 12
Name:ddd Age 27 Score 22
Name:ccc Age 8 Score 45
Name:bbb Age 19 Score 99
Name:aaa Age 18 Score 100
删除第四个后打印结果:
Name:eee Age 34 Score 12
Name:ddd Age 27 Score 22
Name:ccc Age 8 Score 45
Name:aaa Age 18 Score 100
-------------查找结果返回第一个结点--------------
Name:eee Age 34 Score 12
请按任意键继续. . .
*/
运行结果截图: