|
1鱼币
#include "stdio.h"
#include "malloc.h"
#define Null 0
#define LEN sizeof(struct student)
int n;
struct student //定义结构
{
int num;
float score;
struct student *next;
};
struct student * creat(void) //定义一个指针函数 结果返还为地址
{
struct student *head,*p1,*p2; //定义一个结构指针 为head p1 p2
n=0;
p1=p2=(struct student *) malloc(LEN);//开辟新结点 使p1 p2指向新开辟结点的头地址
printf("please input two numbers:");
scanf("%ld%lf",&p1->num,&p1->score);
head=NULL; //将头指针置0或者清空
while(p1->num!=NULL) //判断条件,当p1->num不为0(NULL)时执行循环
{
n=n+1;
if(n==1)
{
head=p1; //为1时 使head指针指向p1
}
else p2->next=p1; //把p2->next指向p1
p2=p1;
p1=(struct student *) malloc(LEN); //再次开辟一个新结点
scanf("%ld,%lf",&p1->num,&p2->score);
}
p2->next=NULL; //p2的指针 指向null
return(head); //把head 的地址返回去
}
void print(struct student *head)
{ struct student *p; //定义一个结构指针p
printf("These is NUM%d records are:\n ",n);
p=head; //把头地址给予p
while(p!=NULL) //当p不等于0时 输出 num 和score
{
printf("%ld %lf\n",p->num,p->score);
p=p->next; //指向下一个结点
}
}
void main()
{
struct student *head;
head=creat();
print(head);
}
结果是错的 我不明白 我错在哪里 我觉得语句都好像是对的啊 求大侠指点下,还有就是 我注释的有错误吗?我是这么理解的,有错误请指出 感激涕零啊
|
最佳答案
查看完整内容
你主要的错误应该就是在输入整形的时候 输入格式写错了 整形是 %d 长整形才是 %ld
|