6-7 deal with student achievement list (20 points) PTA

This problem required to achieve two functions, a tissue input student performance into unidirectional list; the score of another student scores below a certain node removed from the list.

Function interface definition:

struct stud_node *createlist();
struct stud_node *deletelist( struct stud_node *head, int min_score );

Createlist function scanf acquired using the input information from the student, and organize them into a one-way linked list, and returns a list head pointer.
List node structure is defined as follows:

struct stud_node {
    int              num;      /*学号*/
    char             name[20]; /*姓名*/
    int              score;    /*成绩*/
    struct stud_node *next;    /*指向下个结点的指针*/
};

Entered into a number of student information (number, name, score) and ends when the input student number is zero.

Function deletelist remove students score below min_score from head to head for the linked list pointer, head pointer and returns the results list.

Referee test program Example:

#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;
}

/* 你的代码将被嵌在这里 */

Here Insert Picture Description

AC Code:

struct stud_node *createlist()
{
    struct stud_node* head=(struct stud_node*)malloc(sizeof(struct stud_node));
    int num;
    scanf("%d",&num);
    if(num==0)
        return head;
    scanf("%s %d",head->name,&head->score);
    head->num=num;
    head->next=NULL;
    struct stud_node* p=head;
    while(1)
    {
        struct stud_node* tmp=(struct stud_node*)malloc(sizeof(struct stud_node));
        scanf("%d",&num);
        if(!num)
            break;
        tmp->num=num;
        scanf("%s %d",tmp->name,&tmp->score);
        p->next=tmp;
        p=p->next;
    }
    return head;
}

struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node* p=head;
    while(p!=NULL)
    {
        int a=p->score;
        if(a<min_score)
        {
            p=p->next;
        }
        else
        {
            head=p;
            break;
        }
    }
    if(p==NULL)
        return NULL;
    while(p->next!=NULL)
    {
        if(p->next->score<min_score)
        {
            p->next=p->next->next;
        }
        else
            p=p->next;
    }
    return head;
}

Ah Wei write their own code of Yo, hee hee hee ~

Published 32 original articles · won praise 12 · views 1376

Guess you like

Origin blog.csdn.net/qq_18873031/article/details/103073621