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

相关推荐
牛客企业服务25 分钟前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
糖葫芦君1 小时前
Policy Gradient【强化学习的数学原理】
算法
向阳@向远方3 小时前
第二章 简单程序设计
开发语言·c++·算法
github_czy4 小时前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁4 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子4 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z5 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao5 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx5 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背5 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft