c语言统计链表值的总合,C语言链表综合操作
/*----------------------------------预处理命令-----------------------------------------*/
#include
#include
#define LEN sizeof(struct student)
#define FORMAT "%ld,%f"
#define P_FORMAT "%ld %5.1f\n"
#define Null 0
/*---------------------------------构造结构体------------------------------------------*/
struct student//构造一个结构体类型
{
long num;//学生学号
float score;//学生成绩
struct student *next;//存放下一节点地址的指针变量
};
int n = 0; //用于统计节点个数的全局变量
/*---------------------------------建立链表函数--------------------------------------- */
struct student *creat(void) //建立链表的函数
{
struct student *p1,*p2,*head;
n = 0;//头结点个数初始化为0
p2 = p1 = (struct student *)malloc(LEN);//开辟一个节点,并使p1、p2指向它
scanf(FORMAT,&p1->num,&p1 ->score);//对第一个节点赋值
head = Null;//将头指针初始化为Null
while (p1->num != 0)
{
n = n + 1; //统计节点个数
if (n == 1)
{
head = p1;//将第一个节点的地址发送给头指针
}
else
{
p2->next = p1;//将新开辟的节点的地址发给前一节点指针变量next
}
p2 = p1;//将p2指向表尾(当前链表的最后一个节点)
p1 = (struct student *)malloc(LEN);//再开辟一个新节点,使p1指向它
scanf(FORMAT,&p1->num,&p1 ->score);
}
p2->next = Null;//若新开辟的节点num=0,那么表尾节点的指针变量置为NULL
return (head);
}
/*-----------------------------------输出链表函数--------------------------------------- */
void print(struct student *head)//输出链表的函数
{
struct student *p;
printf("\n\nNOW,These %d record are:\n",n);
if (head != Null) //判断头指针是否为空
{
p = head; //使p指第一个节点
do
{
printf(P_FORMAT,p->num,p->score);//输出p所指的节点的成员
p = p->next; //使p指向下一节点
}while (p != Null);
}
else
{
printf(" \nlist Null!\n");
}
return;
}
/*----------------------------------删除链表节点函数----------------------------------- */
struct student *del(struct student *head,long num)//删除链表节点的函数
{
struct student *p1,*p2;
if (head == Null) //判断头指针是否为空
{
printf("\nlist Null!\n");
}
else
{
p1 = head;
while (p1->num != num && p1->next != Null) //当p1所指节点不是要删除的节点以及不是表尾节点时,继续寻找。
{
p2 = p1;
p1 = p1->next;
}
if (p1->num == num)//判断p1是否是要删除的节点
{
if(p1 == head)//判断p1是否指向头结点
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
printf("\n%ld has been deleted\n",num);
n=n-1;
}
else
{
printf("\n%ld is not foud!\n",num);
}
}
return (head);
}
/*----------------------------------增加链表节点函数----------------------------------- */
struct student *insert(struct student *head,struct student *stu)//插入链表节点的函数
{
struct student *p0,*p1,*p2;
p0 = stu; //使p0指向要插入的节点
if (head == Null) //判断链表是否为空
{
head = p0; //将p0所指的节点作为唯一节点
}
else
{
p1 = head; //使p1指向第一个节点
while (p1->num < p0->num && p1->next != Null)//当p1所指的节点成员num小于要插入的节点num以及p1所指的不是表尾节点,继续寻找
{
p2 = p1;
p1 = p1->next;
}
if (p0->num <= p1->num) //判断是否找到大于等于要插入的节点num的节点
{
if (p1 == head) //判断p1是否指向头结点
{
head = p0; //插到表头之前
p0->next = p1;
}
else
{
p2->next = p0; //插到表中间
p0->next = p1;
}
}
else //插到表尾之后
{
p1->next = p0;
p0->next = Null;
}
}
n = n + 1;
return (head);
}
/*----------------------------------主函数----------------------------------- */
int main(void)
{
struct student *stu,*head;
long del_num;
printf("Input record:\n");
head = creat();//不能写成head = creat(void);
print(head);
printf("\nInput the deleted number:");
scanf("%ld",&del_num);
while (del_num != 0)
{
head = del(head,del_num);
print(head);
printf("\nInput the deleted number:");
scanf("%ld",&del_num);
}
printf("\nInput the inserted record: ");
stu = (struct student*)malloc(LEN);
scanf(FORMAT,&stu->num, &stu->score);
while (stu->num != 0)
{
head = insert(head,stu);
print(head);
printf("\nInput the inserted record: ");
stu = (struct student*)malloc(LEN);
scanf(FORMAT,&stu->num, &stu->score);
}
printf("The end\n");
return 0;
}
/*在VC++6.0中的输出结果为:
------------------------------------------
Input record:
10101,90
10102,876
10103,87
10104,100
10105,82
10106,76
10107,92
10108,91
10109,76
10110,82
10111,988
0,0
NOW,These 11 record are:
10101 90.0
10102 876.0
10103 87.0
10104 100.0
10105 82.0
10106 76.0
10107 92.0
10108 91.0
10109 76.0
10110 82.0
10111 988.0
Input the deleted number:10102
10102 has been deleted
NOW,These 10 record are:
10101 90.0
10103 87.0
10104 100.0
10105 82.0
10106 76.0
10107 92.0
10108 91.0
10109 76.0
10110 82.0
10111 988.0
Input the deleted number:10104
10104 has been deleted
NOW,These 9 record are:
10101 90.0
10103 87.0
10105 82.0
10106 76.0
10107 92.0
10108 91.0
10109 76.0
10110 82.0
10111 988.0
Input the deleted number:10111
10111 has been deleted
NOW,These 8 record are:
10101 90.0
10103 87.0
10105 82.0
10106 76.0
10107 92.0
10108 91.0
10109 76.0
10110 82.0
Input the deleted number:0
Input the inserted record: 10102,81
NOW,These 9 record are:
10101 90.0
10102 81.0
10103 87.0
10105 82.0
10106 76.0
10107 92.0
10108 91.0
10109 76.0
10110 82.0
Input the inserted record: 10104,93
NOW,These 10 record are:
10101 90.0
10102 81.0
10103 87.0
10104 93.0
10105 82.0
10106 76.0
10107 92.0
10108 91.0
10109 76.0
10110 82.0
Input the inserted record: 0,0
The end
Press any key to continue
c语言统计链表值的总合,C语言链表综合操作相关推荐
- asin c语言中 返回值范围_大学C语言考试易错知识点总结
作者:cggwz 来源:https://blog.csdn.net/cggwz/article/details/103740713?utm_medium=distribute.pc_relevant. ...
- c语言中初始值的作用,初始C语言学习
一.我的第一个C语言程序 #include int main() { printf("Hello C语言!\n"); printf("Hello Word!\n" ...
- 滑动窗口滤波 c语言,关于中值滤波算法 以及C语言实现
1.什么是中值滤波? 中值滤波是对一个滑动窗口内的诸像素灰度值排序,用其中值代替窗口中心象素的原来灰度值,它是一种非线性的图像平滑法,它对脉冲干扰级椒盐噪声的抑制效果好,在抑制随机噪声的同时能有效保护 ...
- c语言统计宿舍 信息,学生宿舍管理系统C语言.doc
<C语言程序设计>课程设计报告 题 目: 学生宿舍管理系统 专 业: 软件技术 班 级: 软件技术1班 姓 名: 李桦楠 齐忠春 周滨滨 徐烨 潘喜隆 郑竹强 学 号: 124109124 ...
- c语言统计字符串中各种字符个数,c语言统计字符串中各个字符的个数
目标: 输入一行字符,统计其中各种字符的个数. 具体代码: #include #include #include #define M 1024 void main() { char str[M]; f ...
- 检验int值在list中是否存在_R语言统计与绘图:卡方检验
卡方检验在计数资料中的应用,包括推断两个总体率或构成比之间有无差别.多个总体率或构成比之间有无差别.多个样本率间的多重比较.两个分类变量之间有无关联性.多维列联表的分析和频数分布拟合优度的卡方检验. ...
- R语言使用epiDisplay包的kap函数(kap.2.raters函数)计算Kappa统计量的值(总一致性、期望一致性)、对两个评分对象的结果进行一致性分析、评分的类别为多个类别
R语言使用epiDisplay包的kap函数(kap.2.raters函数)计算Kappa统计量的值(总一致性.期望一致性).对两个评分对象的结果进行一致性分析.评分的类别为多个类别 目录
- R语言epiDisplay包的kap函数计算配对列联表的计算一致性的比例以及Kappa统计量的值(总一致性、期望一致性)、使用xtabs函数生成二维列联表、使用wttable参数设置权重表参数为w2
R语言使用epiDisplay包的kap函数计算配对列联表的计算一致性的比例以及Kappa统计量的值(总一致性.期望一致性).使用xtabs函数生成二维列联表.使用wttable参数设置权重表参数为w ...
- R语言使用epiDisplay包的kap函数(kap.m.raters)计算Kappa统计量的值(总一致性、期望一致性)、对多个评分对象的结果进行一致性分析、评分的类别为多个类别
R语言使用epiDisplay包的kap函数(kap.m.raters)计算Kappa统计量的值(总一致性.期望一致性).对多个评分对象的结果进行一致性分析.评分的类别为多个类别 目录
最新文章
- rhel5.5安装xwindow
- 计算机php外文翻译,php外文翻译.doc
- MPB:林科院袁志林组-​枫香-真菌互作培养体系构建
- lr:lr中错误解决方法19种
- 创业故事:腾讯的创始人们
- Spring IOC和MVC基础知识
- 罗马数字转阿拉伯数字
- C++程序中调用其他exe可执行文件方法
- Luogu2495[SDOI2011]消耗战
- java编程 内存_Java编程技术之浅析JVM内存
- 深度强化学习-Actor-Critic算法原理和实现
- 第一篇:NSOperation的概念
- 道理与例子【人人都是产品经理:9009】
- vscode: remote-ssh下与阿里云端编程
- html代码大全右对齐,html特效代码大全
- scipy回归分析_业余时间学数据分析,如何快速上手
- 计算机 统计学考研,统计学考研科目有哪些
- MySQL 常用命令汇总
- Failed to obtain JDBC Connection;
- 百度快照劫持的表现及解决方法