首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

有一道c,关于结点的,小弟我用函数做,可是不知错在那里

2012-04-12 
有一道c,关于结点的,我用函数做,可是不知错在那里#include stdio.h#include stdlib.hstructstudent{in

有一道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);
}

热点排行