当前位置:   article > 正文

结构体链表 (创建)_结构体链表的创建

结构体链表的创建
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct student
  5. {
  6. int no;
  7. char name[21];
  8. double s[3];
  9. double ave;
  10. double sum;
  11. }stu;
  12. typedef struct node
  13. {
  14. stu date;
  15. struct node *next;
  16. }lnode;
  17. void input(lnode *p)
  18. {
  19. int i;
  20. int no;
  21. char nm[21];
  22. double fs[4];
  23. scanf("%d",&no);
  24. scanf("%s",nm);
  25. p->date.no=no;
  26. strcpy(p->date.name,nm);
  27. p->date.sum=0;
  28. for(i=0;i<3;i++)
  29. {
  30. scanf("%lf",&fs[i]);
  31. p->date.s[i]=fs[i];
  32. p->date.sum+=fs[i];
  33. }
  34. p->date.ave=p->date.sum/3;
  35. }
  36. void creat(lnode *h,int n)
  37. {
  38. lnode *p,*r;
  39. int i;
  40. r=h;
  41. for(i=0;i<n;i++)
  42. {
  43. p=(lnode *)malloc(sizeof(lnode));
  44. input(p);
  45. r->next=p;
  46. r=p;
  47. }
  48. r->next=NULL;
  49. }
  50. void output(lnode *h)
  51. {
  52. int i;
  53. lnode *p;
  54. p=h->next;
  55. while(p!=NULL)
  56. {
  57. printf("%d %s ",p->date.no,p->date.name);
  58. for(i=0;i<3;i++)
  59. {
  60. printf("%.2lf ",p->date.s[i]);
  61. }
  62. printf("%.2lf %.2lf\n",p->date.ave,p->date.sum);
  63. p=p->next;
  64. }
  65. }
  66. int main()
  67. {
  68. int n;
  69. lnode *h;
  70. while(scanf("%d",&n)!=-1)
  71. {
  72. h=(lnode *)malloc(sizeof(lnode));
  73. h->next=NULL;
  74. creat(h,n);
  75. output(h);
  76. }
  77. return 0;
  78. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号