C语言:单链表的循环添加、插入操作,直到不在插入为止

 

目录

老规矩,先看结果:

代码分析: 

第一步:声明

第二步:输入函数

 第三步:添加函数

 第四步:输出函数

 第五步:主函数

完整的代码:


老规矩,先看结果:


代码分析: 

第一步:声明

#include<stdio.h>

#include<stdlib.h>

struct student  //声明结构体类型
{
	int num;
	float score;
	struct student *next;
};

int n; //全局变量

第二步:输入函数

struct student *creat()
{
	struct student *head;  
	struct student *p1,*p2; //声明结构体类型变量

	p1=(struct student *)malloc(sizeof(struct student)); //开辟新的空间
	p2=p1;  //赋值

	printf("请输入学生学号、成绩 :");
	scanf("%d%f",&p1->num,&p1->score); //输入数据

	head=NULL; //赋初始值
	n=0; //赋初始值

	while(p1->num!=0) //循环输入
	{
		n++; //记录结点数

		if(n==1) //头结点
		{
			head=p1; //赋值
		}

		else
		{
			p2->next=p1; //p2指针指向p1
		}
		p2=p1; //赋值

		p1=(struct student *)malloc(sizeof(struct student)); //又开辟新的空间
		printf("请输入学生学号、成绩:");
		scanf("%d%f",&p1->num,&p1->score); //输入
	}
	p2->next=NULL; //结尾为NULL

	return head; //返回头指针
}

 第三步:添加函数

struct student *add(struct student *h,int num,float score)
{
	struct student *p1,*p2,*New; //声明结构体变量

	p1=h; //p1开始指向头指针h
	p2=NULL; //p2不动,初始化为NULL
	
	while(p1!=NULL&&p1->num<num) //判断结点在何处
	{
		p2=p1;
		p1=p1->next;
	}
	
	New=(struct student *)malloc(sizeof(struct student)); //开辟新的空间
	
	New->num=num; //输入的数据给相应的结点
	New->score=score; //输入的数据给相应的结点
	New->next=p1; //由于结点已找到,所以New的尾部可以指向p1
	
	if(p2==NULL) //p2未动,说明在头部
	{
		h=New; //将New的头部赋值给h
	}
	else
	{
		p2->next=New; //否则为中间或结尾,将New的头部赋值给p2的尾部
	}
	
	return h; //返回新的链表
}

 第四步:输出函数

void print(struct student *h)
{
	struct student *p;
	p=h;
	if(h!=NULL) //判断是否为空指针
	{
		printf("\n结果是:");
		do{
			printf("\n\t%d\t%.2f",p->num,p->score); //循环输出
			p=p->next;
		}while(p!=NULL);
	}
}

 第五步:主函数

int main()
{
	struct student *creat(); //输入函数声明
	struct student *add(struct student *h,int num,float score); //插入函数声明
	void print(struct student *h); //输出函数声明
	
	struct student *h; 
	int num;
	float score;
	int ch;
	
	h=creat(); //调用输入函数
	print(h); //调用输出函数,打印结果
	
	while(1) //外循环,判断是否要添加数据
	{
		printf("\n你是否需要添加数据:"); 
		do{
			ch=getchar();
		}while(ch!='Y'&&ch!='N'); //用do——while循环判断,如果输入错误,再次输入
		
		if(ch=='Y') 
		{
			printf("\n请你输入要加入的学生学号:");
			scanf("%d",&num); //输入要添加的数据

			printf("请你输入要加入的学生成绩:");
			scanf("%f",&score); //输入要添加的数据

			h=add(h,num,score); //调用添加函数
			print(h);
		}
		else
		{
			break; //如果不添加,则跳出
		}
	}
	
	printf("添加完毕!");
	printf("最终数据为:\n");
	print(h);
	
	return 0;
}

 


完整的代码:

 

#include<stdio.h>
#include<stdlib.h>
struct student 
{
	int num;
	float score;
	struct student *next;
};
int n;
int main()
{
	struct student *creat();
	struct student *add(struct student *h,int num,float score);
	void print(struct student *h);
	
	struct student *h;
	int num;
	float score;
	int ch;
	
	h=creat();
	print(h);
	
	while(1)
	{
		printf("\n你是否需要添加数据:");
		do{
			ch=getchar();
		}while(ch!='Y'&&ch!='N');
		
		if(ch=='Y')
		{
			printf("\n请你输入要加入的学生学号:");
			scanf("%d",&num);
			printf("请你输入要加入的学生成绩:");
			scanf("%f",&score);
			h=add(h,num,score);
			print(h);
		}
		else
		{
			break;
		}
	}
	
	printf("添加完毕!");
	printf("最终数据为:\n");
	print(h);
	
	return 0;
}

struct student *creat()
{
	struct student *head;
	struct student *p1,*p2;
	p1=(struct student *)malloc(sizeof(struct student));
	p2=p1;
	printf("请输入学生学号、成绩 :");
	scanf("%d%f",&p1->num,&p1->score);
	head=NULL;
	n=0;
	while(p1->num!=0)
	{
		n++;
		if(n==1)
		{
			head=p1;
		}
		else
		{
			p2->next=p1;
		}
		p2=p1;
		p1=(struct student *)malloc(sizeof(struct student));
		printf("请输入学生学号、成绩:");
		scanf("%d%f",&p1->num,&p1->score);
	}
	p2->next=NULL;
	return head;
}

struct student *add(struct student *h,int num,float score)
{
	struct student *p1,*p2,*New;
	p1=h;
	p2=NULL;
	
	while(p1!=NULL&&p1->num<num)
	{
		p2=p1;
		p1=p1->next;
	}
	
	New=(struct student *)malloc(sizeof(struct student));
	
	New->num=num;
	New->score=score;
	New->next=p1;
	
	if(p2==NULL)
	{
		h=New;
	}
	else
	{
		p2->next=New;
	}
	
	return h;
}

void print(struct student *h)
{
	struct student *p;
	p=h;
	if(h!=NULL)
	{
		printf("\n结果是:");
		do{
			printf("\n\t%d\t%.2f",p->num,p->score);
			p=p->next;
		}while(p!=NULL);
	}
}

  • 17
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小黎的培培笔录

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

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

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

打赏作者

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

抵扣说明:

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

余额充值