帮忙看看调试问题处在哪里?
#include "stdlib.h "
#include "stdio.h "
#define LEN sizeof(struct STU)
struct STU
{int num;
float score;
struct STU *next;
};
main()
{
int n,i;
struct STU *head,*p,*q;
head=NULL;
q=NULL;
printf( "please input one information of a student:\n number= ");
scanf( "%d ",&n);
while(n> 0)
{
p=(struct STU*)malloc(LEN);
//p-> num=n;
printf( "please input the num and score: ");
scanf( "%d,%f ", &(p-> score), &(p-> num));
p-> next=NULL;
if(head==NULL)
head=p;
else
q-> next=p;
q=p;
printf( "please input the information of student:\n number= ");
scanf( "%d ",&n);
}
p=head;
for(i=0;p!=NULL;i++)
{p=p-> next;
}
printf( "The node of this list is: %d\n ",i);
getch();
}
[解决办法]
上边的代码可以优化一下,把if(head==NULL)放在循环外边效率会高些。