有一道c,关于结点的,我用函数做,可是不知错在那里
#include <stdio.h>
#include <stdlib.h>
struct student
{
int num;
char name[20];
int score;
struct student *next;
}
#define size sizeof(struct student)
(struct student *) creat();
void print(struct student *);
struct student *head;
struct student *temp,*last;
//--------------------------------
void main()
{
printf( "请输入数据:\n ");
head=creat();
print(head);
}
//-------------------------------------
(struct student *) creat()
{
int n=1;
last=temp=(struct student *)malloc(size);
head=NULL;
printf( "请输入第%d个学生的学号: ",n);
scanf( "%d ",&temp-> num);
printf( "请输入第%d个学生的姓名: ",n);
scanf( "%s ",temp-> name);
printf( "请输入第%d个学生的分数: ",n);
scanf( "%d ",&temp-> score);
while(temp-> num!=0)
{
if(n==1)
{
head=temp;
n++;
}
else
{
temp=(struct student *)malloc(size);
printf( "请输入第%d个学生的学号: ",n);
scanf( "%d ",&temp-> num);
printf( "请输入第%d个学生的姓名: ",n);
scanf( "%s ",temp-> name);
printf( "请输入第%d个学生的分数: ",n);
scanf( "%d ",&temp-> score);
last-> next=temp;
last=temp;
n++;
}
temp-> next=NULL;
}
return(head);
}
//------------------------------------
viod print(struct student *head)
{
temp=head;
do
{
printf( "%d,%s,%d ",temp-> num,temp-> name,temp-> score);
temp=temp-> next;
}while(temp-> next!=NULL);
}
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#define size sizeof(struct student)
typedef struct student
{
int num;
char name[20];
int score;
struct student *next;
}student;
struct student * creat();
void print(struct student *);
struct student *head;
struct student *temp,*last;
//--------------------------------
void main()
{
printf( "请输入数据:\n ");
head=creat();
print(head);
}
//-------------------------------------
struct student *creat()
{
int n=1;
last=temp=(struct student *)malloc(size);
head=NULL;
printf( "请输入第%d个学生的学号: ",n);
scanf( "%d ",&temp-> num);
printf( "请输入第%d个学生的姓名: ",n);
scanf( "%s ",temp-> name);
printf( "请输入第%d个学生的分数: ",n);
scanf( "%d ",&temp-> score);
while(temp-> num!=0)
{
if(n==1)
{
head=temp;
n++;
}
else
{
temp=(struct student *)malloc(size);
printf( "请输入第%d个学生的学号: ",n);
scanf( "%d ",&temp-> num);
printf( "请输入第%d个学生的姓名: ",n);
scanf( "%s ",temp-> name);
printf( "请输入第%d个学生的分数: ",n);
scanf( "%d ",&temp-> score);
last-> next=temp;
last=temp;
n++;
}
temp-> next=NULL;
}
return(head);
}
//------------------------------------
void print(struct student *head)
{
temp=head;
do
{
printf( "%d,%s,%d ",temp-> num,temp-> name,temp-> score);
temp=temp-> next;
}while(temp-> next!=NULL);
}