目录

老规矩,先看结果:

代码分析:

第一步:声明

第二步:输入函数

第三步:添加函数

第四步:输出函数

第五步:主函数

完整的代码:


老规矩,先看结果:


代码分析:

第一步:声明

#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; //结尾为NULLreturn head; //返回头指针
}

第三步:添加函数

struct student *add(struct student *h,int num,float score)
{struct student *p1,*p2,*New; //声明结构体变量p1=h; //p1开始指向头指针hp2=NULL; //p2不动,初始化为NULLwhile(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的尾部可以指向p1if(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);}
}

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

  1. c语言单链表功能,[数据结构]单链表(C语言)的各种功能

    06-03阅读200,000 + 链表是一种常见的基本数据结构,在此充分利用了结构指针. 链表可以动态存储和分配,即链表是一个功能非常强大的数组. 他可以在节点中定义多种数据类型,并可以根据需要随意添 ...

  2. C++语言单链表实现荷兰旗问题

    C++语言单链表实现荷兰旗问题 一.设备及软件 VC6.0 二.语言 C++ 三.涉及的数据结构与算法 单链表.尾插法 四.问题描述 荷兰旗问题亦称三色旗问题. 这里荷兰旗用0,1,2分别表示三种颜色 ...

  3. 第二章——单链表和循环单链表

    线性表--链表 顺序表需要事先占用一整块实现分配大小的存储空间,但是对于某些问题:很多空间只使用一次(甚至根本用不到),使用顺序表存储空间的利用率往往很低.于是需要一种能够动态管理存储空间的存储结构- ...

  4. C语言单链表基本操作总结

    C语言单链表基本操作     本文是参考他人实现的C语言单链表,对多篇博文整理的结果,仅作为学习笔记.文末有参考出处. 1.单链表定义 链表是通过一组任意的存储单元来存储线性表中的数据元素,这些存储单 ...

  5. C语言单链表实现多项式

    C语言单链表实现多项式 一.多项式的存储结构 注意:多项式每项的指数必须递增 typedef struct PNode{int coef; //系数int expn; //指数struct PNode ...

  6. C语言单链表实现19个功能完全详解

    #include "stdafx.h" #include "stdio.h" #include <stdlib.h> #include " ...

  7. c语言单链表_突破C语言难点之单链表?一绘图即可

    数据结构之单链表 单链表是一种链式存取的数据结构,用一组地址任意的存储单元 存放线性表中的数据元素 .链表中的数据是以结点来表示的,每个结点的构成:元素( 数据元素 的映象) + 指针 (指示后继元素 ...

  8. C语言单链表,能直接运行的代码!

    C语言单链表,实现增删改查 不废话 直接上代码,COPY就能运行 #include <stdio.h> #include <stdlib.h> /** *定义数据元素 */ t ...

  9. C语言单链表代码实现

    C语言单链表代码实现 一.头文件.常量以及自定义数据结构 #include<stdio.h> #include<malloc.h> #include<stdlib.h&g ...

最新文章

  1. 【Android 电量优化】电量优化 ( JobScheduler | JobService | AsyncTask )
  2. 微信小程序 详解 小程序支付
  3. Sitemesh 3 的使用及配置
  4. WCF系列教程之WCF客户端调用服务
  5. 用框架的你,可能早已忽略了这些事件API
  6. akka linux 端口,Actor模型开发库 Akka
  7. 动态规划——最小路径和(Leetcode 64)
  8. SAP License:雾里看花系列——SAP顾问应该脱离”保姆”的角色
  9. 还在一节一节数链条吗?使用SOLIDWORKS参数化设计自动计算链条节数
  10. 第一次~通过MockingBird进行声音模仿的感悟
  11. 【Novel AI】基于Koishi的QQ群配置AI绘图机器人方法
  12. Python爬虫实例:爬取“查IPIP”查询结果,查询IP地址归属地
  13. requests爬取图片(百思不得其姐)
  14. 通灵学院|游戏设计研习2:人类的外在人群特征★(1300字)
  15. golang 域名 转 ip
  16. arctanx麦克劳林公式推导过程_徒手搭建三角函数公式推导体系
  17. Ubuntu14.04网易云音乐的下载及安装 ssh安装 卸载 安装输入法
  18. 深入理解Pytorch负对数似然函数(torch.nn.NLLLoss)和交叉熵损失函数(torch.nn.CrossEntropyLoss)
  19. 一周热图|黄晓明、刘亦菲走进瑞士天梭工厂;卡特彼勒牵手CBA联赛;爱马仕匠心工坊登陆西安...
  20. 保研经验:夏令营英语面试不要慌,告诉你几个口语小技巧!

热门文章

  1. 2021届毕业应届生到现在的职场工作,从一个懵懂大学生到IT职场人的转变
  2. 163邮箱自动化登录实现模块化【2】
  3. 【字符识别】模板匹配(区域生长法)字母+数字识别【含Matlab源码 1214期】
  4. cdoj1087 基爷的中位数 二分
  5. ZZULIOJ1025
  6. EasyNVR是怎么做到Web浏览器播放RTSP摄像机直播视频延时控制在一秒内的
  7. vue+jQuery
  8. SharePoint中在线编辑文档
  9. 图网络embeding transE及node2vec方法
  10. MATLAB裁剪视频(裁剪固定区域)