设计算法,根据输入的学生人数和成绩建立一个单链表,并累计成绩不及格的人数。
要求:
(1)学生人数和成绩均从键盘输入;
(2)输出所有学生的成绩和不及格的人数。
1 #include<stdio.h> 2 #include<malloc.h> 3 typedef int ElemType; 4 typedef struct node 5 { 6 ElemType data; 7 struct node *next; 8 } StudNode, *StudLink; 9 10 void create(StudLink &sl) 11 { 12 int i, n, score; 13 StudNode *s, *r; 14 sl = (StudNode*)malloc(sizeof(StudNode)); 15 r = sl; 16 printf("学生人数:"); 17 scanf_s("%d", &n); 18 for (i = 0; i < n; i++){ 19 s = (StudNode*)malloc(sizeof(StudNode)); 20 printf("输入成绩:"); 21 scanf_s("%d", &score); 22 s->data = score; 23 r->next = s; 24 r = s; 25 } 26 r->next = NULL; 27 } 28 int output(StudLink sl) 29 { 30 StudNode *q; 31 if (sl->next == NULL) return 0; 32 q = sl->next; 33 while (q != NULL) 34 { 35 printf("%d\t", q->data); 36 q = q->next; 37 } 38 } 39 int count(StudLink sl) 40 { 41 int n = 0; 42 StudNode *p = sl->next; 43 while (p != NULL) 44 { 45 if (p->data < 60) n++; 46 p = p->next; 47 } 48 return n; 49 } 50 void main() 51 { 52 int n; 53 StudLink h; 54 create(h); 55 n = count(h); 56 printf("所有学生的成绩:"); 57 output(h); 58 printf("\n不及格人数:%d\n", n); 59 }
欢迎访问我的博客https://www.ndmiao.cn/