链表找高手…………………………
#include<stdio.h>#include<stdlib.h>
#include<string.h>
struct student
{
long num;
char name[20];
float score;
struct student *next;
};
struct student *x();
void main()
{
struct student *p;
p=x();
while(p->num)
{
printf("%ld %s %f",p->num,p->name,p->score);
p=p->next;
}
}
struct student *x()
{
int size;
struct student *head,*tail,*p;
head=tail=NULL;
size=sizeof(struct student);
p=(struct student *)malloc(size);
p->next=NULL;
long num;
char name[20];
float score;
printf("请输入学生信息:\n");
scanf("%ld%s%f",&num,name,&score);
while(num){
if(p!=NULL){
p->num=num;
strcpy(p->name,name);
p->score=score;
}
else
{
printf("error!");
break;
}
if(head==NULL){
head=p;
}
else
{
tail->next=p;
tail=p;
}
printf("请再输入学生信息,学号0将结束:\n");
scanf("%ld",&num);
if(num==0)
continue;
else
{
scanf("%s%f",name,&score);
}
p=(struct student *)malloc(size);
}
return head;
}