函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。链表节点结构定义如下:
struct stud_node {
int num; /*学号*/
char name[20]; /*姓名*/
int score; /*成绩*/
struct stud_node *next; /*指向下个结点的指针*/
};
输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。
函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。
#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 = NULL;
int n,m;
char h;
scanf("%d", &n);
while (n != 0)
{
struct stud_node* space = (struct stud_node*)malloc(sizeof(struct stud_node));
space->num = n;
scanf("%s", space->name);
scanf("%d", &space->score);
space->next = NULL;
if (Head == NULL)
Head = space;
else
{
struct stud_node* py = Head;
while (py->next!=NULL)
py = py->next;
py->next = space;
}
scanf("%d", &n);
}
return Head;
}
struct stud_node* deletelist(struct stud_node* head, int min_score)
{
if (head == NULL)
return;
if (head->score < min_score)
{
struct Node* pp = head;
head = head->next;
free(pp);
}
struct Node* q = head;
struct stud_node* p = q;
while (p->next!=NULL)
{
struct stud_node* py = p->next;
if (py->score < min_score)
{
p->next = py->next;
free(py);
continue;
}
p = p->next;
}
return head;
}