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

学习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;

}

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

相关文章:

  • Java网络编程:深入剖析UDP数据报的奥秘与实践
  • 【Linux系统】第三节—权限
  • 使用 React 实现语音识别并转换功能
  • STM32教程:串口USART使用(基于STM32F103C8T6最小系统板标准库开发)*详细教程*
  • MCP 智能体性能监控、弹性扩展与大规模调度系统设计
  • 【Qt开发】Qt开发的认识
  • CF每日5题
  • 网络接入服务商查询
  • 数据结构-堆排序
  • Linux的基础开发工具
  • C++ - 输入输出
  • 网工实验——OSPF配置
  • 面试问题总结(回忆版)
  • 油藏模拟开源资源
  • [matlab]private和+等特殊目录在新版本matlab中不允许添加搜索路径解决方法
  • Android开发补充内容
  • 微信小程序备案的一些记录
  • MySQL 数据库初体验
  • 【基础复习笔记】计算机视觉
  • Android 查看 Logcat (可纯手机方式 无需电脑)
  • “仓促、有限”,美英公布贸易协议框架,两国分别获得了什么?
  • 上海质子重离子医院二期项目启动,有望成为全世界最大粒子治疗中心
  • 国家主席习近平同普京总统举行小范围会谈
  • 家庭相册㉙在沪打拼25年,我理解了父母清晨去卖蜜饯的辛苦
  • 中国以“大幅开放市场”回应贸易保护主义
  • 央行:上市公司回购增持股票自有资金比例要求从30%下调至10%