本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。
函数接口定义:
struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score );
函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:
struct stud_node {
int num; /*学号*/
char name[20]; /*姓名*/
int score; /*成绩*/
struct stud_node *next; /*指向下个结点的指针*/
};
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。
struct stud_node *createlist()
{
struct stud_node *head = NULL, *temp, *l;
int n, s, i;
char m[20];
scanf("%d", &n);
while (n != 0)
{
scanf("%s %d", m, &s);
l = (struct stud_node *)malloc(sizeof(struct stud_node));
strcpy(l->name, m);
l->num = n;
l->score = s;
l->next = NULL;
if (head == NULL)
{
head = l;
temp = l;
}
else
{
temp->next = l;
temp = temp->next;
}
scanf("%d", &n);
}
return head;
}
struct stud_node *deletelist(struct stud_node *head, int min_score)
{
struct stud_node *p, *before;
p = head;
if (head == NULL)
return head;
while (p->score < min_score && p->next != NULL)
{
p = p->next;
}
if (p->next == NULL)
{
return NULL;
}
head = p;
while (p->next != NULL)
{
if (p->score < min_score)
{
before->next = p->next;
p = p->next;
}
else
{
before = p;
p = p->next;
}
}
if (p->score < min_score)
{
before->next = NULL;
}
return head;
}