鱼C论坛

 找回密码
 立即注册
查看: 1551|回复: 3

[已解决]关于修改链表里面某个元素的内容

[复制链接]
发表于 2019-3-21 23:55:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 体弱多病程序权 于 2019-3-22 00:06 编辑

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct Student *head = NULL;

  5. int count = 1;

  6. struct Student
  7. {
  8.         int number;
  9.         char *name[4];
  10.         float score;
  11.         struct Student *next;
  12. };

  13. void create(struct Student **head);
  14. void print(struct Student *head);
  15. void change(struct Student *head);
  16. void releaseStorager(struct Student **head);

  17. int main(void){
  18.         char menu[][40] =
  19.         {
  20.         "\t****************************\n",       
  21.         "\t\t学生成绩管理系\n",
  22.         "\t\t1.录入学生成绩\n",
  23.         "\t\t2.打印学生成绩\n",
  24.         "\t\t3.修改学生成绩\n",
  25.         "\t****************************\n",
  26.         };
  27.         while(1)
  28.         {
  29.                 system("cls");
  30.                 for(int i = 0;i<sizeof(menu)/sizeof(menu[0]);i++)
  31.                 {               
  32.                           printf("%s",menu[i]);
  33.                 }
  34.                 char input;
  35.                 fflush(stdin);
  36.                 input = getchar();
  37.                 switch(input)
  38.                 {
  39.                         case '1':create(&head);break;
  40.                         case '2':print(head);break;
  41.                         case '3':change(head);break;       
  42.                         case '4':exit(1);
  43.                 }
  44.         }
  45. }

  46. void create(struct Student **head){
  47.         system("cls");
  48.         struct Student *p1,*p2;
  49.         p1 = p2 = (struct Student *)malloc(sizeof(struct Student));
  50.         if(p1 == NULL)
  51.         {
  52.                 printf("分配内存失败了.....\n");
  53.                 exit(1);
  54.         }
  55.         do{
  56.                 printf("第%d个学生\n",count);
  57.                 printf("请输入学生的学号:");
  58.                 scanf("%d",&p1->number);
  59.                 printf("请输入学生的姓名:");
  60.                 scanf("%s",&p1->name);
  61.                 printf("请输入学生的成绩:");
  62.                 scanf("%f",&p1->score);
  63.                 system("cls");
  64.                 if(*head != NULL)
  65.                 {
  66.                         p2->next = p1;
  67.                         p1->next = NULL;
  68.                 }
  69.                 else
  70.                 {
  71.                         *head = p1;
  72.                         p1->next = NULL;
  73.                 }
  74.                 count++;
  75.                 p2 = p1;
  76.                 p1 = (struct Student *)malloc(sizeof(struct Student));
  77.         }while(count != 3);
  78.         p2->next = NULL;
  79. }

  80. void print(struct Student *head)
  81. {
  82.                 system("cls");
  83.                 printf("学生成绩如下:\n");
  84.                 while(head != NULL)
  85.                 {
  86.                         printf("%d %s %f\n",head->number,head->name,head->score);
  87.                         head = head->next;
  88.                 }
  89.                 system("pause");
  90. }

  91. void change(struct Student *head){
  92.         system("cls");
  93.         int temp = 0;
  94.         printf("请输入要修改的学生学号:");
  95.         scanf("%d",&temp);
  96.         struct Student *p1,*p2;
  97.         p1 = p2 =  (struct Student *)malloc(sizeof(struct Student));
  98.         p1 = head;
  99.         while(1){
  100.                 if( p1 == NULL )
  101.                 {
  102.                         printf("This list is null!\n");
  103.                         system("pause");
  104.             break;
  105.                 }
  106.                 else if(p1->number == temp )
  107.                 {
  108.                         printf("查询该学生信息如下:\n");
  109.                         printf("%d %s %f",p1->number,p1->name,p1->score);
  110.                         printf("\n请重新输入该学生的信息:");
  111.                         scanf("%d %s %f",&p2->number,&p2->name,&p2->score);
  112.                         p1 = p2;
  113.                         printf("Change ID:%d succeed!",temp);
  114.                         system("pause");
  115.                         break;
  116.                 }
  117.                 else if(p1->number != temp && p1->next == NULL)
  118.                 {
  119.                         printf("\n您输入的学号有误,请核对后重新输入.");
  120.                         system("pause");                       
  121.                         break;
  122.                 }
  123.                 p1 = p1->next;
  124.         }
  125.         printf("修改后的成绩如下:\n");
  126.         print(head);
  127. }

  128. void releaseStorager(struct Student **head){


  129. }
复制代码


录入和打印功能正常,就是修改功能出问题了,如图,程序直接报错了......
希望能人相助,帮助迷途的羊羔....
最佳答案
2019-3-22 02:44:15
本帖最后由 jackz007 于 2019-3-22 09:58 编辑

    楼主的代码逻辑太混乱了,实在看不下去,帮你重写了代码,但基本维持原来的框架:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct Student {
  5.         int number            ;
  6.         char name[20]         ;
  7.         float score           ;
  8.         struct Student * next ;
  9. } node                        ;

  10. node * head = NULL            ;
  11. int count = 0                 ;

  12. bool find(node ** px , node * head , const int number)   // 寻找学号为 number 节点的前一个节点
  13. {
  14.         node * p1 , * p2                        ;
  15.         bool ret                                ;
  16.         
  17.         ret = false                             ;
  18.         if(count > 0) {
  19.                 p1 = p2 = head                  ;
  20.                 while(p2 != NULL) {
  21.                         if (p2 -> number == number) {
  22.                                 * px = p1       ;        // 返回指向目标节点前一个节点的指针
  23.                                 ret = true      ;
  24.                                 break           ;
  25.                         } else {
  26.                                 p1 = p2         ;
  27.                                 p2 = p1 -> next ;                        
  28.                         }
  29.                 }
  30.         }
  31.         return ret                              ;        // 返回布尔变量指示是否找到目标节点
  32. }

  33. void print(node * head)
  34. {
  35.         node * p                                                                   ;
  36.         system("cls")                                                              ;
  37.         if(count > 0) {
  38.                 printf("学生成绩如下:\n")                                          ;
  39.                 p = head                                                           ;
  40.                 while(p != NULL) {
  41.                         printf("%d %s %f\n", p -> number , p -> name , p -> score) ;
  42.                         p = p -> next                                              ;
  43.                 }
  44.         } else {
  45.                 printf("链表为空!\n")                                             ;
  46.         }
  47.         system("pause")                                                            ;
  48. }

  49. node * create(node * hd)
  50. {
  51.         node * p1 , * p2 , * px , * head                                      ;
  52.         int number                                                            ;
  53.         float score                                                           ;
  54.         char name[20]                                                         ;

  55.         system("cls")                                                         ;
  56.         p1 = head = hd                                                        ;
  57.         if(p1 != NULL) {
  58.                 while(p1 -> next != NULL) p1 = p1 -> next                     ; // p1 指向当前链表的最后一个节点
  59.         } else {
  60.                 p1 = p2 = head = NULL                                         ;
  61.         }
  62.         for(;;) {
  63.                 printf("第 %d 个学生\n" , count + 1)                          ;  
  64.                 printf("请输入学生的学号:")                                   ;
  65.                 scanf("%d" , & number)                                        ;
  66.                 if (! number) break                                           ;
  67.                 if (! find(& px , head , number)) {
  68.                         printf("请输入学生的姓名:")                           ;
  69.                         scanf("%s" , name)                                    ;
  70.                         printf("请输入学生的成绩:")                           ;
  71.                         scanf("%f" , & score)                                 ;
  72.                         system("cls")                                         ;
  73.                         if ((p2 = (node *)malloc(sizeof(node))) != NULL) {
  74.                                 p2 -> number = number                         ;
  75.                                 strcpy(p2 -> name , name)                     ;
  76.                                 p2 -> score = score                           ;
  77.                                 p2 -> next = NULL                             ;
  78.                                 if(! count) head = p2                         ;
  79.                                 else p1 -> next = p2                          ;
  80.                                 p1 = p2                                       ;
  81.                                 count ++                                      ;  
  82.                         } else {
  83.                                 fprintf(stderr , "malloc() 分配内存失败")     ;
  84.                                 break                                         ;
  85.                         }
  86.                 } else {
  87.                         printf("抱歉,学号为 %d 的学生已经存在!\n" , number) ;
  88.                 }
  89.         }
  90.         return head                                                           ;
  91. }

  92. void change(node * head)
  93. {
  94.         node * p1 , * p2 , * px                                                      ;
  95.         int temp                                                                     ;

  96.         system("cls")                                                                ;
  97.         if(count > 0) {
  98.                 printf("请输入要修改的学生学号 : ")                                  ;
  99.                 scanf("%d", & temp)                                                  ;
  100.                 if (find(& p1 , head , temp)) {
  101.                         if(p1 != head) p2 = p1 -> next                               ; // 如果找到的节点不是首节点 head
  102.                         else p2 = p1                                                 ; // 否则,p1、p2 都指向 head
  103.                         printf("查询该学生信息如下:\n")                              ;
  104.                         printf("%d %s %f" , p2 -> number , p2 -> name , p2 -> score) ;
  105.                         printf("\n请重新输入该学生的信息:")                          ; // 学号就不可以修改了吧。
  106.                         scanf("%s %f",  p2 -> name , & p2 -> score)                  ;
  107.                         printf("Change ID:%d succeed!" , temp)                       ;
  108.                         system("pause")                                              ;
  109.                 } else {
  110.                         printf("抱歉:没有查询到学号为 %d 的学生\n" , temp)          ;
  111.                 }
  112.                 printf("修改后的成绩如下:\n")                                        ;
  113.                 print(head)                                                          ;
  114.         } else {
  115.                 printf("链表为空!\n")                                               ;
  116.         }
  117. }

  118. int main(void)
  119. {
  120.         char menu[][40] = {
  121.         "\t****************************\n",        
  122.         "\t\t学生成绩管理系\n",
  123.         "\t\t1.录入学生成绩\n",
  124.         "\t\t2.打印学生成绩\n",
  125.         "\t\t3.修改学生成绩\n",
  126.         "\t****************************\n",
  127.         }                                                                                     ;
  128.         node * head                                                                           ;
  129.         char input                                                                            ;
  130.         int i                                                                                 ;
  131.         bool f                                                                                ;
  132.         f = true                                                                              ;
  133.         count = 0                                                                             ;
  134.         head = NULL                                                                           ;
  135.         while(f) {
  136.                 system("cls")                                                                 ;
  137.                 for(i = 0 ; i < sizeof(menu) / sizeof(menu[0]) ; i ++) printf("%s" , menu[i]) ;
  138.                 fflush(stdin)                                                                 ;
  139.                 input = getchar()                                                             ;
  140.                 switch(input) {
  141.                         case '1': head = create(head) ; break                                 ;
  142.                         case '2': print(head)         ; break                                 ;
  143.                         case '3': change(head)        ; break                                 ;        
  144.                         case '4': f = false           ;                                       ;
  145.                 }
  146.         }
  147. }
复制代码
QQ图片20190321235441.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-22 00:09:45 From FishC Mobile | 显示全部楼层
因为刚刚学习,如果大家有什么C方面的建议可以告诉我,如格式是否规范,函数内容因该如何如何,指针使用是否正确等等,望大家不吝啬,尽情的吐槽,记得留下样板让我模仿....别吐槽就溜........
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-22 00:10:17 From FishC Mobile | 显示全部楼层
我爱编程、爱fishc
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-22 02:44:15 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2019-3-22 09:58 编辑

    楼主的代码逻辑太混乱了,实在看不下去,帮你重写了代码,但基本维持原来的框架:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct Student {
  5.         int number            ;
  6.         char name[20]         ;
  7.         float score           ;
  8.         struct Student * next ;
  9. } node                        ;

  10. node * head = NULL            ;
  11. int count = 0                 ;

  12. bool find(node ** px , node * head , const int number)   // 寻找学号为 number 节点的前一个节点
  13. {
  14.         node * p1 , * p2                        ;
  15.         bool ret                                ;
  16.         
  17.         ret = false                             ;
  18.         if(count > 0) {
  19.                 p1 = p2 = head                  ;
  20.                 while(p2 != NULL) {
  21.                         if (p2 -> number == number) {
  22.                                 * px = p1       ;        // 返回指向目标节点前一个节点的指针
  23.                                 ret = true      ;
  24.                                 break           ;
  25.                         } else {
  26.                                 p1 = p2         ;
  27.                                 p2 = p1 -> next ;                        
  28.                         }
  29.                 }
  30.         }
  31.         return ret                              ;        // 返回布尔变量指示是否找到目标节点
  32. }

  33. void print(node * head)
  34. {
  35.         node * p                                                                   ;
  36.         system("cls")                                                              ;
  37.         if(count > 0) {
  38.                 printf("学生成绩如下:\n")                                          ;
  39.                 p = head                                                           ;
  40.                 while(p != NULL) {
  41.                         printf("%d %s %f\n", p -> number , p -> name , p -> score) ;
  42.                         p = p -> next                                              ;
  43.                 }
  44.         } else {
  45.                 printf("链表为空!\n")                                             ;
  46.         }
  47.         system("pause")                                                            ;
  48. }

  49. node * create(node * hd)
  50. {
  51.         node * p1 , * p2 , * px , * head                                      ;
  52.         int number                                                            ;
  53.         float score                                                           ;
  54.         char name[20]                                                         ;

  55.         system("cls")                                                         ;
  56.         p1 = head = hd                                                        ;
  57.         if(p1 != NULL) {
  58.                 while(p1 -> next != NULL) p1 = p1 -> next                     ; // p1 指向当前链表的最后一个节点
  59.         } else {
  60.                 p1 = p2 = head = NULL                                         ;
  61.         }
  62.         for(;;) {
  63.                 printf("第 %d 个学生\n" , count + 1)                          ;  
  64.                 printf("请输入学生的学号:")                                   ;
  65.                 scanf("%d" , & number)                                        ;
  66.                 if (! number) break                                           ;
  67.                 if (! find(& px , head , number)) {
  68.                         printf("请输入学生的姓名:")                           ;
  69.                         scanf("%s" , name)                                    ;
  70.                         printf("请输入学生的成绩:")                           ;
  71.                         scanf("%f" , & score)                                 ;
  72.                         system("cls")                                         ;
  73.                         if ((p2 = (node *)malloc(sizeof(node))) != NULL) {
  74.                                 p2 -> number = number                         ;
  75.                                 strcpy(p2 -> name , name)                     ;
  76.                                 p2 -> score = score                           ;
  77.                                 p2 -> next = NULL                             ;
  78.                                 if(! count) head = p2                         ;
  79.                                 else p1 -> next = p2                          ;
  80.                                 p1 = p2                                       ;
  81.                                 count ++                                      ;  
  82.                         } else {
  83.                                 fprintf(stderr , "malloc() 分配内存失败")     ;
  84.                                 break                                         ;
  85.                         }
  86.                 } else {
  87.                         printf("抱歉,学号为 %d 的学生已经存在!\n" , number) ;
  88.                 }
  89.         }
  90.         return head                                                           ;
  91. }

  92. void change(node * head)
  93. {
  94.         node * p1 , * p2 , * px                                                      ;
  95.         int temp                                                                     ;

  96.         system("cls")                                                                ;
  97.         if(count > 0) {
  98.                 printf("请输入要修改的学生学号 : ")                                  ;
  99.                 scanf("%d", & temp)                                                  ;
  100.                 if (find(& p1 , head , temp)) {
  101.                         if(p1 != head) p2 = p1 -> next                               ; // 如果找到的节点不是首节点 head
  102.                         else p2 = p1                                                 ; // 否则,p1、p2 都指向 head
  103.                         printf("查询该学生信息如下:\n")                              ;
  104.                         printf("%d %s %f" , p2 -> number , p2 -> name , p2 -> score) ;
  105.                         printf("\n请重新输入该学生的信息:")                          ; // 学号就不可以修改了吧。
  106.                         scanf("%s %f",  p2 -> name , & p2 -> score)                  ;
  107.                         printf("Change ID:%d succeed!" , temp)                       ;
  108.                         system("pause")                                              ;
  109.                 } else {
  110.                         printf("抱歉:没有查询到学号为 %d 的学生\n" , temp)          ;
  111.                 }
  112.                 printf("修改后的成绩如下:\n")                                        ;
  113.                 print(head)                                                          ;
  114.         } else {
  115.                 printf("链表为空!\n")                                               ;
  116.         }
  117. }

  118. int main(void)
  119. {
  120.         char menu[][40] = {
  121.         "\t****************************\n",        
  122.         "\t\t学生成绩管理系\n",
  123.         "\t\t1.录入学生成绩\n",
  124.         "\t\t2.打印学生成绩\n",
  125.         "\t\t3.修改学生成绩\n",
  126.         "\t****************************\n",
  127.         }                                                                                     ;
  128.         node * head                                                                           ;
  129.         char input                                                                            ;
  130.         int i                                                                                 ;
  131.         bool f                                                                                ;
  132.         f = true                                                                              ;
  133.         count = 0                                                                             ;
  134.         head = NULL                                                                           ;
  135.         while(f) {
  136.                 system("cls")                                                                 ;
  137.                 for(i = 0 ; i < sizeof(menu) / sizeof(menu[0]) ; i ++) printf("%s" , menu[i]) ;
  138.                 fflush(stdin)                                                                 ;
  139.                 input = getchar()                                                             ;
  140.                 switch(input) {
  141.                         case '1': head = create(head) ; break                                 ;
  142.                         case '2': print(head)         ; break                                 ;
  143.                         case '3': change(head)        ; break                                 ;        
  144.                         case '4': f = false           ;                                       ;
  145.                 }
  146.         }
  147. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 21:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表