嵌入式3-14

1、整理思维导图

2、重写链表的代码

3、实现链表,按值查找返回位置的功能,按位置查找返回值,释放单链表,链表逆置

node_p create_link_list()//创建头结点
{
node_p p=(node_p)malloc(sizeof(node));
if(p==NULL)
{
printf("空间申请失败\n");
return NULL;
}
p->len=0;
p->next==NULL;
printf("申请成功\n");
return p;
}
node_p create_node(datatype data)//申请新节点
{
node_p p=(node_p)malloc(sizeof(node));
if(p==NULL)
{
printf("空间申请失败\n");
return NULL;
}
p->data=data;
p->next=NULL;
return p;
}
int insert_head(node_p p,datatype data)//头插
{
if(p==NULL)
{
printf("链表不合法\n");
return 0;
}
node_p q=create_node(data);
if(q==NULL)
{
printf("空间申请失败\n");
return 0;
}
q->next=p->next;
p->next=q;
p->len++;
return 1;
}
int empty_link(node_p p)//判空
{
if(p==NULL)
{
printf("链表不合法\n");
return -1;
}
return p->next==NULL?1:0;
}
void dele_head(node_p p)//头删
{
if(p==NULL)
{
printf("链表不合法\n");
return;
}
if(empty_link(p))
return;
node_p q=p->next;
p->next=q->next;
free(q);
q=NULL;
p->len--;
}
void show_link(node_p p)//输出
{
if(p==NULL||p->len==0)
{
printf("错误\n");
return;
}
node_p q=p->next;
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
puts("NULL");
}
void insert_tail(node_p p,datatype data)//尾插
{
if(p==NULL)
{
return;
}
node_p q=p->next;
while(q->next!=NULL)
{
q=q->next;
}
q->next=create_node(data);
q->next->next=NULL;
p->len++;
}
void dele_tail(node_p p)//尾删
{
if(p==NULL)
{
return;
}
node_p q=p->next;
while(q->next->next!=NULL)
{
q=q->next;
}
free(q->next);
q->next=NULL;
p->len--;
}
void insert_pos(node_p p,datatype data,int pos)//插入
{
if(p==NULL)
return;
node_p q=p;
while(pos)
{
q=q->next;
pos--;
}
node_p r=create_node(data);
if(r==NULL)
return;
r->next=q->next;
q->next=r;
p->len++;
}
void dele_pos(node_p p,int pos)//删除
{
if(p==NULL)
return;
node_p q=p;
for(int i=0;i<pos;i++)
{
q=q->next;
}
node_p del=q->next;
q->next=q->next->next;
free(del);
del=NULL;
p->len--;
}

相关推荐
_深海凉_9 分钟前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
旖-旎1 小时前
深搜练习(电话号码字母组合)(3)
c++·算法·力扣·深度优先遍历
谭欣辰1 小时前
C++快速幂完整实战讲解
算法·决策树·机器学习
Mr_pyx1 小时前
【LeetHOT100】随机链表的复制——Java多解法详解
算法·深度优先
AIFarmer1 小时前
【无标题】
开发语言·c++·算法
AGV算法笔记2 小时前
CVPR 2025 最新感知算法解读:GaussianLSS 如何用 Gaussian Splatting 重构 BEV 表示?
算法·重构·自动驾驶·3d视觉·感知算法·多视角视觉
勤劳的进取家3 小时前
数据链路层基础
网络·学习·算法
Advancer-3 小时前
第二次蓝桥杯总结(上)
java·算法·职场和发展·蓝桥杯
ん贤3 小时前
加密算法(对称、非对称、哈希、签名...)
算法·哈希算法
superior tigre4 小时前
78 子集
算法·leetcode·深度优先·回溯