This problem requires two functions, one is to organize the input students' scores into a one-way linked list, the other is to delete the students' nodes whose scores are lower than a certain score line from the linked list.
Function interface definition:
struct stud_node *createlist(); struct stud_node *deletelist( struct stud_node *head, int min_score );
The function createlist uses scanf to get the information of students from the input, organizes it into a one-way linked list, and returns the pointer of the link header. The structure of linked list nodes is defined as follows:
struct stud_node { int num; /*Student ID*/ char name[20]; /*Full name*/ int score; /*achievement*/ struct stud_node *next; /*Pointer to next node*/ };
Enter the information (student number, name, score) of several students. When the student number is 0, it ends.
The deletelist function deletes the students whose score is lower than min'score from the linked list with head as the head pointer, and returns the head pointer of the result linked list.
Sample referee test procedure:
#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; } /* Your code will be embedded here */
Input example:
1 zhang 78 2 wang 80 3 li 75 4 zhao 85 0 80
Output example:
2 wang 80 4 zhao 85
Code:
struct stud_node *createlist() { struct stud_node *p=NULL,*head=NULL,*tail=NULL; int num,score; char name[20]; head=(struct stud_node*)malloc(sizeof(struct stud_node)); head->next=NULL; tail=head; scanf("%d",&num); while(1) { p=(struct stud_node*)malloc(sizeof(struct stud_node)); if(num==0)break; p->num=num; p->next=NULL; scanf("%s%d",p->name,&p->score); tail->next=p; tail=p; scanf("%d",&num); } return head; } struct stud_node *deletelist( struct stud_node *head, int min_score ) { struct stud_node *p=head; int flag; while(p->next!=NULL) { flag=0; if(p->next->score<min_score) { p->next=p->next->next; flag=1; } if(flag==0)p=p->next; } return head->next; }