思路:
1.构建插入链表,将输入的数据按成绩插入,成绩高的插入在前;
2.构建输出链表,按顺序输出链表信息;
3.构建删除链表,将高于60分的不用补考的学生信息删除。
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[10];
float score;
struct student* next;
};
void print(struct student* head)
{
struct student* p = head;
while (p != NULL)
{
printf("%s %.1f\n", p->name, p->score);
p = p->next;
}
}
struct student* insert(struct student* head)
{
struct student* p, * pnew, * pold=NULL;
pnew = (struct student*)malloc(sizeof(struct student));
scanf("%s%f", pnew->name, &pnew->score);
p = head;
if (pnew->score > head->score)
{
pnew->next = head;
head = pnew;
}
else
{
while (p != NULL && pnew->score < p->score)
{
pold = p;
p = p->next;
}
pnew->next = p;
pold->next = pnew;
}
return head;
}
struct student* pdelete(struct student* head, int grade)
{
struct student* p, * pold;
p = head;
while (head != NULL && head->score >= grade)
{
head = head->next;
free(p);
p = head;
}
if (head == NULL)
return head;
p = head->next;
pold = head;
while (p != NULL)
{
if (p->score >= grade)
{
pold->next = p->next;
free(p);
p = pold->next;
}
else
{
pold = p;
p = p->next;
}
}
return head;
}
int main()
{
struct student* head;
int i,n;
printf("input the number of nodes:\n");
scanf("%d", &n);
printf("plese input the students' names and scores:\n");
head = (struct student*)malloc(sizeof(struct student));
scanf("%s%f", head->name, &head->score);
head->next = NULL;
for (i = 1; i < n; i++)
head = insert(head);
printf("\noutput all nodes:\n");
print(head);
head = pdelete(head, 60);
printf("\noutput fail-nodes:\n");
print(head);
return 0;
}