数据节点定义
struct LinkNode{
struct LinkNode* next;
};
链表结构定义
struct LList{
struct LinkNode head;
int nSize;
};
类型重命名
typedef void* LinkList;
头文件定义
#ifndef _LINKLIST_H
#define _LINKLIST_H
//#include <stdio.h>
//#include "stdlib.h"
typedef void* LinkList;
//初始化链表
LinkList initial_LinkList();
//插入链表元素
void insert_LinkList(LinkList list,int pos,void* data);
//遍历链表元素
void visit_LinkList(LinkList list,void(*MyPrint)(void*));
//根据位置删除链表元素
void delByPos_LinkList(LinkList list,int pos);
//返回元素
void* getItemByPos_LinkList(LinkList list,int pos);
//返回链表大小
int size_LinkList(LinkList list);
//链表逆序
void reverse_LinkList(LinkList list);
//销毁链表
void destroy_LinkList(LinkList list);
#endif
实现文件
#include "stdlib.h"
#include "CLinkList.h"
struct LinkNode{
struct LinkNode* next;
};
struct LList{
struct LinkNode head;
int nSize;
};
LinkList initial_LinkList()
{
struct LList* list = (struct LList*)malloc(sizeof(struct LList));
if (list == NULL)
{
return NULL;
}
list->head.next = NULL;
list->nSize = 0;
return list;
}
void insert_LinkList(LinkList list,int pos,void* data)
{
if (list == NULL)
{
return;
}
if (data == NULL)
{
return;
}
struct LList* listTemp = (struct LList*)list;
if(pos<0||pos>listTemp->nSize)//尾插
{
pos = listTemp->nSize;
}
struct LinkNode* pCurrent = &listTemp->head;
for(int i=0;i<pos;++i)//移动到待插入位置的前一节点
{
pCurrent = pCurrent->next;
}
struct LinkNode* pNewNode = (struct LinkNode*)data;
pNewNode->next = pCurrent->next;
pCurrent->next = pNewNode;
listTemp->nSize++;
}
void visit_LinkList(LinkList list,void(*MyPrint)(void*))
{
if (list == NULL)
{
return;
}
if (MyPrint == NULL)
{
return;
}
struct LList* listTemp = (struct LList*)list;
struct LinkNode* pCurrent = &listTemp->head;
for(int i=0;i<listTemp->nSize;++i)
{
pCurrent = pCurrent->next;
MyPrint(pCurrent);
}
}
void delByPos_LinkList(LinkList list,int pos)
{
if (list == NULL)
{
return;
}
struct LList* listTemp = (struct LList*)list;
if (pos<0||pos>listTemp->nSize)
{
return;
}
struct LinkNode* pPre = &listTemp->head;
for(int i=0;i<pos;++i)
{
pPre = pPre->next;
}
pPre->next = pPre->next->next;
listTemp->nSize--;
}
//返回元素
void* getItemByPos_LinkList(LinkList list,int pos)
{
if (list == NULL)
{
return NULL;
}
struct LList* listTemp = (struct LList*)list;
if (pos<0||pos>listTemp->nSize)
{
return NULL;
}
struct LinkNode* pPCurrent = &listTemp->head;
for(int i=0;i<pos;++i)
{
pPCurrent = pPCurrent->next;
}
return pPCurrent->next;
}
int size_LinkList(LinkList list)
{
if (list == NULL)
{
return 0;
}
struct LList* listTemp = (struct LList*)list;
return listTemp->nSize;
}
//递归实现链表翻转
struct LinkNode* reverse(struct LinkNode* lNode)
{
if(lNode->next == NULL)
{
return lNode;
}
struct LinkNode* nodeTemp = reverse(lNode->next);
lNode->next->next = lNode;
lNode->next = NULL;
return nodeTemp;
}
//链表逆序
void reverse_LinkList(LinkList list)
{
if(list == NULL)
{
return;
}
struct LList* plist = (struct LList*)list;
struct LinkNode* node = reverse(plist->head.next);
plist->head.next = node;
}
void destroy_LinkList(LinkList list)
{
if (list == NULL)
{
return;
}
free(list);
list = NULL;
}
测试代码
struct Person
{
void* data;
char name[64];
int nAge;
};
void MyPrint(void* data)
{
Person* person = (Person*)data;
printf("name:%s,age:%d\n",person->name,person->nAge);
}
struct Person p1 = {NULL,"张三",12};
struct Person p2 = {NULL,"李四",15};
struct Person p3 = {NULL,"王麻子",18};
struct Person p4 = {NULL,"张五",19};
struct Person p5 = {NULL,"张飞",22};
//linkList
LinkList list = initial_LinkList();
insert_LinkList(list,0,&p1);
insert_LinkList(list,-1,&p2);
insert_LinkList(list,4,&p3);
insert_LinkList(list,2,&p4);
insert_LinkList(list,1,&p5);
visit_LinkList(list,MyPrint);
printf("List size:%d\n",size_LinkList(list));
delByPos_LinkList(list,3);
visit_LinkList(list,MyPrint);
printf("List size:%d\n",size_LinkList(list));
reverse_LinkList(list);
visit_LinkList(list,MyPrint);
printf("List Reversed.\n");
destroy_LinkList(list);
测试结果