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

相关推荐
艾莉丝努力练剑29 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
秋说6 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen7 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
liupenglove7 小时前
自动驾驶数据仓库:时间片合并算法。
大数据·数据仓库·算法·elasticsearch·自动驾驶
python_tty8 小时前
排序算法(二):插入排序
算法·排序算法
然我8 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法
risc1234568 小时前
BKD 树(Block KD-Tree)Lucene
java·数据结构·lucene
F_D_Z9 小时前
【EM算法】三硬币模型
算法·机器学习·概率论·em算法·极大似然估计