编写一个CREATE()函数,按照规定的接点结构,创建一个单链表。
#define NULL 0
#define LEN sizeof(struct grade)
struct grade
{ char no[7];
int score;
struct grade *next;
};
struct grade *create( void )
{ struct grade *head=NULL, *new, *tail;
int count=0;
for( ; ; )
{ new=(struct grade *)malloc(LEN);
printf("Input the number of student No.%d(6 bytes): ", count+1);
scanf("%6s", new->no);
if(strcmp(new->no,"000000")==0)
{ free(new);
break; }
printf("Input the score of the student No.%d: ", count+1);
scanf("%d", &new->score);
count++;
new->next=NULL;
if(count==1) head=new;
else tail->next=new;
tail=new;
}
return(head);
}
#define NULL 0
#define LEN sizeof(struct grade)
struct grade
{ char no[7];
int score;
struct grade *next;
};
struct grade *create( void )
{ struct grade *head=NULL, *new, *tail;
int count=0;
for( ; ; )
{ new=(struct grade *)malloc(LEN);
printf("Input the number of student No.%d(6 bytes): ", count+1);
scanf("%6s", new->no);
if(strcmp(new->no,"000000")==0)
{ free(new);
break; }
printf("Input the score of the student No.%d: ", count+1);
scanf("%d", &new->score);
count++;
new->next=NULL;
if(count==1) head=new;
else tail->next=new;
tail=new;
}
return(head);
}
main()
{
struct gride *head;
head=create();
}
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=1207456