c9-9.c 787 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LEN sizeof(struct student)
  4. struct student
  5. {long num;
  6. float score;
  7. struct student *next;
  8. };
  9. int n;
  10. struct student *creat(void)
  11. {struct student *head;
  12. struct student *p1,*p2;
  13. n=0;
  14. p1=p2=(struct student *)malloc(LEN);
  15. scanf("%ld,%f",&p1->num,&p1->score);
  16. head=NULL;
  17. while(p1->num!=0)
  18. {n=n+1;
  19. if(n==1)head=p1;
  20. else p2->next=p1;
  21. p2=p1;
  22. p1=(struct student*)malloc(LEN);
  23. scanf("%ld,%f",&p1->num,&p1->score);
  24. }
  25. p2->next=NULL;
  26. return(head);
  27. }
  28. int main()
  29. { struct student *pt;
  30. pt=creat(); // 函数返回链表第一个结点的地址
  31. printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score); // 输出第一个结点的成员值
  32. return 0;
  33. };