当前位置:网站首页>链表之打基础--基本操作(必会)
链表之打基础--基本操作(必会)
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?!,一个男生晕车很丢人的啊,怎么搞的啊!!!!
边栏推荐
- 连续型特征做embedding代码示例
- MySQL的安装教程(嗷嗷详细,包教包会~)
- UniApp scroll-view 事件不生效(@scroll、@scrolltolower、@scrolltoupper ...)
- UniApp 自定义条件编译详细使用流程
- Detailed explanation of AutoInt network and pytorch reproduction
- Use of Alibaba Cloud SMS Service (create, test notes)
- C # program with administrator rights to open by default
- Oracle 数据库集群常用巡检命令
- AQS、CAS、Synchronized小理解
- Chrome 配置samesite=none方式
猜你喜欢
随机推荐
Postman知识汇总
沉铜/黑孔/黑影工艺,PCB该 Pick 哪一种?
Scala 基础 (三):运算符和流程控制
Monitoring Oracle11gR2 in Zabbix6.0 of OracleLinux8.6
SVN客户端安装及操作文档
微信小程序 - 监听 TabBar 切换点击事件
ES 中时间日期类型 “yyyy-MM-dd HHmmss” 的完全避坑指南
Chrome 配置samesite=none方式
UniApp 自定义条件编译详细使用流程
el-tabs(标签栏)的入门学习
UniApp scroll-view 事件不生效(@scroll、@scrolltolower、@scrolltoupper ...)
【OpenStack云平台】搭建openstack云平台
5 个开源的 Rust Web 开发框架,你选择哪个?
单节点部署 gpmall 商城系统(一)
Prometheus monitors container, pod, email alerts
MySQL的 DDL和DML和DQL的基本语法
Nvidia NX使用向日葵远程桌面遇到的问题
WinServer2012r2破解多用户同时远程登录,并取消用户控制
10 common data types in MySQL
【Personal summary】Key points of MES system development/management









