Home > Back-end >  P -> next=pstudHead -> next; PstudHead -> next=p what do you mean?
P -> next=pstudHead -> next; PstudHead -> next=p what do you mean?

Time:10-09

 struct Student {
Int studid./* */student id
Char name [10]./* name */
Int math;/* */
in mathInt English; English/* */
Int total;/* grade */
Struct Student * next; Pointer to the next node/* */
};
Struct Student * pstudHead;/* global pointer variable to head node */
Void main () {
Struct Student * p; Temporary structure pointer/* */
PstudHead=(struct Student *) malloc (sizeof (struct Student));
PstudHead - & gt; Next=NULL;
While (1)
{
P=(struct Student *) malloc (sizeof (struct Student));
The scanf (" % d ", & amp; (p - & gt; Studid));
If (p - & gt; Studid==1)
{free (p); break; }
The scanf (" % s ", & amp; (p - & gt; Name)); The scanf (" % d ", & amp; (p - & gt; Math));
The scanf (" % d ", & amp; (p - & gt; English)); P - & gt; Total=p - & gt; Math + p - & gt; English;
P - & gt; Next=pstudHead - & gt; Next; PstudHead - & gt; Next=p;
}
P=pstudHead - & gt; Next;
Printf (" student id \ t \ \ t t name English grades in math scores \ n ");
While (s!=NULL) {
Printf (" % d \ t ", p - & gt; Studid); Printf (" % s \ t ", p - & gt; Name);
Printf (" % d \ t ", p - & gt; Math); Printf (" % d \ t ", p - & gt; English);
Printf (" % d \ n ", p - & gt; Total); P=p - & gt; Next;
}
}

Textbook program, always don't understand here

CodePudding user response:

When the students entered the first p - & gt; Next=NULL, patudHead - & gt; Next=first pointer,
When students enter the second, p points to the first student, pointing towards the second student

So each loop is, before the new student to a student, the head of the list to add the latest student,
  • Related