Home > Back-end >  The great god. I am the beginner C. Please check my list error
The great god. I am the beginner C. Please check my list error

Time:10-19

Specific, I do a list, call three function respectively, one is to create the indicator function, one is the sorting function, one is the output function, to the effect of input data, sort by grade, below is the program I wrote, the last one to perform error screenshots, seek expert points out mistakes and correct, in order to achieve the desired effect, thank you!


# include & lt; stdio.h>
# include & lt; String. H>
# include & lt; Stdlib. H>


Void sort (struct student mm [], int n);
Struct student * creatlist (struct student * head);
Void print (struct student * pp);
Struct student
{
Char name [20].
Int sc;
Struct student * next;
};
Int n=0;
Int main (void)
{
Struct student * head;
The head=creatlist (head);
Sort (head, n);
Print (the head);

return 0;
}

Struct student * creatlist (struct student * head)
{
Char a;
P1 struct student * * (p2);
The p2=(struct student *) malloc (sizeof (struct student));
P1=p2;
Do
{
If (n!=0)
{
P1 - & gt; Next=p2;
P1=p2;
}
+ + n.
Printf (" please enter the first % d a student's name: ", n);
The scanf (" % s ", p2 - & gt; Name);
Printf (" please enter the first % d student performance, ", n);
The scanf (" % d ", & amp; (* (p2). Sc);
If (n=1)
The head=p1;
The p2=(struct student *) malloc (sizeof (struct student));

Printf (" whether to continue the input? Y/N)?" );
//the scanf (" % c ", & amp; a);
A=getchar ();

}
While (a=='y' | | a=='y');
P1 - & gt; Next=NULL;


Return (the head);
}

Void sort (struct student mm [], int n)
{
int i,j;
for (i=0; iFor (j=I + 1; JIf (mm [I] sc> Mm [j]. Journal of sc)
{
Mm [I] next=mm [j]. Next;
Mm [j] next=& amp; Mm [I];
}

return;
}
Void print (struct student * pp)
{
Printf (" pos: \ n ");
Do
{
Printf (" name: % s, score: % d \ n ", pp - & gt; The name, pp - & gt; Sc);
Pp=pp - & gt; next;
} while (pp - & gt; Next!=NULL);

return;
}




Yes prompt error
But
The operation of the error result
Is




Please answer as soon as possible thank you!



CodePudding user response:

What time do you here have problems:
1, in after you input the first data, you can use the scanf (" % s ", & amp; a); Prompt for Y/N, rather than a=getchar ();
2, the if (n=1) should be n==1;
3, in the judgment, while the output of the (pp - & gt; Next! Pp (=NULL) should be for a while!=NULL);

CodePudding user response:

You have a list sorting problem in detail can refer to http://blog.csdn.net/jasonxzy/article/details/5764348
  • Related