论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
ToB企服应用市场:ToB评测及商务社交产业平台
»
论坛
›
软件与程序人生
›
后端开发
›
Java
›
单向顺序链表的创建,增,删,减,查
单向顺序链表的创建,增,删,减,查
美丽的神话
金牌会员
|
2024-5-18 01:19:02
|
显示全部楼层
|
阅读模式
楼主
主题
880
|
帖子
880
|
积分
2640
单向顺序链表的创建,增,删,减,查
/*******************************************************************
*
* file name: 单向顺序链表的创建,增,删,减,查
* author : 17647576169@163.com
* date : 2024-4-22
* function :
* note : None
*
* CopyRight (c) 2024 17647576169@163.com All Right Reseverd
*
* *****************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
// 构造链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct LinkedList
{
DataType_t data; // 结点的数据域
struct LinkedList *next; // 结点的指针域
} LList_t;
/********************************************************************
*
* name : LList_Create
* function : 创建一个空的单向顺序链表,空链表应该有一个头结点,对链表进行初始化
* argument : None
*
* retval : 返回创建的链表的头地址
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
LList_t *LList_Create(void)
{
// 1.创建一个头结点并对头结点申请内存
LList_t *Head = (LList_t *)calloc(1, sizeof(LList_t));
if (NULL == Head)
{
perror("Calloc memory for Head is Failed");
exit(-1);
}
// 2.对头结点进行初始化,头结点是不存储有效内容的!!!
Head->next = NULL;
// 3.把头结点的地址返回即可
return Head;
}
/********************************************************************
*
* name : LList_NewNode
* function : 创建新的结点,并对新结点进行初始化(数据域 + 指针域)
* argument : @data:需插入的数据
*
* retval : 返回新创建链表节点的地址
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
LList_t *LList_NewNode(DataType_t data)
{
// 1.创建一个新结点并对新结点申请内存
LList_t *New = (LList_t *)calloc(1, sizeof(LList_t));
if (NULL == New)
{
perror("Calloc memory for NewNode is Failed");
return NULL;
}
// 2.对新结点的数据域和指针域进行初始化
New->data = data;
New->next = NULL;
return New;
}
/********************************************************************
*
* name : LList_HeadInsert
* function : 向链表的头部进行数据插入
* argument : @head:目标链表
* @data:需插入的数据
* retval : 返回新创建链表节点的地址
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool LList_HeadInsert(LList_t *Head, DataType_t data)
{
// 1.创建新的结点,并对新结点进行初始化
LList_t *New = LList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (NULL == Head->next)
{
Head->next = New;
return true;
}
// 3.如果链表为非空,则把新结点插入到链表的头部
New->next = Head->next;
Head->next = New;
return true;
}
/********************************************************************
*
* name : LList_TailInsert
* function : 向链表的尾部进行数据插入
* argument : @head:目标链表
* @data:需插入的数据
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool LList_TailInsert(LList_t *Head, DataType_t data)
{
// 1.创建新的结点,并对新结点进行初始化
LList_t *New = LList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (NULL == Head->next)
{
Head->next = New;
return true;
}
// 3对链表的头文件的地址进行备份
LList_t *Phead = Head;
// 4遍历链表找到尾部
while (Phead->next)
{
// 把头的直接后继作为新的头结点
Phead = Phead->next;
}
// 5.把新结点插入到链表的尾部
Phead->next = New;
return true;
}
/********************************************************************
*
* name : LList_TailInsert
* function : 向链表的任意位置插入
* argument : @head:目标链表
* @data:需插入的数据
* @dest:插入位置
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool LList_TailInsert(LList_t *Head, DataType_t data, int dest)
{
// 1.创建新的结点,并对新结点进行初始化
LList_t *New = LList_NewNode(data);
if (NULL == New)
{
printf("can not insert new node\n");
return false;
}
// 2.判断链表是否为空,如果为空,则直接插入即可
if (NULL == Head->next)
{
Head->next = New;
return true;
}
if (NULL == Head->next->next)
{
Head->next->next = New;
return true;
}
// 定义两指针备份首节点地址及其后置
LList_t *P1 = Head->next;
LList_t *P2 = P1->next;
// 偏移找的插入位置
for (int i = 0; i < dest - 1; i++)
{
P1 = P1->next;
P2 = P2->next;
}
// 把新结点插入
New->next = P2->next;
P1->next = New;
P2->next = NULL;
free(P2);
return true;
}
/********************************************************************
*
* name : LList_DestInsert
* function : 删除链表中的数据
* argument : @head:目标链表
* @data:需删除的数据
*
* retval : 返回1成功0失败
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
bool LList_DestInsert(LList_t *Head, DataType_t data)
{
// 判断链表是否为空
if (NULL == Head->next)
{
return false;
}
// 要删除的节点在第一个
if (data == Head->next)
{
// 备份首节点地址
LList_t *P = Head;
Head->next = Head->next->next;
P->next = NULL;
free(P);
return true;
}
{
/* code */
}
// 备份头节点
LList_t *P1 = Head->next;
LList_t *P2 = P1->next;
while (P2->next)
{
// 找到data数据所在位置
if (data == P2->data)
{
// 如果data所在节点是为尾节点,如果在则执行删除
if (NULL == P2->next)
{
P1->next = NULL;
free(P2);
return true;
}
// 如果data所在节点不是为尾节点
// P1链接P2的下节点
P1 = P2->next;
// 初始化P2的指针域
P2->next = NULL;
// 释放堆空间
free(P2);
return true;
}
// 偏移
P1 = P1->next;
P2 = P2->next;
}
}
/********************************************************************
*
* name : LList_Print
* function : 遍历链表
* argument : @head:目标链表
*
*
* retval : none
* author : 17647576169@163.com
* date : 2024-4-22
* note :
*
* *****************************************************************/
void LList_Print(LList_t *Head)
{
// 对链表的头文件的地址进行备份
LList_t *Phead = Head;
// 首结点
while (Phead->next)
{
// 把头的直接后继作为新的头结点
Phead = Phead->next;
// 输出头结点的直接后继的数据域
printf("data = %d\n", Phead->data);
}
}
```
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复
使用道具
举报
0 个回复
倒序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
回帖后跳转到最后一页
发新帖
回复
美丽的神话
金牌会员
这个人很懒什么都没写!
楼主热帖
Python 实现贪心算法
Spark快速上手(3)Spark核心编程-RDD转 ...
什么是超融合数据中心网络? ...
java中Files.mismatch方法具有什么功能 ...
Kubernetes——Pod对象的声明周期(Pod ...
Python自动操作 GUI 神器——PyAutoGUI ...
哈工大软件构造Lab3(2022)
C# net core 微信公众号导出历史文章 ...
微服务介绍
彻底理解 volatile 关键字及应用场景, ...
标签云
存储
挺好的
服务器
快速回复
返回顶部
返回列表