Home > Back-end >  Establish student information list
Establish student information list

Time:11-02

Title:
Subject demanding an input of student achievement, singly linked list of simple function,

Function interface definition:

Void input ();
The function USES the scanf for student information from the input, and organized into a singly linked list, linked list node structure are defined as follows:

Struct stud_node {
Int num./* */student id
Char name [20]./* name */
Int score;/* grade */
Struct stud_node * next; Pointer to the next node/* */
};
Singly linked list of end pointer stored in global variables in the head and tail,

Input for a number of students information (student id, name, grade), end when enter student id 0,

The referee sample testing program:

#include
#include
#include

Struct stud_node {
Int num.
Char name [20].
Int score;
Struct stud_node * next;
};
Head struct stud_node * and * tail;

Void input ();

Int main ()
{
Struct stud_node * p;

The head tail of==NULL;
The input ();
For (p=head; P!=NULL; P=p - & gt; Next)
Printf (" % d % d % s \ n ", p - & gt; Num, p - & gt; The name, p - & gt; Score);

return 0;
}

/* * your code will be embedded in here/
Input the sample:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
The output sample:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85

Correct answer: (testing, but by the pta/go up after a test point is wrong)
#include
#include
#include
Struct stud_node
{
int num;
Char name [20].
int score;
Struct stud_node * next;
Head} * and * tail;
Void input ();
Int main ()
{
Struct stud_node * p;
The head tail of==NULL;
The input ();
For (p=head; p!=NULL; P=p - & gt; Next)
Printf (" % d % d % s \ n ", p - & gt; Num, p - & gt; The name, p - & gt; Score);
return 0;
}
Void input ()
{
Struct stud_node * q;
int score;
Char name [20].
Q=(struct stud_node *) malloc (sizeof (struct stud_node));
The scanf (" % d ", & amp; Q - & gt; Num);
While (q - & gt; Num!=0)
{
The scanf (" % s % d ", the name, & amp; Score);
Strcpy (q - & gt; The name, name);
Q - & gt; Score=score;
Q - & gt; Next=NULL;
If (head==NULL)
{
The head=q;
Tail=q;
}
The else
{
Tail - & gt; Next=q;
}
Tail=q;
Q=(struct stud_node *) malloc (sizeof (struct stud_node));
The scanf (" % d ", & amp; Q - & gt; Num);
}
Tail - & gt; Next=NULL;
}

CodePudding user response:

Void input ()
{
Struct stud_node * q;
int score;
Char name [20].
Q=(struct stud_node *) malloc (sizeof (struct stud_node));
The scanf (" % d ", & amp; Q - & gt; Num);
If (q - & gt; Num==0) head=NULL;
The else
{
While (q - & gt; Num!=0)
{
The scanf (" % s % d ", the name, & amp; Score);
Strcpy (q - & gt; The name, name);
Q - & gt; Score=score;
Q - & gt; Next=NULL;
If (head==NULL)
{
The head=q;
Tail=q;
}
The else tail - & gt; Next=q;
Tail=q;
Q=(struct stud_node *) malloc (sizeof (struct stud_node));
The scanf (" % d ", & amp; Q - & gt; Num);
}
Tail - & gt; Next=NULL;
}
}

Considering the input of the first number is 0, the program will crash
  • Related