当前位置:网站首页>链表之打基础--基本操作(必会)
链表之打基础--基本操作(必会)
2022-08-03 05:30:00 【张遇桥】
链表之打基础–基本操作(必会)
前言:没有系统学过链表的同学过来看看,看看链表的操作,主要是单链表,因为我们以后刷力扣的题目,也主要是单链表的题目,所以,它对我们至关重要哦;如果学过的小伙伴,可以复习复习,毕竟温故而知新,可以为所欲为(bushi)
基本操作:
1.动态申请一个节点
2.单链表打印
3.单链表尾插
4.单链表的头插
5.单链表的尾删
6.单链表头删
7.单链表查找
8.单链表在pos位置之后插入x
9.单链表的销毁
开始了哦
1.动态申请一个节点
SListNode* BuySListNode(SLTDateType x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
exit(-1);
}
newnode->data = x;
return newnode;
}
2.单链表打印
void SListPrint(SListNode* plist)
{
if (plist == NULL)
{
printf("NULL\n");
return;
}
else
{
while (plist)
{
printf("%d->", plist->data);
plist = plist->next;
}
printf("NULL\n");
}
}
3.单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{
SListNode* tail = *pplist;
SListNode* newnode = BuySListNode(x);
newnode->next = NULL;
if (tail == NULL)
{
*pplist = newnode;
}
else
{
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
}
4.单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
SListNode* newnode = BuySListNode(x);
newnode->next = *pplist;
*pplist = newnode;
}
5.单链表的尾删
void SListPopBack(SListNode** pplist)
{
assert(*pplist);
SListNode* tail = *pplist;
SListNode* Pretail = NULL;
if (tail->next == NULL)
{
*pplist = NULL;
return;
}
else
{
while (tail->next)
{
Pretail = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
Pretail->next = NULL;
}
}
6.单链表头删
void SListPopFront(SListNode** pplist)
{
assert(*pplist);
SListNode* front = *pplist;
*pplist = front->next;
free(front);
front = NULL;
}
7.单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
assert(plist);
SListNode* pos = plist;
while (pos && pos->data != x)
{
pos = pos->next;
}
return pos;
}
8.单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
assert(pos);
SListNode* newnode = BuySListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
9.单链表的销毁
void SListDestory(SListNode** pplist)
{
SListNode* node = *pplist;
SListNode* PreNode = NULL;
while (node)
{
PreNode = node->next;
free(node);
node = PreNode;
}
}
好啦,以上就是链表的基础操作啦,希望大家的数据结构更上一层楼!
ps:今天第一次,开车开出外地,一开好几个小时,200多公里的路程,回到家里好累好累,但也很奇怪,开车从不晕车,但不开车就晕车,why?!,一个男生晕车很丢人的啊,怎么搞的啊!!!!
边栏推荐
- sql中 exists的用法
- PCB 多层板为什么都是偶数层?
- pyspark --- 统计多列的众数并一次返回
- 【云原生 · Kubernetes】Kubernetes基础环境搭建
- IDEA连接mysql又报错!Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope
- Redis哨兵模式+过期策略、淘汰策略、读写策略
- SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)
- Content type ‘applicationx-www-form-urlencoded;charset=UTF-8‘ not supported“【已解决】
- Oracle 11g silent install
- 【nohup】nohup命令的简单使用
猜你喜欢
随机推荐
保姆级讲解Transformer
SQLServer2019安装(Windows)
CISP-PTE真题演示
MySQL的 DDL和DML和DQL的基本语法
Redis哨兵模式+过期策略、淘汰策略、读写策略
MySQL的10种常用数据类型
Chrome插件开发入门
mysql 数据去重的三种方式[实战]
异常检测 IsolationForest 返回概率
el-table获取读取数据表中某一行的数据属性
计算机网络高频面试考点
【应届生租房】应届生如何租房以及注意事项
国内首款PCB资料分析软件,华秋DFM使用介绍
MySQL 数据库基础知识(系统化一篇入门)
JUC并发编程深入浅出!
【经验分享】配置用户通过Console口登录设备示例
process.env环境变量配置方式(配置环境变量区分开发环境和生产环境)
empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType),
xshell报错-要继续使用此程序,您必须应用最新的更新或使用新版本
MySQL 操作语句大全(详细)









