C语言学习笔记—链表(二)链表的静态添加及动态遍历

在这里插入图片描述

链表的静态添加及动态遍历

我们知道数组中的数据存储是有序的,而链表中的数据是无序的但是存在某种联系使之组成链表。
那么我们如果向一组数据中添加一个数据元素,对与数组来说比较的费劲,若是越界还得需要重新申请空间等方式满足数据元素的添加和存储。链表就比较简单了,只需要把对应的指针指向适合的节点地址即可。

#include <stdio.h>
//定义结构体
struct Test 
{
	int data;
	struct Test *next;//链表有一个指向自己的指针
};

int main()
{
	putchar('\n');
	//定义结构体变量
	struct Test t1 = {1,NULL};
	struct Test t2 = {2,NULL};
	struct Test t3 = {3,NULL};
	struct Test t4 = {4,NULL};
	struct Test t5 = {5,NULL};
	//指向对应节点地址形成链表
	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	//输出
	printf("%d %d %d %d %d \n",t1.data, t1.next->data, t1.next->next->data, t1.next->next->next->data, t1.next->next->next->next->data);
	return 0;
}

执行结果:在这里插入图片描述

我们优化一下上边程序的写法:

#include <stdio.h>
//定义结构体
struct Test 
{
	int data;
	struct Test *next;//链表有一个指向自己的指针
};
//输出链表数据
void printLink(struct Test *head)
{
	struct Test *piont = head;
	while(piont != NULL){
		printf("%d ",piont->data);
		piont = piont->next;
	}
	putchar('\n');
}
int main()
{
	
	//定义结构体变量
	struct Test t1 = {1,NULL};
	struct Test t2 = {2,NULL};
	struct Test t3 = {3,NULL};
	struct Test t4 = {4,NULL};
	struct Test t5 = {5,NULL};
	//指向对应节点地址形成链表
	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	//输出
	//printf("%d %d %d %d %d \n",t1.data, t1.next->data, t1.next->next->data, t1.next->next->next->data, t1.next->next->next->next->data);
	printLink(&t1);
	putchar('\n');
	
	return 0;
}

在这里插入图片描述

统计链表节点个数及链表查找

统计链表节点个数

接着用上边的例子:
思想:编写函数,设置一个计数器,通过传过来的链表头节点,查看其指向是否为空,不为空计数器加。

#include <stdio.h>
//定义结构体
struct Test 
{
	int data;
	struct Test *next;//链表有一个指向自己的指针
};

//获取列表节点数
int getLink(struct Test *head)
{
	int cnt = 0;
	struct Test *piont = head;
	while(piont != NULL){
		cnt++;
		piont = piont->next;
	}
	return cnt;
}

int main()
{
	
	//定义结构体变量
	struct Test t1 = {1,NULL};
	struct Test t2 = {2,NULL};
	struct Test t3 = {3,NULL};
	struct Test t4 = {4,NULL};
	struct Test t5 = {5,NULL};
	//指向对应节点地址形成链表
	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	int ret = getLink(head);
	printf("the link's is %d \n",ret);
	
	return 0;
}

执行结果:
在这里插入图片描述

链表数据查找
#include <stdio.h>
//定义结构体
struct Test 
{
	int data;
	struct Test *next;//链表有一个指向自己的指针
};
//查找数据
int searchLink(struct Test *head,int data)
{
	while(head != NULL){
		if(head->data == data){
		//如果输入的数据与链表里的某一项数据相等就返回1否则返回0
			return 1;
		}
		//跳到下一节点
		head = head->next;
	}
	return 0;
}


int main()
{
	
	//定义结构体变量
	struct Test t1 = {1,NULL};
	struct Test t2 = {2,NULL};
	struct Test t3 = {3,NULL};
	struct Test t4 = {4,NULL};
	struct Test t5 = {5,NULL};
	//指向对应节点地址形成链表
	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	//定义、输入数据
	int num;
	scanf("%d",&num);
	int ret = searchLink(&t1,num);
	//结果返回
	if(ret){
		printf("have %d\n",num);	
	}else{
		printf("don't have %d\n",num);
	}
	
	return 0;
}

执行结果:
输入不存在的数据:
在这里插入图片描述
存在的数据:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值