c语言链表:编写input()output()insert()delete()来输入学生成绩

拼搏现实的明天。 2022-12-31 13:25 207阅读 0赞
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Student
  4. {
  5. int studid; /*学号*/
  6. char name[10]; /*姓名*/
  7. int score1;
  8. int score2;
  9. int score3;
  10. int total; /*总成绩*/
  11. struct Student *next; /*指向下一个结点的指针*/
  12. };
  13. void inputlist(struct Student *phead)
  14. {
  15. struct Student *p;
  16. while(1)
  17. {
  18. p = (struct Student*)malloc(sizeof(struct Student));
  19. printf("请输入学生学号:");
  20. scanf("%d", &(p->studid));//相当于-1为终止输入符号
  21. if (p->studid==-1 )
  22. {
  23. free(p);
  24. break;
  25. }
  26. printf("请输入学生姓名:");
  27. scanf("%s", &(p->name));
  28. printf("请输入第一科成绩:");
  29. scanf("%d", &(p->score1));
  30. printf("请输入第二科成绩:");
  31. scanf("%d", &(p->score2)) ;
  32. printf("请输入第三科成绩:");
  33. scanf("%d", &(p->score3)) ;
  34. p->total = p->score1+ p->score2+p->score3;
  35. p->next = phead->next;//相当于p的尾结点指向了null
  36. phead->next = p;//相当于phead的尾节点从指向null指向了p的头节点
  37. printf("\n");
  38. printf("若想终止输入数据,请输入学号为-1");
  39. printf("\n");
  40. }
  41. }
  42. void print(struct Student *phead)
  43. {
  44. struct Student *p;
  45. p = (struct Student*)malloc(sizeof(struct Student));
  46. p= phead->next;
  47. printf("学号 姓名 第一科 第二科 第三科 总分 \n");
  48. while(p!=NULL)
  49. {
  50. printf("%d\t", p ->studid);
  51. printf("%s\t", p ->name);
  52. printf("%d\t", p ->score1);
  53. printf("%d\t", p ->score2);
  54. printf("%d\t", p ->score3);
  55. printf("%d\t", p ->total);
  56. printf("\n");
  57. p = p ->next;
  58. }
  59. }
  60. void max(struct Student *phead)
  61. {
  62. struct Student *p,*q;
  63. p=phead->next; //可以不用申请空间了
  64. q=p->next;//使得指向下一个链表
  65. while( (p->next)!=NULL)
  66. {
  67. if ((p->total)<=(q->total))
  68. {
  69. p=q;
  70. }
  71. q=q->next;
  72. }
  73. printf("\n");
  74. printf("平均分最高分的同学数据为:\n");
  75. printf("学号 姓名 第一科 第二科 第三科 总分 \n");
  76. printf("%d\t", p ->studid);
  77. printf("%s\t", p ->name);
  78. printf("%d\t", p ->score1);
  79. printf("%d\t", p ->score2);
  80. printf("%d\t", p ->score3);
  81. printf("%d\t", p ->total);
  82. }
  83. void insert(struct Student *phead)
  84. {
  85. struct Student *p;
  86. p=(struct Student*)malloc(sizeof(struct Student));
  87. printf("\n输入需要插入的学生数据\n");
  88. printf("学 号:");
  89. scanf("%d", &(p->studid));
  90. printf("请输入学生姓名:");
  91. scanf("%s", &(p->name));
  92. printf("请输入第一科成绩:");
  93. scanf("%d", &(p->score1));
  94. printf("请输入第二科成绩:");
  95. scanf("%d", &(p->score2));
  96. printf("请输入第三科成绩:");
  97. scanf("%d", &(p->score3));
  98. p->next = phead->next;
  99. phead->next = p;
  100. }
  101. void delete(struct Student *phead)
  102. {
  103. int dlt=0;
  104. struct Student *p,*q;
  105. p=phead;
  106. q=phead->next;
  107. printf("\n输入需要删除的学生数据的学号:");
  108. scanf("%d",&dlt);
  109. while(q!=NULL)
  110. {
  111. if(q->studid==dlt)
  112. {
  113. p->next=q->next;
  114. free(p);
  115. break;
  116. }
  117. p=q;
  118. q = p->next;
  119. }
  120. print(phead);
  121. }
  122. void main ()
  123. {
  124. struct Student *phead;
  125. phead =( struct Student *)malloc(sizeof(struct Student));//申请了一个结构体指针,该指针类型有尾标(phead->next),利用这个尾标来建立链表
  126. phead->next=NULL;
  127. inputlist(phead);
  128. print(phead);
  129. max(phead);
  130. insert (phead);
  131. delete(phead);
  132. }

发表评论

表情:
评论列表 (有 0 条评论,207人围观)

还没有评论,来说两句吧...

相关阅读

    相关 学生成绩处理PTA

    本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。 函数接口定义: struct stud_node c