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

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真的想上岸啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值