鱼C论坛

 找回密码
 立即注册
查看: 1612|回复: 6

[已解决]求大佬看看这个程序的哪里想法有问题吧

[复制链接]
发表于 2019-3-24 16:18:40 | 显示全部楼层 |阅读模式

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

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

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

  4. struct students{
  5.         char name[10];
  6.         int num;
  7.         float score;
  8.         struct students *next;       
  9. };


  10. int count=0;
  11. int *p = &count;

  12. void getInfo(struct students *);
  13. int addStu(struct students **,int );
  14. int delStu(struct students **,int );
  15. int searchStu(struct students *,int );
  16. void showList(struct students *);

  17. int main(){
  18.         struct students *S = NULL;//生成一个头节点
  19.         int op,i;
  20.         do{
  21.         printf("welcome to students' information manage system!\n");
  22.         printf("please chose your operation:\n");
  23.         printf("1 add a student to specified location\n");
  24.         printf("2 delete a student from the specified location\n");
  25.         printf("3 find a student through his number\n");
  26.         printf("4 show the list:\n");
  27.         printf("0 exit\n");
  28.         scanf("%d",&op);
  29.                 switch(op){
  30.                         case 1:printf("input the num you want to insert:");scanf("%d",&i);addStu(&S,i);break;
  31.                         case 2:printf("input the num you want to delete:");scanf("%d",&i);delStu(&S,i);break;
  32.                         case 3:printf("input his num:");scanf("%d",&i);searchStu(S,i);break;
  33.                         case 4:showList(S);break;
  34.                 }
  35.         }while(op != 0);
  36.        
  37.         return 0;
  38. }
  39. void getInfo(struct students *s){
  40.         printf("input students's name :\n");
  41.         scanf("%s",s->name);
  42.         printf("input students's num and score:\n");
  43.         scanf("%d%f",&s->num,&s->score);
  44. }
  45. int addStu(struct students **S1,int n){
  46.         struct students *ns,*temp;
  47.         int i;
  48.         if(n < 0 || n > count+1){
  49.                 printf("error:invalid num");
  50.                 return 1;
  51.         }
  52.         ns = (struct students *)malloc(sizeof(struct students));
  53.         if(ns == NULL){
  54.                 printf("menory allocation error!");
  55.         }
  56.         getInfo(ns);
  57.         for(i = 0;i < n-1;i++){
  58.                 *S1 = (*S1)->next;
  59.         }
  60.         if(*S1 !=  NULL){
  61.                 temp = *S1;
  62.                 *S1 = ns;
  63.                 ns->next = temp;
  64.         }
  65.         else{
  66.                 *S1 = ns;
  67.                 ns->next = NULL;
  68.         }
  69.         (*p)++;
  70.         return 0;
  71. }
  72. int delStu(struct students **S1,int n){
  73.         struct students *temp; //用来接收旧的结构体节点,并准备释放内存
  74.         int i;
  75.         if(n > count || n < 0){
  76.                 printf("error!");
  77.                 return 1;
  78.         }
  79.         for(i = 0;i < n-1;i++){
  80.                 *S1 = (*S1)->next;
  81.         }
  82.         temp = *S1;
  83.         *S1 = temp->next;
  84.         free(temp);
  85.         printf("delete complete");
  86.         (*p)--;
  87.         return 0;
  88. }
  89. int searchStu(struct students *S1,int n){
  90.         int i;
  91.         for(i = 0;i < count;i++){
  92.                 if(S1->num == n){
  93.                         printf("he/she is the %d student");
  94.                         return i+1;
  95.                 }
  96.                 S1 = S1->next;
  97.         }
  98.         if(i > count){
  99.                         printf("can't find this student");
  100.                         return 0;
  101.                 }
  102. }
  103. void showList(struct students *S1){
  104.         int i;
  105.         struct students *s;
  106.         s = S1;
  107.         if((*p) == 0){
  108.                 printf("no student in LinkList");
  109.         }
  110.         while(s != NULL){
  111.         printf("name\tnum\tscore\n");
  112.         printf("%s\t%d\t%f\n",s->name,s->num,s->score);
  113.         s = s->next;
  114.         }
  115.        
  116. }
复制代码

插到第几个,第几个前面的就没有了,插到最后一个整个链表都没了
救救孩子吧
最佳答案
2019-3-25 17:56:18
本帖最后由 jackz007 于 2019-3-25 18:09 编辑

        addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普通变量变来变去,而这个变量 S1 又是通过地址传入,在函数中所有的改变都会映射到主函数中去,而在主函数中,却一直认为这个 S1 是链表首节点指针,从而造成严重混乱。
        我贴出来的代码是这次才精心编写的,没有照抄任何人的东西,你前面看见相似的代码估计也是我给别人的回帖。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-24 18:54:31 | 显示全部楼层
想法很好
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-24 20:17:07 | 显示全部楼层
      找问题实在是苦差事,我按楼主的程序框架重写了全部代码,楼主可以自己比对差别。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct students {
  5.         char name[10]          ;
  6.         int num                ;
  7.         float score            ;
  8.         struct students * next ;        
  9. }                              ;

  10. int count = 0                  ;

  11. struct students * findStu(struct students * hd , const int num)
  12. {
  13.         struct students * p1 , * p2 , * ret     ;
  14.         bool f                                  ;
  15.         ret = NULL                              ;
  16.         if(count > 0 && hd != NULL) {
  17.                 for(f = false , p1 = p2 = hd ; p2 != NULL && ! f ;) {
  18.                         if(p2 -> num == num) {
  19.                                 f = true        ;
  20.                         } else {
  21.                                 p1 = p2         ;
  22.                                 p2 = p1 -> next ;
  23.                         }
  24.                 }
  25.                 if(f) ret = p1                  ;
  26.         }
  27.         return ret                              ;
  28. }

  29. struct students * addStu(struct students * hd)
  30. {
  31.         struct students * p1 , * p2 , * head                                                  ;
  32.         int num                                                                               ;

  33.         head = hd                                                                             ;
  34.         p1 = hd                                                                               ;
  35.         if(p1 != NULL) while(p1 -> next != NULL) p1 = p1 -> next                              ;
  36.         for(;;) {
  37.                 printf("\n")                                                                  ;
  38.                 printf("input student\'s num to add : ")                                      ;
  39.                 scanf("%d" , & num)                                                           ;
  40.                 if(! num) break                                                               ;
  41.                 if(findStu(head , num) == NULL) {
  42.                         if((p2 = (struct students *) malloc(sizeof(struct students))) != NULL) {
  43.                                 printf("input student\'s name : ")                            ;
  44.                                 scanf("%s" , p2 -> name)                                      ;
  45.                                 printf("input student\'s score: ")                            ;
  46.                                 scanf("%f" , & p2 -> score)                                   ;
  47.                                 p2 -> num = num                                               ;
  48.                                 p2 -> next = NULL                                             ;
  49.                                 if(! count) head = p2                                         ;
  50.                                 else p1 -> next = p2                                          ;
  51.                                 p1 = p2                                                       ;
  52.                                 count ++                                                      ;
  53.                         } else {
  54.                                 fprintf(stderr , "malloc() cann\'t  to alocate the memory !") ;
  55.                                 break                                                         ;
  56.                         }
  57.                 } else {
  58.                         printf("\n")                                                          ;
  59.                         printf("the student with num = %d already exists !\n" , num)          ;
  60.                 }
  61.         }
  62.         return head                                                                           ;
  63. }

  64. struct students * delStu(struct students * hd)
  65. {
  66.         struct students * p1 , * p2 , * head                                                                ;
  67.         int num                                                                                             ;
  68.         head = hd                                                                                           ;
  69.         if(count > 0 && hd != NULL) {
  70.                 while(count > 0) {
  71.                         printf("\n")                                                                        ;
  72.                         printf("input student\'s num to delete : ")                                         ;
  73.                         scanf("%d" , & num)                                                                 ;
  74.                         if(! num) break                                                                     ;
  75.                         if((p1 = findStu(head , num)) != NULL) {
  76.                                 if(p1 == head) {
  77.                                        head = head -> next                                                  ;
  78.                                        p2 = p1                                                              ;
  79.                                 } else {
  80.                                        p2 = p1 -> next                                                      ;
  81.                                        p1 -> next = p2 -> next                                              ;
  82.                                 }
  83.                                 free(p2)                                                                    ;
  84.                                 count --                                                                    ;
  85.                                 printf("the student with num = %d has been successfully deteted !\n" , num) ;
  86.                         } else {
  87.                                 printf("\n")                                                                ;
  88.                                 printf("the student with num = %d doesn\'t exists !\n" , num)               ;
  89.                         }
  90.                 }
  91.         } else {
  92.                 printf("\n")                                                                                ;
  93.                 printf("empty list !\n")                                                                    ;
  94.         }
  95.         return head                                                                                         ;
  96. }

  97. void searchStu(struct students * hd)
  98. {
  99.         struct students * p                                                                   ;
  100.         int num                                                                               ;

  101.         if(count > 0 && hd != NULL) {
  102.                 for(;;) {
  103.                         printf("\n")                                                          ;
  104.                         printf("input student\'s num to search : ")                           ;
  105.                         scanf("%d" , & num)                                                   ;
  106.                         if(! num) break                                                       ;
  107.                         if((p = findStu(hd , num)) != NULL) {
  108.                                 printf("p -> num = %d\n" , p -> num)                          ;
  109.                                 if(p != hd) p = p -> next                                     ;
  110.                                 printf("student's name  : %s\n"    , p -> name)               ;
  111.                                 printf("student's num   : %d\n"    , p -> num)                ;
  112.                                 printf("student's score : %5.2f\n" , p -> score)              ;
  113.                         } else {
  114.                                 printf("\n")                                                  ;
  115.                                 printf("the student with num = %d doesn\'t exists !\n" , num) ;
  116.                         }
  117.                 }
  118.         } else {
  119.                 printf("\n")                                                                  ;
  120.                 printf("empty list !\n")                                                      ;
  121.         }
  122. }

  123. void showList(struct students * hd)
  124. {
  125.         struct students * p                                               ;
  126.         if(count > 0 && hd != NULL) {
  127.                 p = hd                                                    ;
  128.                 while(p != NULL) {
  129.                         printf("\n")                                      ;
  130.                         printf("student\'s name  : %s\n"    , p -> name)  ;
  131.                         printf("student\'s num   : %d\n"    , p -> num)   ;
  132.                         printf("student\'s score : %5.2f\n" , p -> score) ;
  133.                         p = p -> next                                     ;
  134.                 }
  135.         } else {
  136.                 printf("\n")                                              ;
  137.                 printf("empty list !\n")                                  ;
  138.         }
  139. }

  140. int main()
  141. {
  142.         struct students * head                                               ;
  143.         int op                                                               ;
  144.         head = NULL                                                          ;
  145.         count = 0                                                            ;
  146.         do {
  147.                 printf("\n")                                                 ;
  148.                 printf("welcome to students\' information manage system!\n") ;
  149.                 printf("    1 add a student.\n")                             ;
  150.                 printf("    2 delete a student.\n")                          ;
  151.                 printf("    3 find a student.\n")                            ;
  152.                 printf("    4 show the list:\n")                             ;
  153.                 printf("    0 exit\n")                                       ;
  154.                 printf("\n")                                                 ;
  155.                 printf("       your choice : ")                              ;
  156.                 scanf("%d" , & op)                                           ;
  157.                 switch(op) {
  158.                         case 1 :
  159.                                 head = addStu(head)                          ;
  160.                                 break                                        ;
  161.                         case 2:
  162.                                 head = delStu(head)                          ;
  163.                                 break                                        ;
  164.                         case 3:
  165.                                 searchStu(head)                              ;
  166.                                 break                                        ;
  167.                         case 4:
  168.                                 showList(head)                               ;
  169.                                 break                                        ;
  170.                 }
  171.         } while(op != 0)                                                     ;
  172. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-25 17:24:34 | 显示全部楼层
jackz007 发表于 2019-3-24 20:17
找问题实在是苦差事,我按楼主的程序框架重写了全部代码,楼主可以自己比对差别。

我在网上又看到和你这个差不多的,就是看不懂才来论坛问的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-25 17:56:18 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2019-3-25 18:09 编辑

        addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普通变量变来变去,而这个变量 S1 又是通过地址传入,在函数中所有的改变都会映射到主函数中去,而在主函数中,却一直认为这个 S1 是链表首节点指针,从而造成严重混乱。
        我贴出来的代码是这次才精心编写的,没有照抄任何人的东西,你前面看见相似的代码估计也是我给别人的回帖。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-26 09:51:54 | 显示全部楼层
jackz007 发表于 2019-3-25 17:56
addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普 ...

哦 原来如此,难怪我看很多人写的都有另外的指针来替代头节点
谢谢大佬,受教了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-28 15:31:52 | 显示全部楼层
jackz007 发表于 2019-3-25 17:56
addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普 ...

大佬,这个程序还是不行啊,就算改用其他指针指向头节点,也不能写道这个头节点里啊
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct students{
  5.         char name[10];
  6.         int num;
  7.         float score;
  8.         struct students *next;       
  9. };


  10. int count=0;
  11. int *p = &count;

  12. void getInfo(struct students *);
  13. int addStu(struct students **);
  14. int delStu(struct students **,int );
  15. int searchStu(struct students *,int );
  16. void showList(struct students **);

  17. int main(){
  18.         struct students *S = NULL;//生成一个头节点
  19.         int op,i;
  20.         do{
  21.         printf("welcome to students' information manage system!\n");
  22.         printf("please chose your operation:\n");
  23.         printf("1 add a student to specified location\n");
  24.         printf("2 delete a student from the specified location\n");
  25.         printf("3 find a student through his number\n");
  26.         printf("4 show the list:\n");
  27.         printf("0 exit\n");
  28.         scanf("%d",&op);
  29.                 switch(op){
  30.                         case 1:printf("input the num you want to insert:");addStu(&S);break;
  31.                         case 2:printf("input the num you want to delete:");scanf("%d",&i);delStu(&S,i);break;
  32.                         case 3:printf("input his num:");scanf("%d",&i);searchStu(S,i);break;
  33.                         case 4:showList(&S);break;
  34.                 }
  35.         }while(op != 0);
  36.        
  37.         return 0;
  38. }
  39. void getInfo(struct students *s){
  40.         printf("input students's name :\n");
  41.         scanf("%s",s->name);
  42.         printf("input students's num and score:\n");
  43.         scanf("%d%f",&s->num,&s->score);
  44. }
  45. int addStu(struct students **S1){
  46.         struct students *ns,*temp,*ptr = *S1;
  47.         ns = (struct students *)malloc(sizeof(struct students));
  48.         if(ns == NULL){
  49.                 printf("menory allocation error!");
  50.         }
  51.         getInfo(ns);
  52.         if(ptr == NULL){
  53.                 ptr = ns;
  54.                 ns->next = NULL;
  55.                 (*p)++;
  56.         }
  57.         while(ptr->next != NULL && ptr->num < ns->num){
  58.                 temp = ptr;
  59.                 ptr = ptr->next;
  60.         }
  61.         if(ptr == *S1){
  62.                 temp = *S1;
  63.                 *S1 = ns;
  64.                 ns->next = temp;
  65.         }
  66.         else if(ptr->next == NULL){
  67.                 ptr->next = ns;
  68.                 ns->next = NULL;
  69.                
  70.         }
  71.         else{
  72.                 temp->next = ns;
  73.                 ns->next = ptr;
  74.         }
  75.         printf("name\tnum\tscore\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
  76.         return 0;
  77. }
  78. int delStu(struct students **S1,int n){
  79.         struct students *temp,*ptr = *S1; //用来接收旧的结构体节点,并准备释放内存
  80.         if(ptr->next == NULL){
  81.                 printf("can't delete student from an empty list\n");
  82.         }
  83.         while(ptr->num < n){
  84.                 ptr=ptr->next;
  85.         }
  86.         if(ptr == NULL){
  87.                 printf("can't find this student!\n");
  88.         }
  89.         if(ptr->num == n){
  90.                 temp = ptr;
  91.                 ptr = temp->next;
  92.                 free(temp);
  93.                 printf("delete complete\n");
  94.         }
  95.         return 0;
  96. }
  97. int searchStu(struct students *S1,int n){
  98.         int i;
  99.         while(S1 != NULL){
  100.                 if(S1->num == n){
  101.                         printf("he/she is the %d student");
  102.                         return i+1;
  103.                 }
  104.                 S1 = S1->next;
  105.         }
  106.         if(S1->next == NULL && S1->num != n){
  107.                         printf("can't find this student");
  108.                         return 0;
  109.                 }
  110. }
  111. void showList(struct students **S1){
  112.         if(*S1 == NULL){
  113.                 printf("no student in this list!\n");
  114.         }
  115.         else{
  116.                 printf("name\tnum\tscore\t\n");
  117.         }
  118.         while(*S1 != NULL){
  119.                 printf("%s\t%d\t%d\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
  120.                 *S1 = (*S1)->next;
  121.         }
  122.        
  123. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 15:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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