【数据结构】单链表带头双向循环链表的实现

embedded/2024/10/21 21:11:42/

一、链表的概念及结构

1.链表的概念

概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。

2.链表的结构

一般讲的链表包括数据域和指针域:

二、链表的种类

实际中链表的结构非常多样,由以下三组类型自由组合可得8种链表结构:

1.单向、双向:

2.带头、不带头:

3.循环、非循环:

虽然有这么多类型,但我们最长使用的就是以下两种:

在此我们只实现这两种链表

三、单链表的实现

1.自定义链表结点struct SListNode
typedef int SLTDateType;
typedef struct SListNode
{SLTDateType data;struct SListNode* next;
}SListNode;
2.链表打印数据SListPrint
//打印
void SListPrint(SListNode* plist)
{if (plist == NULL)return;while (plist){printf("%d->", plist->data);plist = plist->next;}
}
3.链表创建结点BuyListNode
//创建节点
SListNode* BuySListNode(SLTDateType x)
{SListNode* new_node = (SListNode*)malloc(sizeof(SListNode));if (new_node == NULL){perror("malloc fail");return NULL;}new_node->data = x;new_node->next = NULL;return new_node;
}
4.链表尾部插入数据SListPushBack
//尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{//无节点if (*pplist == NULL){(*pplist) = BuySListNode(x);}//有节点SListNode* tail = *pplist;while (tail->next){tail = tail->next;}tail->next = BuySListNode(x);
}
5.链表头部插入数据SListPushFront
//头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{SListNode* new_node = BuySListNode(x);new_node->next = *pplist;*pplist = new_node;
}
6.链表尾部删除数据SListPopBack
//尾删
void SListPopBack(SListNode** pplist)
{//空链表if ((*pplist) == NULL)return;//一个节点if ((*pplist)->next == NULL){SListNode* tmp = *pplist;free(tmp);tmp = NULL;}//多个节点else{SListNode* cur = *pplist;SListNode* next = cur->next;//找尾while (cur->next->next){cur = next;next = next->next;}free(next);cur->next = NULL;}}
7.链表头部删除数据SListPopFront
//头删
void SLPopFront(SListNode** pplist)
{//没有节点if ((*pplist) == NULL)return;//一个节点//多个节点SListNode* tmp = *pplist;*pplist = (*pplist)->next;free(tmp);tmp = NULL;
}
8.链表查找数据
//查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{SListNode* cur = plist;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}
9.链表在pos位置之后插入数据
// 单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x)
{SListNode* newnode = BuySListNode(x);//在非尾节点插入if (pos->next != NULL){newnode->next = pos->next;pos->next = newnode;}else{SListPushBack(&pos, x);}
}

10.删除pos位置之后的值

// 单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos)
{if (pos->next == NULL){printf("erroe");return;}SListNode* del = pos->next;pos->next = del->next;free(del);del = NULL;
}

11.销毁链表

//销毁链表
void SLTDestroy(SListNode** pphead)
{if (*pphead == NULL)return;SListNode* next = (*(pphead))->next;while (*pphead){//销毁SListNode* del = *pphead;free(del);del = NULL;//迭代*pphead = next;if(next)next = next->next;}
}

四.带头双向循环链表的实现

1.自定义链表结点 ListNode

typedef int LTDataType;
typedef struct ListNode
{LTDataType _data;struct ListNode* _next;struct ListNode* _prev;
}ListNode;

2.创建新节点


//创建新节点
ListNode* BuyLTNode(LTDataType x)
{ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));if (newnode == NULL){perror("malloc fail");return NULL;}newnode->_data = x;newnode->_next = NULL;newnode->_prev = NULL;return newnode;
}

3.创建一个新链表,返回头结点

// 创建返回链表的头结点.
ListNode* ListCreate()
{ListNode* Phead = BuyLTNode(-1);Phead->_next = Phead;Phead->_prev = Phead;return Phead;
}

4.打印链表

//打印链表
void ListPrint(ListNode* pHead)
{if (pHead == NULL)return;ListNode* cur = pHead->_next;printf("pHead <=> ");while (cur!=pHead){printf("%d <=> ", cur->_data);cur = cur->_next;}
}

5.在pos之前插入值为x节点

//在pos前插入
void ListInsert(ListNode* pos, LTDataType x)
{ListNode* pre = pos->_prev;ListNode* newnode = BuyLTNode(x);newnode->_next = pos;pos->_prev = newnode;pre->_next = newnode;newnode->_prev = pre;
}

6.尾插

//尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{ListInsert(pHead,  x);
}

7.头插

//头插
void ListPushFront(ListNode* pHead, LTDataType x)
{ListInsert(pHead->_next, x);
}

8.删除pos位置节点

//删除pos位置的节点
void ListErase(ListNode* pos)
{ListNode* pre = pos->_prev;ListNode* next = pos->_next;free(pos);pos = NULL;pre->_next = next;next->_prev = pre;
}

9.双向链表头删

// 双向链表头删
void ListPopFront(ListNode* pHead)
{ListErase(pHead->_next);
}

10.双向链表尾删

// 双向链表尾删
void ListPopBack(ListNode* pHead)
{ListErase(pHead->_prev);
}

11.查找

// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{ListNode* cur = pHead->_next;while (cur != pHead){if (cur->_data == x)return cur;cur = cur->_next;}return NULL;
}

12.销毁链表

//销毁链表
void ListDestory(ListNode* pHead)
{ListNode* cur = pHead->_next;while (cur){ListNode* next = cur->_next;free(cur);pHead->_next = next;cur = next;}
}

注:带头双向循环链表的头插头删,尾插尾删直接复用了插入删除代码。


http://www.ppmy.cn/embedded/86718.html

相关文章

深入解读 Java 中的 `StringUtils.isNotBlank` 与 `StringUtils.isNotEmpty`

个人名片 🎓作者简介:java领域优质创作者 🌐个人主页:码农阿豪 📞工作室:新空间代码工作室(提供各种软件服务) 💌个人邮箱:[2435024119@qq.com] 📱个人微信:15279484656 🌐个人导航网站:www.forff.top 💡座右铭:总有人要赢。为什么不能是我呢? 专栏导…

Vue3响应式高阶用法之toRaw()

Vue3响应式高阶用法之toRaw() 文章目录 Vue3响应式高阶用法之toRaw()一、简介二、使用场景2.1 性能优化2.2 与外部库的集成 三、基本使用3.1 创建响应式对象3.2 获取原始对象3.3 修改原始对象 四、功能详解4.1 toRaw的工作原理4.2 使用注意事项 五、最佳实践及案例5.1 性能优化…

Highcharts 饼图:数据可视化的魅力

Highcharts 饼图:数据可视化的魅力 引言 在数据可视化的世界中,饼图作为一种经典且直观的图表类型,被广泛应用于各种领域。Highcharts,作为一个功能强大且易于使用的JavaScript图表库,为我们提供了创建精美饼图的便捷途径。本文将深入探讨Highcharts饼图的特点、应用场景…

昇思25天学习打卡营第8天 |昇思MindSpore SSD 目标检测算法 学习与总结

一、引言 SSD&#xff08;Single Shot MultiBox Detector&#xff09;是 Wei Liu 等人在 ECCV 2016 上提出的一种目标检测算法&#xff0c;具有较高的检测精度和速度。 二、算法概述 主流算法类型&#xff1a; Two-stage 方法&#xff1a;如 RCNN 系列&#xff0c;先产生候选…

@Builder注释导致@RequestBody的前端json反序列化失败,HTTP400

项目里发生了一个bug&#xff0c;就是前端请求一个接口时候&#xff0c;报了HTTP 400 Bad Request 通常来说这个问题是前后端的参数没对齐&#xff0c;比如前端传了个String&#xff0c;但后端对应的是Integer。 所以我就排查了半天&#xff0c;结果没发现啥错误&#xff0c;…

HarmonyOS持久化存储数据Preference

Preference首选项 首选项&#xff1a;首选项为应用提供Key-Value键值型的数据处理能力&#xff0c;支持应用持久化轻量级数据&#xff0c;并对其修改和查询。数据存储形式为键值对&#xff0c;键的类型为字符串型&#xff0c;值的存储数据类型包括数字型、字符型、布尔型以及这…

pinia安装及简介

pinia简介 基本特点 轻量级&#xff1a;Pinia相比于传统的Vuex&#xff0c;体积更小&#xff0c;性能更好&#xff0c;只有大约1KB左右。 简化API&#xff1a;Pinia简化了状态管理库的使用方法&#xff0c;抛弃了Vuex中的mutations&#xff0c;只保留了state、getters和actions…

Qt源码交叉编译带openssl的Qt版本

一.背景 近期项目由于对接的后台服务是https的&#xff0c;之前交叉编译的Qt是不带openssl的&#xff0c;为了能支持https&#xff0c;必须要重新编译Qt。 二.环境 环境准备&#xff1a; Ubuntu版本 &#xff1a;18.04&#xff1b; openssl 版本&#xff1a;1.1.1.g&#xff1b…