鱼C论坛

 找回密码
 立即注册
查看: 2506|回复: 1

关于结构体指针的问题。

[复制链接]
发表于 2019-4-6 13:43:42 | 显示全部楼层 |阅读模式

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

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

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. void 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 student information:");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. void addStu(struct students **S1){
  46.         struct students *ns,*temp,*head;
  47.         int flag = 0;
  48. //        struct students **move = S1;
  49.        
  50.         ns = (struct students *)malloc(sizeof(struct students));
  51.         if(ns == NULL){
  52.                 printf("menory allocation error!");
  53.                 exit(1);
  54.         }
  55.         getInfo(ns);
  56.         if(*S1 == NULL){ //空链表的操作;
  57.                 *S1=ns;
  58.                 ns->next=NULL;
  59.                 head = *S1;
  60.         }
  61.         else{
  62.                 while((*S1)->num<ns->num && (*S1)->next != NULL){//通过新学生的学号来移动move与temp指针,
  63.                         flag = 1;
  64.                         temp = (*S1);
  65.                         (*S1) = (*S1)->next;
  66.                 }
  67.                 if((*S1)->next == NULL && (*S1)->num<=ns->num){ //新学生的学号是最大的
  68.                         (*S1)->next = ns;
  69.                         ns->next = NULL;
  70.                 }
  71.                 else if(!flag){ //目前没有发生问题
  72.                         temp = *S1;
  73.                         (*S1) = ns;
  74.                         ns->next = temp;
  75.                 }
  76.                 else{ //学生学号刚好在中间
  77.                         temp->next = ns;
  78.                         ns->next = (*S1);       
  79.                 }
  80.         }
  81.         *S1 = head; // 问题很有可能在这里
  82. }
  83. int delStu(struct students **S1,int n){
  84.         struct students *temp;
  85.         struct students **ptr = S1; //用来接收旧的结构体节点,并准备释放内存
  86.         if((*ptr)->next == NULL){
  87.                 printf("can't delete student from an empty list\n");
  88.         }
  89.         while((*ptr)->num < n){
  90.                 (*ptr)=(*ptr)->next;
  91.         }
  92.         if((*ptr) == NULL){
  93.                 printf("can't find this student!\n");
  94.         }
  95.         if((*ptr)->num == n){
  96.                 temp = (*ptr);
  97.                 (*ptr) = temp->next;
  98.         //        free(temp);
  99.                 printf("delete complete\n");
  100.         }
  101.         return 0;
  102. }
  103. int searchStu(struct students *S1,int n){
  104.         int i = 0;
  105.         while(S1 != NULL){
  106.                 if(S1->num == n){
  107.                         printf("he/she is the %d student\n",i+1);
  108.                         return i+1;
  109.                 }
  110.                 S1 = S1->next;
  111.                 i++;
  112.         }
  113.         if(S1->next == NULL && S1->num != n){
  114.                         printf("can't find this student");
  115.                         return 0;
  116.                 }
  117.         return 0;
  118. }
  119. void showList(struct students **S1){
  120.         struct students *s;
  121.         s = *S1;
  122.         if(*S1 == NULL){
  123.                 printf("no student in this list!\n");
  124.         }
  125.         else{
  126.                 printf("name\tnum\tscore\t\n");
  127.         }
  128.         while(s != NULL){
  129.                 printf("%s\t%d\t%f\t\n",s->name,s->num,s->score);
  130.                 s = s->next;
  131.         }
  132.        
  133. }
复制代码

这个代码有很多BUG ,但是目前想弄明白是的addStu函数中的head指针的问题。
思路是用head指针获取链表的头节点的地址,在addStu函数完成后更新头结点的值,但是不知道为什么head指针有些时候里面储存的值不是头节点的值。比如你输入三个学生信息后,将S的地址传给ShowList函数后会出问题
不知道是我哪里想错了,请大佬们看看吧。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-6 14:00:11 | 显示全部楼层
本帖最后由 cookies945 于 2019-4-6 14:06 编辑

自己偶然发现了解决的方法,欢迎大佬们也来发表一下自己的看法呀,但是还是有很多问题,比如删除链表最后一个学生,链表就没有了,选择删除操作的时候,如果输入了一个比链表长度还大的数,也会发生错误

  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. void 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 student information:");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. void addStu(struct students **S1){
  46.         struct students *ns,*temp,*move = *S1;
  47.         int flag = 0;
  48.        
  49.         ns = (struct students *)malloc(sizeof(struct students));
  50.         if(ns == NULL){
  51.                 printf("menory allocation error!");
  52.                 exit(1);
  53.         }
  54.         getInfo(ns);
  55.         if(move == NULL){ //空链表的操作;
  56.                 move=ns;
  57.                 ns->next=NULL;
  58.                 *S1 = move;
  59.         }
  60.         else{
  61.                 while(move->num<ns->num && move->next != NULL){//通过新学生的学号来移动move与temp指针,
  62.                         flag = 1;
  63.                         temp = move;
  64.                         move = move->next;
  65.                 }
  66.                 if(move->next == NULL && move->num<=ns->num){ //新学生的学号是最大的
  67.                         move->next = ns;
  68.                         ns->next = NULL;
  69.                 }
  70.                 else if(!flag){ //目前没有发生问题
  71.                         temp = *S1;
  72.                         move = ns;
  73.                         ns->next = temp;
  74.                         *S1 = move;
  75.                 }
  76.                 else{ //学生学号刚好在中间
  77.                         temp->next = ns;
  78.                         ns->next = move;       
  79.                 }
  80.         }
  81. }
  82. int delStu(struct students **S1,int n){
  83.         struct students *temp;
  84.         struct students **ptr = S1; //用来接收旧的结构体节点,并准备释放内存
  85.         if((*ptr)->next == NULL){
  86.                 printf("can't delete student from an empty list\n");
  87.         }
  88.         while((*ptr)->num < n){
  89.                 (*ptr)=(*ptr)->next;
  90.         }
  91.         if((*ptr) == NULL){
  92.                 printf("can't find this student!\n");
  93.         }
  94.         if((*ptr)->num == n){
  95.                 temp = (*ptr);
  96.                 (*ptr) = temp->next;
  97.         //        free(temp);
  98.                 printf("delete complete\n");
  99.         }
  100.         return 0;
  101. }
  102. int searchStu(struct students *S1,int n){
  103.         int i = 0;
  104.         while(S1 != NULL){
  105.                 if(S1->num == n){
  106.                         printf("he/she is the %d student\n",i+1);
  107.                         return i+1;
  108.                 }
  109.                 S1 = S1->next;
  110.                 i++;
  111.         }
  112.         if(S1->next == NULL && S1->num != n){
  113.                         printf("can't find this student");
  114.                         return 0;
  115.                 }
  116.         return 0;
  117. }
  118. void showList(struct students *S1){
  119.         struct students *s;
  120.         s = S1;
  121.         if(S1 == NULL){
  122.                 printf("no student in this list!\n");
  123.         }
  124.         else{
  125.                 printf("name\tnum\tscore\t\n");
  126.         }
  127.         while(s != NULL){
  128.                 printf("%s\t%d\t%f\t\n",s->name,s->num,s->score);
  129.                 s = s->next;
  130.         }
  131.        
  132. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 20:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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