题目如下
裁判测试程序样例:
#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 %d\n", p->num, p->name, p->score);
return 0;
}
/* 你的代码将被嵌在这里 */
代码如下
struct stud_node *createlist()
{
struct stud_node *head = (struct stud_node *)malloc(sizeof(struct stud_node));
head->next = NULL;
struct stud_node *tail = head;
int number;
scanf("%d",&number);
while(number!=0)
{
struct stud_node *tmp = (struct stud_node *)malloc(sizeof(struct stud_node));
tmp->next = NULL;
tmp->num = number;
scanf("%s",tmp->name);
scanf("%d",&tmp->score);
tail->next = tmp;//将临时结点连接到尾部
tail = tmp;
scanf("%d",&number);
}
return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
//在进行操作前,进行安全性检验
if(!head)
{
return head;
}
//创建一个结点进行遍历
struct stud_node *cur = head;
while(cur->next)
{
if(cur->next->score < min_score)
{
//创建临时结点,保存当前结点
struct stud_node *temp = cur->next;
//修改cur->next指向待删除结点的下一个结点
cur->next = temp->next;
//释放temp
free(temp);
}
else{
cur = cur->next;
}
}
return head->next;
}