数据结构——练习之学生信息操作

ゝ一纸荒年。 2022-07-03 12:20 226阅读 0赞

数据结构——练习之学生信息操作

实现的功能如下:

20170408121932874

源码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. /**定义学生数据对象的结构体**/
  6. struct stu_node{
  7. char name[10];
  8. int score;
  9. struct stu_node *next;
  10. };
  11. typedef struct stu_node Student; //将结构体别名
  12. /**函数体声明**/
  13. Student* CreateList(int n);
  14. void PrintList(Student* s);
  15. int InsertList(Student* s,int i,char name[],int score,int n);
  16. int FindList(Student* s,char name[]);
  17. void DeleteList(Student* s,int j);
  18. void ChangeList(Student* s,int j,char name[],int score);
  19. /**创建链表**/
  20. Student* CreateList(int n){
  21. Student *head;
  22. Student *pre,*p; //pre指向当前元素,p指向前一个元素
  23. int i;
  24. head = (Student *)malloc(sizeof(Student)); //分配空间
  25. head->next = NULL; //创建空链表
  26. pre = head;
  27. for(i = 0;i<n;i++){
  28. printf("input name of the %d student:",i);
  29. p = (Student *)malloc(sizeof(Student));//为当前元素分配空间
  30. scanf("%s",&p->name);
  31. printf("input score of the %d student:",i);
  32. scanf("%d",&p->score);
  33. pre->next = p;
  34. pre = p;
  35. }
  36. p->next = NULL;
  37. PrintList(head);
  38. return head;
  39. }
  40. /**输出链表**/
  41. void PrintList(Student* s){
  42. Student *p;
  43. p = s->next;
  44. while(p){
  45. printf("%s,%d",p->name,p->score);
  46. p = p->next;
  47. printf("\n");
  48. }
  49. }
  50. /**插入元素**/
  51. int InsertList(Student* s,int i,char name[],int score,int n){
  52. int j = 0;
  53. Student *pre,*ne;
  54. pre = s;
  55. if(i<1||i>n+1){
  56. printf("Error!");
  57. }else{
  58. while(j<i-1){
  59. pre = pre->next;
  60. j++;
  61. }
  62. ne = (Student *)malloc(sizeof(Student));
  63. strcpy(ne->name,name);
  64. ne->score = score;
  65. ne->next = pre->next;
  66. pre->next = ne;
  67. n++;
  68. }
  69. PrintList(s);
  70. return n;
  71. }
  72. /**按名字查找结点**/
  73. int FindList(Student* s,char name[]){
  74. Student *pre = s;
  75. int i =0;
  76. while(pre){
  77. if(strcmp(pre->name,name)==0){
  78. return i;
  79. }else{
  80. i++;
  81. pre = pre->next;
  82. }
  83. }
  84. return -1;
  85. }
  86. /**删除节点**/
  87. void DeleteList(Student* s,int j){
  88. Student *pre = s;
  89. int i = 0;
  90. while(i<j-1){
  91. pre = pre->next;
  92. i++;
  93. }
  94. Student *p = pre->next;
  95. pre->next = p->next;
  96. printf("Deleted student (%s,%d)\n",p->name,p->score);
  97. free(p);
  98. PrintList(s);
  99. }
  100. /**改变链表节点**/
  101. void ChangeList(Student* s,int j,char name[],int score){
  102. Student *pre = s;
  103. int i = 0;
  104. while(i<j){
  105. pre = pre->next;
  106. i++;
  107. }
  108. printf("Changed student (%s,%d) to (%s,%d)\n",pre->name,pre->score,name,score);
  109. strcpy(pre->name,name);
  110. pre->score = score;
  111. PrintList(s);
  112. }
  113. void main(){
  114. Student *student;
  115. int i =1,n=0,j;
  116. char name[10];
  117. char old_name[10];
  118. int score;
  119. while(i){
  120. printf("1--建立信息表\t");
  121. printf("2--更改信息表\n");
  122. printf("3--查找信息表\t");
  123. printf("4--删除学生信息\n");
  124. printf("5--打印信息表\t");
  125. printf("6--添加学生信息\n");
  126. scanf("%d",&i);
  127. switch (i)
  128. {
  129. case 1:
  130. printf("Input the number of students\t");
  131. scanf("%d",&n);
  132. if(n<0){
  133. printf("Error!\n");
  134. break;
  135. }
  136. student = CreateList(n);
  137. break;
  138. case 2:
  139. printf("Input the name of student to change:");
  140. scanf("%s",&old_name);
  141. j =FindList(student,old_name);
  142. if(j==-1){
  143. printf("Can't Find student %s\n",old_name);
  144. }else{
  145. printf("Input the student's name\t");
  146. scanf("%s",&name);
  147. printf("Input the student's score\t");
  148. scanf("%d",&score);
  149. ChangeList(student,j,name,score);
  150. }
  151. break;
  152. case 3:
  153. printf("Input the name of student to find:");
  154. scanf("%s",&old_name);
  155. j =FindList(student,old_name);
  156. if(j==-1){
  157. printf("Can't Find student %s\n",old_name);
  158. }else{
  159. printf("The student's position is %d\n",j);
  160. }
  161. break;
  162. case 4:
  163. printf("Input the name of student to delete:");
  164. scanf("%s",&old_name);
  165. j =FindList(student,old_name);
  166. if(j==-1){
  167. printf("Can't Find student %s\n",old_name);
  168. }else{
  169. DeleteList(student,j);
  170. n--;
  171. }
  172. break;
  173. case 5:
  174. if(n>0)
  175. PrintList(student);
  176. else
  177. printf("The list is empty\n");
  178. break;
  179. case 6:
  180. printf("Input the student's name\t");
  181. scanf("%s",&name);
  182. printf("Input the student's score\t");
  183. scanf("%d",&score);
  184. printf("Input the position to insert:");
  185. int p;
  186. scanf("%d",&p);
  187. n = InsertList(student,p,name,score,n);
  188. break;
  189. default:
  190. printf("Please choose from the menu!");
  191. break;
  192. }
  193. }
  194. }

发表评论

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

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

相关阅读