PTA: Find student list (20 points) (C language)

This question requires students to create a keyboard to enter information into a singly linked list, and look at student information student information list.

Student information LLI node is defined as follows:

typedef struct List{
  int sno;
  char sname[10];
  List *next;
};

You need to create a function include:

Creating student information list function: CreateList

Find a list of student information functions: Find

Function interface definition:
List * CreateList (); // keyboard input several student number and name, student number and name with a space character spacing, student number when input is -1, the input end, create a student information list function that returns students head pointer list.
List * Find (List * head, int no) // query school student number is no information in the student list (head to head pointer), returns a pointer to the node student.

Ref test procedure Sample:
int main (void) {
List * List = NULL, P;
int NO;
List = CreateList ();
the while (~ Scanf ( "% D", & NO))
{
P = the Find (List, NO );
IF (P) the printf ( "% S \ n-", p-> sname);
the else the printf ( "Not Found \ n-!");
}
return 0;
}
/
Please fill in the answer here * /

Input Sample 1:
20180002 WANG
20180006 Yong
20180008 Wu Tao
20170010 Lin
-1 XX
20180002

Output Sample 1:
given here corresponding output. E.g:

Wang Hong

Input Sample 2:
20180002 WANG
20180006 Yong
20180008 Wu Tao
20170010 Lin
-1 XX
20,170,015

Output Sample 2:
Not Found!

List * CreateList()
{
    List *p, *head, *tail;
    int id; char name[10];

    head = (List*)malloc(sizeof(List));
    head->next = NULL;
    tail = head;
    scanf("%d", &id);
    while (id != -1)
    {
        p = (List*)malloc(sizeof(List));
        p->next = NULL;
        p->sno = id;
        scanf("%s", p->sname);
        scanf("%d", &id);
        tail->next = p;
        tail = p;
    }
    scanf("%s", name);

    return head;
}
List * Find(List *head, int no)
{
    List *p;

    p = head->next;
    while (p->sno != no)
    {
        if (p->next == NULL)
            return p->next;
        p = p->next;
    }

    return p;
}
Published 58 original articles · won praise 21 · views 600

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105400435