C语言通用链表的创建及使用

通用链表的设计

通用链表的基本原理:
链表是一种物理 存储单元上非连续、非顺序的存储结构 ,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储 数据元素的数据域,另一个是存储下一个结点地址的指针 域。
那么其结构图就可以为:

在这里插入图片描述

通用链表结构体的创建:

struct Clist
{	
	void *data;  //存放数据的地址 
	struct Clist *pnext;//存放下一个节点
};

该链表的成员信息

struct staff
{
	int ID;//
	char Name[20];
};

通用链表的一些基本操作

表头的创建:

void *List_Init(void *data)
{
	struct Clist * head;
	head = (struct Clist *)malloc(sizeof(struct Clist));
	head->data=data;
	head->pnext=NULL;
	return head;
}

添加新的节点:

void List_Add(struct Clist *head,void *data)
{
	struct Clist *pNode,*p=head;
	pNode=(struct Clist *)malloc(sizeof(struct Clist ));
	while(p->pnext != NULL )
	{  
	 	p=p->pnext; 
	}  
	p->pnext=pNode;
	pNode->data=data;
	pNode->pnext=NULL;
}

显示链表成员的信息:

void Print_data(struct Clist *head)
{
	struct Clist *p=head->pnext;
	struct staff *people;
	while(p != NULL)
	{ 
		people=(struct staff*)p->data;
		printf("%d\t%s\n",people->ID,people->Name);
		p=p->pnext;
	}
}

链表节点个数的获取:

int  Get_listcount(struct Clist * head)
{
	struct Clist * p;
	int count = 0;
	p=head->pnext;
	while(p != NULL)
	{
		count++;
		p=p->pnext;
	}
	return count;
}

通用链表的基本应用

#include<stdio.h>
#include <stdlib.h>
#include <string.h>

int  Get_listcount(struct Clist * head);
void Print_data(struct Clist *head);
void List_Add(struct Clist *head,void *data);
void *List_Init(void *data);


struct Clist
{	
	void *data;  //存放数据的地址 
	struct Clist *pnext;//存放下一个节点
};
struct staff
{
	int ID;
	char Name[20];
};

int main()
{
	int count=0;
	struct Clist *head;
	struct staff *people1,*people2,*people3;
	//初始化链表
	head=(struct Clist *)List_Init(NULL);//头节点不存储数据,参数为NULL

	people1=(struct staff *)malloc(sizeof(struct staff));
	people2=(struct staff *)malloc(sizeof(struct staff));
	people3=(struct staff *)malloc(sizeof(struct staff));

	people1->ID=101;
	strcpy(people1->Name,"二狗");

	people2->ID=102;
	strcpy(people2->Name,"狗蛋");

	people3->ID=103;
	strcpy(people3->Name,"铁蛋");

	//添加链表节点
	List_Add(head,people1);
	List_Add(head,people2);
	List_Add(head,people3);
	//获取链表的节点个数
	count=Get_listcount(head);
	//打印链表的节点个数
	printf("节点个数=%d\n",count);
	//全部成员信息的打印
	Print_data(head);
	
	return 0;
}
/*
函数名:Get_listcount(struct Clist * head)
函数作用:获取链表节点个数
*/
int  Get_listcount(struct Clist * head)
{
	struct Clist * p;
	int count = 0;
	p=head->pnext;
	while(p != NULL)
	{
		count++;
		p=p->pnext;
	}
	return count;
}

/*
函数名:Print_data(struct Clist *head)
函数作用:输出链表的信息
*/
void Print_data(struct Clist *head)
{
	struct Clist *p=head->pnext;
	struct staff *people;
	while(p != NULL)
	{ 
		people=(struct staff*)p->data;
		printf("%d\t%s\n",people->ID,people->Name);
		p=p->pnext;
	}
}
/*
函数名:List_Add(struct list *head,void *data))
函数作用:在最后一个节点添加新的节点
*/
void List_Add(struct Clist *head,void *data)
{
	struct Clist *pNode,*p=head;
	pNode=(struct Clist *)malloc(sizeof(struct Clist ));
	while(p->pnext != NULL )
	{   p=p->pnext; }   //遍历链表,找到最末尾的节点

	p->pnext=pNode;
	pNode->data=data;
	pNode->pnext=NULL;
}
/*
函数名:*List_Init(void *data)
函数作用:链表头的创建
*/
void *List_Init(void *data)
{
	struct Clist * head;
	head = (struct Clist *)malloc(sizeof(struct Clist));
	head->data=data;
	head->pnext=NULL;
	return head;
}

输出结果:
在这里插入图片描述

  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值