学生成绩读取与分流函数
pta上的一道题
本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。
函数接口定义:
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:
struct stud_node {
int num; /学号/
char name[20]; /姓名/
int score; /成绩/
struct stud_node *next; /指向下个结点的指针/
};
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );
int main()
{
int min_score;
struct stud_node *p, *head = NULL;
1
2
3
4
5
6
7
8head = createlist(); scanf("%d", &min_score); head = deletelist(head, min_score); for ( p = head; p != NULL; p = p->next ) printf("%d %s %dn", p->num, p->name, p->score); return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80
输出样例:
2 wang 80
4 zhao 85
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#include <stdio.h> #include <stdlib.h> struct stud_node { int num; char name[20]; int score; struct stud_node *next; }; struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score ); int main() { int min_score; struct stud_node *p, *head = NULL; head = createlist(); scanf("%d", &min_score); head = deletelist(head, min_score); for ( p = head; p != NULL; p = p->next ) printf("%d %s %dn", p->num, p->name, p->score); return 0; } struct stud_node *createlist() { struct stud_node* head; struct stud_node* p; p=(struct stud_node*)malloc(sizeof(struct stud_node)); head=p; int a=1; while(1){ scanf("%d",&a); if(a==0){ break; } else{ p->next=(struct stud_node*)malloc(sizeof(struct stud_node)); p=p->next; } p->num=a; getchar(); scanf("%s",p->name); scanf("%d",&(p->score)); p->next=NULL; } head=head->next; return head; } struct stud_node *deletelist( struct stud_node *head, int min_score ) { if(head==NULL) return NULL; struct stud_node* stu; struct stud_node* p; struct stud_node* q; while((head->score)<min_score){ if(head->next==NULL) return NULL; head=head->next; } stu=head; p=stu->next; while(p!=NULL){ if(p->score<min_score){ p=p->next; stu->next=p; continue; } stu=p; p=stu->next; } return head; }
1
2
3
4
5
6
7
8
9
10链表是否为空 if(空) 推出; else{ 先找到第一个符合的元素 无 推出 有 找下一个 继续循环直到结束 }
最后
以上就是默默魔镜最近收集整理的关于学生成绩读取与分流函数的全部内容,更多相关学生成绩读取与分流函数内容请搜索靠谱客的其他文章。
发表评论 取消回复