鱼C论坛

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

[已解决]写了一个链表的冒泡排序,用尾插法插入的链表,运行失败了

[复制链接]
发表于 2019-11-12 11:27:12 | 显示全部楼层 |阅读模式

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

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

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

  3. typedef struct Player{
  4.         char id[10];
  5.         char name[20];
  6.         float score;
  7.         struct Player *next;
  8. }Play, *pPlay;

  9. void getInput(pPlay  );
  10. void addPlayer(pPlay * );
  11. void printPlayer(pPlay  );
  12. void Wlist(pPlay );
  13. void BubbleSort(pPlay );


  14. int main(void)
  15. {
  16.          pPlay info = NULL;
  17.          int num;
  18.          
  19.          printf("请输入参赛选手的数目: ");
  20.          scanf("%d", &num);
  21.          
  22.          for(int i = 0; i < num; i++)
  23.          {
  24.                   addPlayer(&info);
  25.                  putchar('\n');        
  26.          }
  27.          
  28.          printf("\n 录入完毕, 现在开始打印验证....\n");
  29.          printPlayer(info);
  30.          printf("\n");
  31.          BubbleSort(info);
  32.          printPlayer(info);
  33.          //Wlist(info);
  34.          
  35. }

  36. void getInput(pPlay player)
  37. {
  38.          printf("请输入选手的编号 姓名 成绩 : ");
  39.          scanf("%s %s %f", player->id, player->name, &player->score);
  40. }
  41. void addPlayer(pPlay *info)
  42. {
  43.          pPlay player, tmp;
  44.          static pPlay tail;
  45.          
  46.          player = (pPlay)malloc(sizeof(Play));
  47.          
  48.          if(player == NULL)
  49.          {
  50.                   printf("memory allocation error!\n");
  51.                   exit(1);
  52.          }
  53.          
  54.          getInput(player);
  55.          
  56.          if(*info != NULL)
  57.          {
  58.                   tail->next = player;
  59.                   player->next = NULL;
  60.          }
  61.          else
  62.          {
  63.                   *info = player;
  64.                   player -> next = NULL;
  65.          }
  66.          
  67.          tail = player;         
  68. }
  69. void printPlayer(pPlay info)
  70. {
  71.          pPlay player;
  72.          player = info;
  73.          while(player != NULL)
  74.          {
  75.                   printf("编号: %s, 姓名: %s, 分数 :%5.2f\n", player->id, player->name, player->score);
  76.                   player = player -> next;
  77.          }
  78. }

  79. void Wlist(pPlay info)
  80. {
  81.          pPlay tmp = info;
  82.          FILE *fp;
  83.          char filename[30];
  84.          printf("input file name : ");
  85.          scanf("%s", filename);
  86.          
  87.          if((fp = fopen(filename,"wb")) == NULL)
  88.          {
  89.                   printf("file open error!\n");
  90.                  exit(0);
  91.          }
  92.          
  93.          while(tmp)
  94.          {
  95.                   fprintf(fp, "%s %s %5.2f\n", tmp->id, tmp->name, tmp->score);
  96.                   tmp = tmp->next;
  97.          }
  98.          fprintf(fp,"\n");
  99.      fclose(fp);
  100. }

  101. void BubbleSort(pPlay head)
  102. {
  103.     pPlay cur, tail, tmp;

  104.     tail = NULL;

  105.     while((head->next) != tail)
  106.     {
  107.         cur = head;
  108.                         
  109.         while(cur->next != tail)
  110.         {
  111.             if((cur->score) > (cur->next->score))
  112.             {
  113.                 cur->next = cur->next->next;   
  114.                 tmp = cur->next->next;   
  115.                 cur->next->next = cur;   
  116.                 cur->next = tmp;                                            
  117.             }            
  118.             cur = cur->next;
  119.         }
  120.         tail = cur;
  121.     }
  122. }
复制代码
用的是小甲鱼的链表插入,头指针里面有数据

请老哥们指点一下哪错了,调试的时候提示
QQ图片20191111165943.png
最佳答案
2019-11-12 19:04:49
添加我会这么改,print我会这么做
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct Player{
  4.         char id[10];
  5.         char name[20];
  6.         float score;
  7.         struct Player *next;
  8. }Play, *pPlay;

  9. void getInput(pPlay  );
  10. void addPlayer(pPlay * );
  11. void printPlayer(pPlay  );
  12. void Wlist(pPlay );
  13. void BubbleSort(pPlay );


  14. int main(void)
  15. {
  16.          pPlay info = NULL;
  17.          int num;

  18.          printf("请输入参赛选手的数目: ");
  19.          scanf("%d", &num);

  20.          for(int i = 0; i < num; i++)
  21.          {
  22.                   addPlayer(&info);
  23.          }

  24.          printf("\n 录入完毕, 现在开始打印验证....\n");
  25.          printPlayer(info);
  26.          printf("\n");
  27.          //BubbleSort(info);
  28.          printPlayer(info);
  29.          //Wlist(info);

  30. }

  31. void getInput(pPlay player)
  32. {
  33.          printf("请输入选手的编号 姓名 成绩 : ");
  34.          scanf("%s %s %f", player->id, player->name, &player->score);
  35.          player -> next = NULL;
  36. }
  37. void addPlayer(pPlay *info)
  38. {
  39.          pPlay newplayer, player;

  40.          newplayer = (pPlay)malloc(sizeof(Play));

  41.          if(newplayer == NULL)
  42.          {
  43.                   printf("memory allocation error!\n");
  44.                   exit(1);
  45.          }

  46.          getInput(newplayer);


  47.          if(*info == NULL) // 判断头节点
  48.          {
  49.                   *info = newplayer; //
  50.          }
  51.          else
  52.          {
  53.              player = *info; // 指向头节点
  54.              while(player->next!=NULL)
  55.              {
  56.                  player = player->next;
  57.              }
  58.             player->next = newplayer; // 把尾节点指向 新增用户
  59.          }
  60. }
  61. void printPlayer(pPlay info)
  62. {
  63.          while(info != NULL)
  64.          {
  65.                   printf("编号: %s, 姓名: %s, 分数 :%5.2f\n", info->id, info->name, info->score);
  66.                   info = info -> next;
  67.          }
  68. }

  69. void Wlist(pPlay info)
  70. {
  71.          pPlay tmp = info;
  72.          FILE *fp;
  73.          char filename[30];
  74.          printf("input file name : ");
  75.          scanf("%s", filename);

  76.          if((fp = fopen(filename,"wb")) == NULL)
  77.          {
  78.                   printf("file open error!\n");
  79.                  exit(0);
  80.          }

  81.          while(tmp)
  82.          {
  83.                   fprintf(fp, "%s %s %5.2f\n", tmp->id, tmp->name, tmp->score);
  84.                   tmp = tmp->next;
  85.          }
  86.          fprintf(fp,"\n");
  87.      fclose(fp);
  88. }

  89. void BubbleSort(pPlay head)
  90. {
  91.     pPlay cur, tail, tmp;

  92.     tail = NULL;

  93.     while((head->next) != tail)
  94.     {
  95.         cur = head;

  96.         while(cur->next != tail)
  97.         {
  98.             if((cur->score) > (cur->next->score))
  99.             {
  100.                 cur->next = cur->next->next;
  101.                 tmp = cur->next->next;
  102.                 cur->next->next = cur;
  103.                 cur->next = tmp;
  104.             }
  105.             cur = cur->next;
  106.         }
  107.         tail = cur;
  108.     }
  109. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-11-12 19:04:49 | 显示全部楼层    本楼为最佳答案   
添加我会这么改,print我会这么做
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct Player{
  4.         char id[10];
  5.         char name[20];
  6.         float score;
  7.         struct Player *next;
  8. }Play, *pPlay;

  9. void getInput(pPlay  );
  10. void addPlayer(pPlay * );
  11. void printPlayer(pPlay  );
  12. void Wlist(pPlay );
  13. void BubbleSort(pPlay );


  14. int main(void)
  15. {
  16.          pPlay info = NULL;
  17.          int num;

  18.          printf("请输入参赛选手的数目: ");
  19.          scanf("%d", &num);

  20.          for(int i = 0; i < num; i++)
  21.          {
  22.                   addPlayer(&info);
  23.          }

  24.          printf("\n 录入完毕, 现在开始打印验证....\n");
  25.          printPlayer(info);
  26.          printf("\n");
  27.          //BubbleSort(info);
  28.          printPlayer(info);
  29.          //Wlist(info);

  30. }

  31. void getInput(pPlay player)
  32. {
  33.          printf("请输入选手的编号 姓名 成绩 : ");
  34.          scanf("%s %s %f", player->id, player->name, &player->score);
  35.          player -> next = NULL;
  36. }
  37. void addPlayer(pPlay *info)
  38. {
  39.          pPlay newplayer, player;

  40.          newplayer = (pPlay)malloc(sizeof(Play));

  41.          if(newplayer == NULL)
  42.          {
  43.                   printf("memory allocation error!\n");
  44.                   exit(1);
  45.          }

  46.          getInput(newplayer);


  47.          if(*info == NULL) // 判断头节点
  48.          {
  49.                   *info = newplayer; //
  50.          }
  51.          else
  52.          {
  53.              player = *info; // 指向头节点
  54.              while(player->next!=NULL)
  55.              {
  56.                  player = player->next;
  57.              }
  58.             player->next = newplayer; // 把尾节点指向 新增用户
  59.          }
  60. }
  61. void printPlayer(pPlay info)
  62. {
  63.          while(info != NULL)
  64.          {
  65.                   printf("编号: %s, 姓名: %s, 分数 :%5.2f\n", info->id, info->name, info->score);
  66.                   info = info -> next;
  67.          }
  68. }

  69. void Wlist(pPlay info)
  70. {
  71.          pPlay tmp = info;
  72.          FILE *fp;
  73.          char filename[30];
  74.          printf("input file name : ");
  75.          scanf("%s", filename);

  76.          if((fp = fopen(filename,"wb")) == NULL)
  77.          {
  78.                   printf("file open error!\n");
  79.                  exit(0);
  80.          }

  81.          while(tmp)
  82.          {
  83.                   fprintf(fp, "%s %s %5.2f\n", tmp->id, tmp->name, tmp->score);
  84.                   tmp = tmp->next;
  85.          }
  86.          fprintf(fp,"\n");
  87.      fclose(fp);
  88. }

  89. void BubbleSort(pPlay head)
  90. {
  91.     pPlay cur, tail, tmp;

  92.     tail = NULL;

  93.     while((head->next) != tail)
  94.     {
  95.         cur = head;

  96.         while(cur->next != tail)
  97.         {
  98.             if((cur->score) > (cur->next->score))
  99.             {
  100.                 cur->next = cur->next->next;
  101.                 tmp = cur->next->next;
  102.                 cur->next->next = cur;
  103.                 cur->next = tmp;
  104.             }
  105.             cur = cur->next;
  106.         }
  107.         tail = cur;
  108.     }
  109. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 00:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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