C语言—通用链表

思考:当一个项目里要使用多种结构体类型的链表,例如点菜系统里有人员、菜谱两种结构体,若要对这两种链表操作,就需要编写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
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值