遇到0就结束,并且输出第一个人的成绩
讲解:
1.首先建立结构体:
struct student{
int num;
float cj;
struct student *next;
};
2.用三个结构体指针(head,p1,p2)和开辟的动态存储区(mly)存储数据
让p1,p2都指向第一次开辟的动态存储区(mly);head存为NULL;
第一次输入数据后,n=1,将该地址存给head;
之后n不再等于1;
将p1指向第二次开辟的动态存储区(mly),p2依旧指向第一个mly,等第二个mly数据输入完成后,让p2指向第二个mly,让p1指向第三次开辟的动态存储区(mly);以此类推
#include<stdio.h>
#include<stdlib.h>
#define mly sizeof(struct student)
struct student{
int num;
float cj;
struct student *next;
};
int n;
struct student *fgo()
{
struct student *head,*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(mly);
scanf("%d %f",&p1->num,&p1->cj);
head=NULL;
while(p1->num!=0)
{
n+=1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(mly);
scanf("%d %f",&p1->num,&p1->cj);
}
p2->next=NULL;
return(head);
}
int main()
{
struct student *pt;
pt=fgo();
printf("\nnum:%d\n成绩:%5.1f\n",pt->num,pt->cj);
}