设计算法,根据输入的学生人数和成绩建立一个单链表,并累计成绩不及格的人数。
要求:
(1)学生人数和成绩均从键盘输入;
(2)输出所有学生的成绩和不及格的人数。
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node *next;
} StudNode, *StudLink;
void create(StudLink &sl)
{
int i, n, score;
StudNode *s, *r;
sl = (StudNode*)malloc(sizeof(StudNode));
r = sl;
printf("学生人数:");
scanf_s("%d", &n);
for (i = 0; i < n; i++){
s = (StudNode*)malloc(sizeof(StudNode));
printf("输入成绩:");
scanf_s("%d", &score);
s->data = score;
r->next = s;
r = s;
}
r->next = NULL;
}
int output(StudLink sl)
{
StudNode *q;
if (sl->next == NULL) return 0;
q = sl->next;
while (q != NULL)
{
printf("%d\t", q->data);
q = q->next;
}
}
int count(StudLink sl)
{
int n = 0;
StudNode *p = sl->next;
while (p != NULL)
{
if (p->data < 60) n++;
p = p->next;
}
return n;
}
void main()
{
int n;
StudLink h;
create(h);
n = count(h);
printf("所有学生的成绩:");
output(h);
printf("\n不及格人数:%d\n", n);
}
欢迎访问我的博客https://www.ndmiao.cn/