输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。 提示: 定义函数struct stud_node *Creat_Stu_Doc()完成创建链表 定义函数struct stud_node *DeleteDoc(struct stud_node *head,int min_score)将分数低于min_score的结点删除 定义函数void Ptrint_Stu_Doc(struct stud_node *head)打印链表 输入输出示例:括号内为说明,无需输入输出
时间: 2023-05-26 13:07:11 浏览: 103
输入:
101 zhangsan 89
102 lisi 78
103 wangwu 92
104 zhaoliu 85
0
输入需要输出的最低成绩:90
输出:
103 wangwu 92
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *Creat_Stu_Doc() {
struct stud_node *p, *head=NULL;
int num, score;
char name[20];
scanf("%d", &num);
while (num != 0) {
scanf("%s %d", name, &score);
p = (struct stud_node*)malloc(sizeof(struct stud_node));
p->num = num;
strcpy(p->name, name);
p->score = score;
p->next = head;
head = p;
scanf("%d", &num);
}
return head;
}
struct stud_node *DeleteDoc(struct stud_node *head, int min_score) {
struct stud_node *p, *pre;
p = head;
while (p != NULL) {
if (p->score < min_score) {
if (p == head) {
head = p->next;
} else {
pre->next = p->next;
}
free(p);
p = pre->next;
} else {
pre = p;
p = p->next;
}
}
return head;
}
void Print_Stu_Doc(struct stud_node *head) {
struct stud_node *p;
p = head;
while (p != NULL) {
printf("%d %s %d\n", p->num, p->name, p->score);
p = p->next;
}
}
int main() {
struct stud_node *head;
int min_score;
head = Creat_Stu_Doc();
scanf("%d", &min_score);
head = DeleteDoc(head, min_score);
Print_Stu_Doc(head);
return 0;
}
阅读全文