/*
********************************************************************************
* 数据结构
*
*
*
* 作者: zhi-z
*
* QQ号: 2282643301
*
********************************************************************************
*文件名 :
*作用 : 基础算法,链表基本操作
*原理 :
*内容 : 添加结点,插入结点到链表指定位置,按关键字在链表中查找内容等
********************************************************************************
*版本 作者 日期 说明
*V0.1 2016-9-24 初始版本
********************************************************************************
*/
#include <string.h>
/********************************************************************************
*函数名 : ChainListAddEnd
*参数 :*head :头指针, data:要添加的数据
*作用 : 添加结点到链表结尾
*内容 :
*
********************************************************************************/
ChainListType *ChainListAddEnd(ChainListType *head,DATA data) //添加结点到链表结尾
{
ChainListType *node,*h;
if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
{
printf("为保存结点数据申请内存失败!\n");
return NULL; //分配内存失败
}
node->data=data; //保存数据
node->next=NULL; //设置结点指针为空,即为表尾
if(head==NULL) //是头指针
{
head=node;
return head;
}
h=head;
while(h->next!=NULL) //查找链表的末尾
h=h->next ;
h->next=node;
return head;
}
/********************************************************************************
*函数名 : ChainListAddFirst
*参数 :*head :头指针, data:要添加的数据
*作用 : 添加结点到链表首部
*内容 :
*
********************************************************************************/
ChainListType *ChainListAddFirst(ChainListType *head,DATA data)
{
ChainListType *node,*h;
if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
{
printf("为保存结点数据申请内存失败!\n");
return NULL; //分配内存失败
}
node->data=data; //保存数据
node->next=head; //指向头指针所指结点
head=node; //头指针指向新增结点
return head;
}
/********************************************************************************
*函数名 : ChainListInsert
*参数 :*head :头指针,findkey:关键点的位置, data:要添加的数据
*作用 : 插入结点到链表指定位置
*内容 :
*
********************************************************************************/
ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data) //插入结点到链表指定位置
{
ChainListType *node,*node1;
if(!(node=(ChainListType *)malloc(sizeof(ChainListType)))) //分配保存结点的内容
{
printf("为保存结点数据申请内存失败!\n");
return 0; //分配内存失败
}
node->data=data; //保存结点中的数据
node1=ChainListFind(head,findkey);//查找节点的位置,返回关键点的指针。
if(node1) //若找到要插入的结点
{
node->next=node1->next; //新插入结点指向关键结点的下一结点
node1->next=node; //设置关键结点指向新插入结点
}else{
free(node);//释放内存
printf("未找到插入位置!\n");
}
return head;//返回头指针
}
/********************************************************************************
*函数名 : ChainListFind
*参数 :*head :头指针,key:关键字
*作用 : 按关键字在链表中查找内容
*内容 :
*
********************************************************************************/
ChainListType *ChainListFind(ChainListType *head,char *key) //按关键字在链表中查找内容
{
ChainListType *h;
h=head; //保存链表头指针
while(h) //若结点有效,则进行查找
{
if(strcmp(h->data.key,key)==0) //若结点关键字与传入关键字相同
return h; //返回该结点指针
h=h->next; //处理下一结点
}
return NULL; //返回空指针
}
/********************************************************************************
*函数名 : ChainListDelete
*参数 :*head :头指针,key:关键字
*作用 : 按关键字删除该节点
*内容 :
*
********************************************************************************/
int ChainListDelete(ChainListType *head,char *key)
{
ChainListType *node,*h; //node保存删除结点的前一结点
node=h=head;
while(h)
{
if(strcmp(h->data.key,key)==0) //找到关键字,执行删除操作
{
node->next=h->next; //使前一结点指向当前结点的下一结点
free(h); //释放内存
return 1;
}else{
node=h; //指向当前结点
h=h->next; //指向下一结点
}
}
return 0;//未删除
}
/********************************************************************************
*函数名 : ChainListLength
*参数 :*head :头指针
*作用 : 获取链表结点数量
*内容 :
*
********************************************************************************/
int ChainListLength(ChainListType *head)//获取链表结点数量
{
ChainListType *h;
int i=0;
h=head;
while(h) //遍历整个链表
{
i++; //累加结点数量
h=h->next;//处理下一结点
}
return i;//返回结点数量
}
数据结构之链表结构(C语言实现)
最新推荐文章于 2023-05-13 13:34:46 发布