嵌入式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--;
}

相关推荐
int型码农1 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT1 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面1 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked931 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,1 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵1 小时前
有效的括号题解
数据结构·算法·
GIS小天2 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
_Itachi__2 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃2 小时前
计算矩阵A和B的乘积
线性代数·算法·矩阵
不爱写代码的玉子2 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#