思考:当一个项目里要使用多种结构体类型的链表,例如点菜系统里有人员、菜谱两种结构体,若要对这两种链表操作,就需要编写2套链表操作代码,对链表的操作有链表创建、添加节点、删除节点、获取链表节点个数、获取链表结点数据、释放链表等,这就会花费我们大量的时间。对于这2种业务,对链表的操作是相同的,那能不能编写一套通用的链表操作函数,这2种应用只要调用相应的操作函数即可?面临这样的问题,所以有了通用链表。
通用链表设计
通用链表结构体:
struct list
{
void *Data; //存放数据的地址
struct list *next;
}
员工信息结构体(Data指向改结构体)
struct staff
{
int iStaffID;
char acName[20];
char acPasswd[10];
}
那么通用链表的数据结构图可以表示为(头节点不存储数据)
通用链表基本操作
初始化链表
void *List_Init(void *data)
{
struct list * head;
head = (struct list *)malloc(sizeof(struct list));
head->data=data;
head->next=NULL;
return head;
}
添加链表节点(添加到链表尾部):
void List_Add(struct list *head,void *data)
{
struct list *pNode,p1=head;
pNode=(struct list *)malloc(sizeof(struct list ));
while(p1->next != NULL )
{
p1=p1->next; //遍历链表,找到最末尾的节点
}
p1->next=pNode;
pNode->data=data;
pNode->next=NULL;
}
获取链表节点个数:
int LIST_Count(struct list * head)
{
struct list * p1;
int nCount = 0;
p1=head->next;
while(p1 != NULL)
{
nCount++;
p1=p1->next;
}
return nCount;
}
获取链表某个节点(返回链表节点的data):
void *LIST_GetNode(struct list * head,int index)
{
struct list * p1;
int nCount = 0;
p1=head;
while(p1->next != NULL)
{
if(nCount == index)
{
return p1->data;
}
p1=p1->next;
nCount++;
}
return NULL;
}
删除链表的某个节点
int List_Del(struct list * head,int index)
{
struct list * p1;
struct list * p2; //要删除结点的临时结点
int nCount = 0;
p1=head;
while(p1->next != NULL)
{
if(nCount == index)
{
p2 = p1->next;
p1->next = p1->next->next;
delete p2;
}
p1=p1->next;
nCount++;
}
return nCount;
}
释放链表
void *List_Free(struct list *head)
{
struct list *ptr=head;
while(ptr!=NULL)
{
ptr=ptr->next;
free(head->data);//先释放数据存储的内存空间
free(head);//再释放链表节点的内存空间
head=ptr;
}
return head;
}
通用链表示例
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
struct list
{
void *Data; //存放数据的地址
struct list *next;
};
struct staff
{
int iStaffID;
char acName[20];
char acPasswd[10];
};
void Staff_Print(struct list *head)
{
struct list *p1=head->next;
struct staff *people1;
while(p1 != NULL)
{
people1=(struct staff*)p1->Data;
printf("%5d%10s%10s\n",people1->iStaffID,
people1->acName,people1->acPasswd);
p1=p1->next;
}
}
void List_Add(struct list *head,void *data)
{
struct list *pNode,*p1=head;
pNode=(struct list *)malloc(sizeof(struct list ));
while(p1->next != NULL )
{ p1=p1->next; } //遍历链表,找到最末尾的节点
p1->next=pNode;
pNode->Data=data;
pNode->next=NULL;
}
void *List_Init(void *data)
{
struct list * head;
head = (struct list *)malloc(sizeof(struct list));
head->Data=data;
head->next=NULL;
return head;
}
void main()
{
struct list *head;
struct staff *people1,*people2;
//初始化链表
head=(struct list *)List_Init(NULL);//头节点不存储数据,参数为NULL
people1=(struct staff *)malloc(sizeof(struct staff));
people2=(struct staff *)malloc(sizeof(struct staff));
people1->iStaffID=1001;
strcpy(people1->acName,"张三");
strcpy(people1->acPasswd,"123456");
people2->iStaffID=1002;
strcpy(people2->acName,"李四");
strcpy(people2->acPasswd,"123456");
//添加链表节点
List_Add(head,people1);
List_Add(head,people2);
//员工信息打印函数
Staff_Print(head);
}
运行结果:
1001 张三 123456
1002 李四 123456