链表全家桶,齐全

目录

链表的概念:

1.创建结点:

2.链表的创建(输入):

3.链表的输出:

4.链表的求最大值(max):

5.链表的插入:

6.链表的删除:

7.主函数代码:


链表的概念:

        链表是一种物理存储单元上非连续、非顺序的存储结构数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

链表的优缺点:

优点:在插入和删除操作时,只需要修改被删节点上一节点的链接地址,不需要移动元素,从而改进了在顺序存储结构中的插入和删除操作需要移动大量元素的缺点。

缺点:

1、没有解决连续存储分配带来的表长难以确定的问题。

2、失去了顺序存储结构随机存取的特性。

1.创建结点:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
//创建结点结构体
struct node{
	int data; //数据域 
	struct node *next; //指针域 
}; 


2.链表的创建(输入):

int create(struct node *head){
	struct node *p=head; //2、定义头指针指向头结点 
	//3、循环创建5个结点 
	struct node *q; //新结点 
	int i;
	for(i=0;i<5;i++){
		q=(struct node *)malloc(sizeof(struct node)); 
		printf("请输入数据:");
		scanf("%d",&q->data);  //数据域赋值
		// 头插法
		q->next=head->next; 
		head->next=q;
	} 
} 

 create  的意思就是创建,

struct node *p=head; //2、定义头指针指向头结点      地址指向第一个结点

3.链表的输出:

int shuchu(struct node *head){
	struct node *p=head;
	p=p->next; //跳过头结点,指向第一个有数据的结点 
	//结果---逆序输出 
//	struct node *p=head->next; 
	while(p!=NULL){
		printf("%d	",p->data); //数据当前结点的数据 
		p=p->next;  //跳到下一个节点上 
	}
} 

 28行:代码p=p->next     指向第一个有数据的结点,存放数据的位置

NULL  表示   空值   意思也很明确就是什么数据也没有的意思

当p所指向的空间是有数据的就输出数据,

4.链表的求最大值(max):

int max(struct node *head){
	
	struct node *p=head->next;
	int max=p->data; //把该结点的数当成最大值
	while(p!=NULL){
		if(max<p->data){
			max=p->data;
		} 
		p=p->next; //指针后移   
		//先比较,再指针后移 (不然存在p变成NULL,还取数据的情况) 
	} 
	printf("最大值为:%d\n",max);
} 

 int max   定义一个 整型变量,把p所指向的数据域的存放第一个数据的值给max   ,把第一个值当成最大值再与其余数据进行比较,如果比我们假设的最大值还大,那么就是更新  max   的值

  91行代码:  p=p->next; //指针后移     指针后移  说白就是往后移动一位,87行代码是循环,88行代码就是判断max的值是否比其他数据大,大的话就互换位置,更新最大值,

最后再输出其max的值,就是我们所有数据的最大的那个数。    

5.链表的插入:

int charu(struct node *head){
	int n,i;	
	struct node *p=head;
	printf("\n请输入你要插入数据的位置:");
	scanf("%d",&n); 
	for(i=1;i<n;i++){  // 循环n-1 
		p=p->next; //跳到下一个结点 
		if(p->next==NULL){ //当前结点指针域为NULL,没法断开 
			printf("位置不合适!");
			return 0; 
		}	
	}
	//创建要插入的节点
	struct node *a; //a 是要插入的结点 	
	a=(struct node *)malloc(sizeof(struct node));  
	printf("请输入数据:");
	scanf("%d",&a->data); 
	
	//插入到对应位置
	a->next=p->next; 
	p->next=a;
} 

这就是插入数据的完整代码,n  就是我们要插入的数据的位置,i   是我们数据的下标

下标从1开始,当<n时,我们就找到了我们想插入的位置,if  判断   p->next==NULL  表示

当前结点指针域为        NULL  (为空),没有办法断开,也就表示没有办法插入我们想

插入的数据,

6.链表的删除:

int shanchu(struct node *head){
	struct node *p=head; //指向头结点
	int n;
	printf("\n请输入你要删除的位置(第几个数):");
	scanf("%d",&n); 
	//跳到删除的前一位 
	int i;
	for(i=1;i<n;i++){ //循环n-1次
		p=p->next; 
	 	//合理判断一下 
		 if(p->next==NULL){
		 	printf("删除不合适!");
		 	return 0; 
		 }	
	} 
//	p->next=p->next->next; 
	//删除结点
	struct node *del=p->next; //del 保存你要删除的结点
	p->next=del->next; 
	free(del); //释放删除的结点 
}

链表的插入和链表的删除是有着相似之处的,也可以套用插入的模块儿的,先输入要删除的位置(第几个数),通过下标找到的,然后在循环里判断一下,我们删除的位置,是否在我们的范围之内,不在就显示不合适,在的话,就继续往下执行。

7.主函数代码:

int main(){
	//创建链表
	//	1、创建头结点,指针域为NULL
	struct node *head=(struct node *)malloc(sizeof(struct node)); 
	head->next=NULL;
	
	create(head); //创建函数 
	
	shuchu(head); //输出函数 

	charu(head);  //插入函数 
	
	shuchu(head); //输出函数 
	
	shanchu(head);  //删除函数 
	 
	shuchu(head); //输出函数 
	
	max(head);  //最大函数 
} 

下面是完整的代码,可复制:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
//创建结点结构体
struct node{
	int data; //数据域 
	struct node *next; //指针域 
}; 

int create(struct node *head){
	struct node *p=head; //2、定义头指针指向头结点 
	//3、循环创建5个结点 
	struct node *q; //新结点 
	int i;
	for(i=0;i<5;i++){
		q=(struct node *)malloc(sizeof(struct node)); 
		printf("请输入数据:");
		scanf("%d",&q->data);  //数据域赋值
		// 头插法
		q->next=head->next; 
		head->next=q;
	} 
} 

int shuchu(struct node *head){
	struct node *p=head;
	p=p->next; //跳过头结点,指向第一个有数据的结点 
	//结果---逆序输出 
//	struct node *p=head->next; 
	while(p!=NULL){
		printf("%d	",p->data); //数据当前结点的数据 
		p=p->next;  //跳到下一个节点上 
	}
} 


int charu(struct node *head){
	int n,i;	
	struct node *p=head;
	printf("\n请输入你要插入数据的位置:");
	scanf("%d",&n); 
	for(i=1;i<n;i++){  // 循环n-1 
		p=p->next; //跳到下一个结点 
		if(p->next==NULL){ //当前结点指针域为NULL,没法断开 
			printf("位置不合适!");
			return 0; 
		}	
	}
	//创建要插入的节点
	struct node *a; //a 是要插入的结点 	
	a=(struct node *)malloc(sizeof(struct node));  
	printf("请输入数据:");
	scanf("%d",&a->data); 
	
	//插入到对应位置
	a->next=p->next; 
	p->next=a;
} 

int shanchu(struct node *head){
	struct node *p=head; //指向头结点
	int n;
	printf("\n请输入你要删除的位置(第几个数):");
	scanf("%d",&n); 
	//跳到删除的前一位 
	int i;
	for(i=1;i<n;i++){ //循环n-1次
		p=p->next; 
	 	//合理判断一下 
		 if(p->next==NULL){
		 	printf("删除不合适!");
		 	return 0; 
		 }	
	} 
//	p->next=p->next->next; 
	//删除结点
	struct node *del=p->next; //del 保存你要删除的结点
	p->next=del->next; 
	free(del); //释放删除的结点 
}

int max(struct node *head){
	
	struct node *p=head->next;
	int max=p->data; //把该结点的数当成最大值
	while(p!=NULL){
		if(max<p->data){
			max=p->data;
		} 
		p=p->next; //指针后移   
		//先比较,再指针后移 (不然存在p变成NULL,还取数据的情况) 
	} 
	printf("最大值为:%d\n",max);
} 

int main(){
	//创建链表
	//	1、创建头结点,指针域为NULL
	struct node *head=(struct node *)malloc(sizeof(struct node)); 
	head->next=NULL;
	
	create(head); //创建函数 
	
	shuchu(head); //输出函数 

	charu(head);  //插入函数 
	
	shuchu(head); //输出函数 
	
	shanchu(head);  //删除函数 
	 
	shuchu(head); //输出函数 
	
	max(head);  //最大函数 
} 

 为了方便我就用拼音代替了,本人表示英语这一块是有点缺陷的,哈哈哈😄,不要介意,目前还在学习的阶段,如果有什么不对的,或者有什么缺陷的,还请各位大神多多指点指点,嘿嘿(●ˇ∀ˇ●)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值