例题9.9

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:21   3346   0
#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;
  else
  p2->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)就不对呢 ;
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP