例题9.9

来源:互联网 发布:mac os最新系统 编辑:程序博客网 时间:2024/04/27 12:06
#include<stdio.h>#include<stdlib.h>#define LEN sizeof(struct Student)struct Student{long num;float score;struct Student*next;} ;int n;struct Student*creat(){struct Student*head;struct Student*p1,*p2;n=0;//p1=p2=(struct Student*)malloc(LEN);p1=p2=(struct Student*)malloc  (sizeof(struct Student));scanf("%ld,%f",&p1->num,&p1->score);head=NULL;while(p1->num!=0){n=n+1;if(n==1)head=p1;elsep2->next=p1;p2=p1;//p1=(struct Student*)malloc(LEN);p1=(struct Student*)malloc (sizeof(struct Student)) ;scanf("%ld,%f",&p1->num,&p1->score);}p2->next=NULL;return (head);}int main(){struct Student*pt;pt=creat();printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score);return 0;}

创建一个链表的时候为什么开辟节点空间的时候会是p1=(struct Student*)malloc (sizeof(struct Student)) ;可以成功,但是用宏定义的p1=(struct Student*)malloc (LEN)就不对呢 ;