我是靠谱客的博主 激昂小懒猪,这篇文章主要介绍链表的简单应用 例一:学生成绩统计,现在分享给大家,希望可以做个参考。

    题目:定义一个学生信息结构,包括姓名、学号、和总成绩。从键盘上输若干学生信息,学生个数未知,当输入姓名为“#####”时表示输入结束。学生的总成绩为整数,范围是0~1600,不会出现非法输入。分别统计出1200以下,1200~1399,1400~1600各分数段学生人数。学生姓名中只能包含大小写字母与空格字符,不会超过20个;学号不超过20个字符,只包含数字字符。完成以下任务:

1,创建单链表,存储学生信息;

2,设计一个函数,函数名为Createlist(),用于建立链表;

3,函数Addlist(),用于向链表为追加结点;

4,函数Deallist(),统计各分数段学生人数;

5,函数Freelist(),释放链表结点,程序结束。

程序:

首先创建用于链表的结构

复制代码
1
2
3
4
5
6
7
typedef struct student{ char name[30]; char number[30]; int score; struct student* next; }Stu; typedef Stu* stu;

函数Createlist():

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//创建链表 stu Createlist() { stu head=NULL;//创建头指针,并初始化为0 char ch[7]="#####";//用于判断输入结束 char ah[30];//存储学生姓名 do { gets(ah); if (strcmp(ah,ch)){//判断输入是否为“######”,此处为对字符串的处理,不能简单使用“==”判断,注意:调用strcmp函数需要头文件<string.h> head=Addlist(head,ah);//添加链表结点 } }while (strcmp(ah,ch)); return head;//返回头指针的地址于主函数 }

关于这个函数,也可以调用头指针的地址,此时函数类型可为void,不需要返回值。

函数Addlist()

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//添加列表结点 stu Addlist(stu head , char* ah)//因为姓名在Greatelist()中用来判别结束,所以直接传入,不需在次函数中再次输入 { stu last=head; stu p=(stu)malloc(sizeof(Stu)); strcpy(p->name,ah);//初始化构建的链表 gets(p->number); scanf ("%d",&p->score); getchar();//用于吸收回车符 p->next=NULL; if (last){//找到链表此时的尾结点 while (last->next){ last=last->next; } last->next=p;//将新建的p连接在最后一位 } else{//如果p为第一个结点,只需将头指针指向p head=p; } return head;、//返回头指针 }

函数Deallist()

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
void Dealist (stu head,int* count)//传入数组count的地址,用于记录学生人数 { stu p; for (p=head ; p ;p=p->next){ if ((p->score)<1200) count[0]++; if (1200<=(p->score)&&(p->score)<=1399) count[1]++; if (1400<=(p->score)&&(p->score)<=1600) count[2]++; } }
函数 Freelist():
复制代码
1
2
3
4
5
6
7
8
void Freelist (stu head) { stu p,q;//此处需要新建 q ,因为 p 被清除后将没有 p->next for (p=head;p;p=q){ q=p->next; free(p); } }

总体程序为:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct student{ char name[30]; char number[30]; int score; struct student* next; }Stu; typedef Stu* stu; stu Createlist();//创建链表 stu Addlist (stu head,char* ah);//向链表尾追加结点 void Dealist (stu head,int* count);//统计各分数段学生 void print (int* count);//打印列表 void Freelist (stu head);//释放列表 int main(void) { stu head=NULL; int count[3]={0,0,0,}; head = Createlist(); Dealist (head,count); print (count); Freelist (head); return 0; } //创建链表 stu Createlist() { stu head=NULL; char ch[7]="#####"; char ah[30]; do { gets(ah); if (strcmp(ah,ch)){ head=Addlist(head,ah); } }while (strcmp(ah,ch)); return head; } //添加列表结点 stu Addlist(stu head , char* ah) { stu last=head; stu p=(stu)malloc(sizeof(Stu)); strcpy(p->name,ah); gets(p->number); scanf ("%d",&p->score); getchar(); p->next=NULL; if (last){ while (last->next){ last=last->next; } last->next=p;//将p连接在最后一位 } else{ head=p; } return head; } //处理链表数据 void Dealist (stu head,int* count) { stu p; for (p=head ; p ;p=p->next){ if ((p->score)<1200) count[0]++; if (1200<=(p->score)&&(p->score)<=1399) count[1]++; if (1400<=(p->score)&&(p->score)<=1600) count[2]++; } } //打印链表 void print (int* count) { printf ("The statistics result is:n"); printf ("There are %d students below 1200.n",count[0]); printf ("There are %d students between 1200 and 1399.n",count[1]); printf ("There are %d students between 1400 and 1600.n",count[2]); } //释放链表 void Freelist (stu head) { stu p,q;//此处需要新建 q ,因为 p 被清除后将没有 p->next for (p=head;p;p=q){ q=p->next; free(p); } }

最后

以上就是激昂小懒猪最近收集整理的关于链表的简单应用 例一:学生成绩统计的全部内容,更多相关链表的简单应用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部