当前位置: 首页 > news >正文

认识单链表

-之前我们学过储存数据的一种表——顺序表,那么为什么还有链表呢
首先我们回顾一下顺序表
顺序表是物理地址连续的一段内存空间(数组),我们通过动态内存开辟的,
那么:在这里插入图片描述
顺序表也有自己的一些优点,比如我们之前做过的一些题,可以通过下标来快速完成,因为他的地址是连续的所以只要利用下标的加减就可以实现

既然顺序表有缺点,那么我们就有了链表。(按需求申请空间)

在这里插入图片描述
在这里插入图片描述
通过插入数据,来理解链表
在这里插入图片描述
在这里插入图片描述

打印函数
在这里插入图片描述

在链表的尾部插入数据

在这里插入图片描述

代码:


SLTPushBack(SLTNode* plist, SLTDataType x)
{//首先要开辟一个结构体来把要插入的数据的内容写进去,在之前的BuySListNode函数就是干这个事情的SLTNode* newnode = BuySListNode(x);SLTNode* tail = plist;while (tail->next != NULL){tail = tail->next;}tail->next = newnode;
}

上述的尾插是建立在之前已经头插了几个节点的情况下的
在这里插入图片描述

那么当链表还是空的时候,这样的尾插还适用吗
在这里插入图片描述
知道了这个问题我们来修改代码:
在这里插入图片描述
那么想要修改plist就要传址,
在这里插入图片描述
尾插总结图
在这里插入图片描述
头插:头插不管什么情况都要挪动plist的,所以也是传址操作,上面已经写到过头插的指针变换了,这里我们直接写代码就行

void TestSList3(){SLTNode* plist = NULL;SLTPushFront(&plist,5);SLTPushFront(&plist, 4);SLTPushFront(&plist, 3);SLTPushFront(&plist, 2);SLTPrin(plist);}void SLTPushFront(SLTNode** head, SLTDataType x)
{SLTNode* newnode = BuySListNode(x);newnode->next = *head;*head = newnode;}

在这里插入图片描述

尾删
在这里插入图片描述
代码:

//尾删SLTPopBack(&plist);SLTPrin(plist);SLTPopBack(&plist);SLTPrin(plist);SLTPopBack(&plist);SLTPrin(plist);SLTPopBack(&plist);SLTPrin(plist);SLTPopBack(&plist);SLTPrin(plist);
void SLTPopBack(SLTNode** head)
{assert(*head);if ((*head)->next == NULL){free(*head);*head = NULL;}else{SLTNode* stail = NULL;SLTNode* tail = *head;while (tail->next!=NULL){stail = tail;tail = tail->next;}free(tail);stail->next = NULL;}
}

在这里插入图片描述

头删
在这里插入图片描述

代码:

SLTPopFront(&plist);SLTPrin(plist);SLTPopFront(&plist);SLTPrin(plist);SLTPopFront(&plist);SLTPrin(plist);SLTPopFront(&plist);SLTPrin(plist);void SLTPopFront(SLTNode** head)
{assert(*head);SLTNode* newnode = (*head)->next;free(*head);*head = newnode;
}

在这里插入图片描述

查找链表中的数的指针,并改变这个指针所指节点的数据

在这里插入图片描述
代码:

//查找链表中的一个值的指针,并且改变他SLTNode* newnode = SLTFind(plist, 3);newnode->data = 20;SLTPrin(plist);
SLTNode* SLTFind(SLTNode* head, SLTDataType x)
{SLTNode* cur = head;while (cur != NULL){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}

在Pos位置插入节点
在这里插入图片描述
代码:

//在指定数据的指针pos位置前插入一个节点SLTNode* pos = SLTFind(plist, 3);//先查找到3所对应的指针posSLTnsert(&plist, pos, 30);SLTPrin(plist);
void SLTnsert(SLTNode** head, SLTNode* pos, SLTDataType x)
{assert(pos);if (pos == *head){SLTPushFront(head, x);//头插}else{SLTNode* prev = *head;while (prev->next != pos){prev = prev->next;//找到pos位置之前的那个节点的指针}SLTNode* newnode= BuySListNode(x);//为要插入的数据创建一个节点prev->next = newnode;newnode->next = pos;}
}

在这里插入图片描述
在pos位置之后插入节点
在这里插入图片描述
代码:

SLTNode* pos = SLTFind(plist, 3);//先查找到3所对应的指针pos/*	SLTnsert(&plist, pos, 30);SLTPrin(plist);*/SLTnsertAfter(plist, pos, 30);SLTPrin(plist);
void SLTnsertAfter(SLTNode* head, SLTNode* pos, SLTDataType x)
{assert(head);SLTNode* newnode = BuySListNode(x);//为要插入的数据创建一个节点newnode->next = pos->next;pos->next = newnode;
}

删除Pos位置的节点
在这里插入图片描述

SLTErase(&plist, pos);pos = NULL;SLTPrin(plist);void SLTErase(SLTNode** head, SLTNode* pos)
{assert(pos);if (pos == *head){SLTPopFront(head);//头删}else{SLTNode* per = *head;while (per->next!=pos){per = per->next;}per->next = pos->next;free(pos);}
}

删除pos位置之后的节点
在这里插入图片描述
代码:

SLTNode* pos = SLTFind(plist, 3);//先查找到3所对应的指针posSLTEraseAfter(pos);SLTPrin(plist);
void SLTEraseAfter(SLTNode* pos)
{assert(pos->next);assert(pos);SLTNode* per = pos->next;pos->next = per->next;free(per);}

在这里插入图片描述释放链表
在这里插入图片描述

http://www.lryc.cn/news/171089.html

相关文章:

  • pytest(二)框架实现一些前后置(固件,夹具)的处理,常用三种
  • 【计算机网络 - 自顶向下方法】计算机网络和因特网
  • 【Java 基础篇】Java Condition 接口详解
  • .360勒索病毒和.halo勒索病毒数据恢复|金蝶、用友、ERP等数据恢复
  • 计算机毕业设计 基于SpringBoot餐厅点餐系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 天空飞鸟 数据集
  • 集成学习-树模型
  • 代码随想录算法训练营第一天(C)| 704. 二分查找 27. 移除元素
  • 重构优化第三方查询接口返回大数据量的分页问题
  • Cento7 Docker安装Zabbix,定制自定义模板
  • 网络防御--防火墙
  • 淘宝商品详情数据采集
  • mac安装virtualenv和virtualenvwrapper
  • 利用PCA科学确定各个指标的权重系数
  • 代码随想录 -- day55 --392.判断子序列 、115.不同的子序列
  • mysql5升级到mysql8的血泪教训
  • Unity 开发人员转CGE(castle Game engine)城堡游戏引擎指导手册
  • 卷运维不如卷网络安全
  • Digger PRO - Voxel enhanced terrains
  • 文字处理工具 word 2019 mac中文版改进功能
  • LeetCode 54. 螺旋矩阵
  • 每天几道Java面试题:集合(第四天)
  • 【论文解读】Faster sorting algorithm
  • latexocr安装过程中遇到的问题解决办法
  • 如何判断linux 文件(或lib)是由uclibc还是glibc编译出来的?
  • WorkPlus | 好用、专业、安全的局域网即时通讯及协同办公平台
  • ARM Linux DIY(十二)NES 游戏
  • MOEA算法的背景知识
  • 【rtp-benchmarks】读取本地文件基于uvgRtp实现多线程发送
  • fire-voc 火光 烟火 火灾 目标检测数据集