当前位置: 首页 > backend >正文

学习c语言的链表的概念、操作(另一篇链表的笔记在其他的栏目先看这个)

在学习Linux之间我们先插入一下链表的知识

学习链表(一种数据结构思想)

链表和数组的区别和实现:

链表(链表是个好东西)

链表概念(什么是链表)?

链表就是数据结构->数据的存储思想

链表的每一项都是一个结构体

        说到数据存放,我们之前学过数组结构体,而且数组一开始就把大小确认了比如int arr[10],还有地址也都是连续的,因此我们需要对数组或者结构体进行增删改查的时候就会显得很不灵活,因为内存消耗很大所以我们有了链表的概念

Tips回顾:

数组的特点:每个元素的地址是连续的

                                                        链表的原理和解释图

链表的实现

t1是链表头  有点像数组首地址通过p++得到后面的元素

链表的动态遍历

添加上查找和计算链表元素个数的功能

这边定义一个found是很巧妙的,count 不断累加记录节点数,found 记录是否找到目标值

        使用 found 变量后,只需要在循环内部判断是否找到目标值,若找到就将 found 置为 1 ,循环结束后再根据 found 的值进行统一的输出判断,使代码逻辑更加简洁明了。

链表的从指定节点后方插入新节点

主要是insertBehindNum这段代码

因为链表有增删改查这些操作,所以这边我直接把链表的增删改查都写出来

这个代码里面包含了前插法(增),后插法(增),删除指定元素,改元素,查元素其中前插法和后插法

代码在这

#include <stdio.h>

struct Test

{

int data;

struct Test *next;

};

void printfInfo(struct Test *head)

{

struct Test *point;

point = head;

while(point !=NULL){

printf("%d ",point->data);

point =point->next;

}

putchar('\n');

}

int  printfInfo1(struct Test *head,int data)

{

int count = 0;

int found = 0;

struct Test *point;

point = head;

while(point !=NULL){

count++;

if((point->data) == data){

found=1;

}

point = point->next;

}

if(found==1){

printf("have\n");

}else{

printf("no\n");

}

return count;

}

struct Test * gaiNum(struct Test *head,int data,int newdata)

{

struct Test *p;

p = head;

while(p !=NULL){

if(p->data == data){

p->data=newdata;

}

p = p->next;

}

return head;

}

struct Test *insertBehindNum(struct Test *head,int data,struct Test *new)

{

struct Test *p;

p=head;

while(p != NULL){

if(p->data == data){

new->next= p->next;

p->next  = new;

return head;

}

    p = p->next;

}

return head;

}

struct Test *insertbeforeNum(struct Test *head,int data,struct Test *new)

{

struct Test *p;

p=head;

if(p->data == data){

new->next=p;

    printf("insert ok\n");

return new;

}

while(p->next != NULL){

if(p->next->data==data){

new->next = p->next;

  p->next = new;

  printf("insert ok\n");

  return head;

}

p = p->next;

}

return head;

}

struct Test *deleteNum(struct Test *head,int data)

{

struct Test *p;

p=head;

if(p->data == data){

p=p->next;

printf("delete ok\n");

return head;

}

while(p->next != NULL){

if(p->next->data == data){

p->next = p->next->next;

printf("delete ok~\n");

return head;

}

p = p->next;

}

return head;

}

int main()

{

 int count;

 struct Test *head = NULL;

 struct Test t1={1,NULL};

 struct Test t2={2,NULL};

 struct Test t3={3,NULL};

 struct Test t4={4,NULL};

 struct Test new={100,NULL};

 struct Test new2={999,NULL};

 struct Test new3={888,NULL};

 t1.next=&t2;

 t2.next=&t3;

 t3.next=&t4;

 printfInfo(&t1);

 count = printfInfo1(&t1,4);//查

 printf("链表元素个数为%d\n",count);

 head=insertBehindNum(&t1,3,&new);//在后方插入

 printfInfo(head);

 head = insertbeforeNum(&t1,3,&new2);//在前方插入

 printfInfo(head);

 head = insertbeforeNum(&t1,1,&new3);//在前方插入

 printfInfo(head);

 head = deleteNum(&t1,1);//删除

 printfInfo(head);

 head = gaiNum(&t1,1,40);//改

 printfInfo(head);

 return 0;

}

        看完之后是这样的我们是初学者那么我们就不应该考虑链表的效率问题,我们要先理解链表的含义和和操作原理,那么我们之后有更好的基础了之后,就可以去考虑链表的效率问题了。

http://www.xdnf.cn/news/4677.html

相关文章:

  • 边缘智能:当AI撕掉“云端依赖症”的标签——从纳米级芯片到城市级网络的算力觉醒之路
  • 69.x的平方根
  • MongoDB(六) - Studio 3T 基本使用教程
  • 顺丰科技:从 Presto 到 Doris 湖仓构架升级,提速 3 倍,降本 48%
  • OpenCV 基于生物视觉模型的工具------模拟人眼视网膜的生物视觉机制类cv::bioinspired::Retina
  • ffmpeg多媒体(音视频)处理常用命令
  • 按句子切分文本、保留 token 对齐信息、**适配 tokenizer(如 BERT)**这种需求
  • 【25软考网工】第五章(9)路由协议BGP、IS IS
  • PPT画图导出为PDF格式
  • 《云计算》第三版总结
  • Java 24:重构数字信任边界 —— 后量子时代的智能安全防御体系构建
  • 从装饰器出发,优雅处理 UI 自动化中的异常
  • Lost connect to debugger on ‘iphone‘
  • Webug4.0靶场通关笔记21- 第26关URL不安全跳转
  • 【Ubuntu】Netplan静态网络配置
  • 【ArcGIS技巧】用地块生成界址点去重、顺时针编号挂接DKBM属性
  • 四、Hadoop 2.X vs 3.X:特性、架构与性能全解析
  • 趣味编程:爱心
  • 昆仑万维财报解读:AI商业化卷王
  • CF每日5题
  • 《数据结构初阶》【链式二叉树】
  • 【时时三省】(C语言基础)怎样定义和引用二维数组
  • 数字孪生医疗:构建患者特异性数字孪生体路径探析
  • 【NLP 71、常见大模型的模型结构对比】
  • 缓存套餐-01.Spring Cache入门案例
  • 阿里云 golang 一面
  • 【开源】Python打造高效剪贴板历史管理器:实现跨平台生产力工具
  • 使用 Vite 创建 Vue 3 项目并手动配置路由的完整步骤
  • 如何通过服务主体获取 Azure 凭据
  • Ansible 流程控制