鱼C论坛

 找回密码
 立即注册
查看: 1002|回复: 7

[已解决]链表冒泡排序我感觉我这么更改应该没有问题但是为什么执行不了哪位能给指点一下谢谢

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

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

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

x

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

  3. struct Node
  4. {
  5.                 int a;
  6.                 struct Node *next;
  7. };

  8. void addlibrary(struct Node **);
  9. void getInput(struct Node *);
  10. void sort(struct Node *);
  11. void printflibrary(struct Node *);

  12. void getInput(struct Node *number)
  13. {
  14.                 scanf("%d", &number->a);

  15. }
  16. void addlibrary(struct Node **library)
  17. {
  18.                 struct Node *number;
  19.                 static struct Node *tail;

  20.                 number = (struct Node *)malloc(sizeof(struct Node));
  21.                 if(number == NULL)
  22.                 {
  23.                                 printf("对不起内存分配失败!");
  24.                                 exit(1);
  25.                 }

  26.                 getInput(number);
  27.                
  28.                 if(*library != NULL)
  29.                 {
  30.                                 tail->next = number;
  31.                                 number->next = NULL;
  32.                 }
  33.                 else
  34.                 {
  35.                                 *library = number;
  36.                                 number->next = NULL;
  37.                 }
  38.                 tail = number;

  39. }
  40. //为什么这个链表一排序就出错呢?我这个排序程序哪里写的有问题呢?

  41. void sort(struct Node *library)
  42. {
  43.                 struct Node *temp;
  44.                 struct Node *one;
  45.                

  46.                 while(library != NULL)
  47.                 {
  48.                                 temp = library->next;
  49.                                 if(library->a > temp->a)
  50.                                 {
  51.                                                 one = library;
  52.                                                 library = temp;
  53.                                                 one->next = temp->next;
  54.                                                 library = one;
  55.                                 }
  56.                 }


  57. }

  58. void printflibrary(struct Node *library)
  59. {
  60.                 struct Node *temp;

  61.                 temp = library;

  62.                 while(temp != NULL)
  63.                 {
  64.                                 printf("%d", temp->a);
  65.                                 temp = temp->next;
  66.                 }
  67. }

  68. int main()
  69. {
  70.                 struct Node *library = NULL;
  71.                 int ch = 10;

  72.                 while(ch)
  73.                 {
  74.                                 addlibrary(&library);
  75.                                 ch--;
  76.                 }

  77.                 sort(library);
  78.                 printflibrary(library);
  79.                 return 0;
  80. }
复制代码
最佳答案
2019-10-4 22:51:19
有2个问题

1.你用错算法了,你的这个算法没办法完成排序
参考这个,你应该能明白你的算法无法完成排序
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node
  4. {
  5.         int a;
  6.         struct Node *next;
  7. };

  8. void getInput(struct Node *number)
  9. {
  10.         scanf("%d", &number->a);

  11. }
  12. void addlibrary(struct Node **library)
  13. {
  14.         struct Node *number;
  15.         static struct Node *tail;

  16.         number = malloc(sizeof(struct Node));
  17.         if(!number)
  18.         {
  19.                 printf("对不起内存分配失败!\n");
  20.                 exit(1);
  21.         }

  22.         getInput(number);

  23.         if(*library != NULL)
  24.         {
  25.                 tail->next = number;
  26.                 number->next = NULL;
  27.         }
  28.         else
  29.         {
  30.                 *library = number;
  31.                 number->next = NULL;
  32.         }
  33.         tail = number;

  34. }

  35. void sort(struct Node *library)
  36. {
  37.         struct Node *current = library;
  38.         while(current)
  39.         {
  40.                 struct Node *min = current;
  41.                 struct Node *p = current->next;
  42.                 while(p)
  43.                 {
  44.                         if(min->a > p->a)
  45.                                 min = p;
  46.                         p = p->next;
  47.                 }

  48.                 int temp = current->a;
  49.                 current->a = min->a;
  50.                 min->a = temp;

  51.                 current = current->next;
  52.         }
  53. }

  54. void printflibrary(struct Node *library)
  55. {
  56.         struct Node *temp = library;
  57.         while(temp)
  58.         {
  59.                 printf("%d ", temp->a);
  60.                 temp = temp->next;
  61.         }
  62.         printf("\n");
  63. }

  64. int main()
  65. {
  66.         struct Node *library = NULL;
  67.         int ch = 10;
  68.         while(ch--)
  69.                 addlibrary(&library);
  70.         sort(library);
  71.         printflibrary(library);
  72.         return 0;
  73. }

  74. /*
  75. 9 1 0 2 8 3 7 4 6 5
  76. 0 1 2 3 4 5 6 7 8 9
  77. 请按任意键继续. . .
  78. */
复制代码



第2个问题
要修改一级指针,你需要二级指针
参考这个代码
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node
  4. {
  5.         int a;
  6.         struct Node *next;
  7. };

  8. void getInput(struct Node *number)
  9. {
  10.         scanf("%d", &number->a);

  11. }
  12. void addlibrary(struct Node **library)
  13. {
  14.         struct Node *number;
  15.         static struct Node *tail;

  16.         number = malloc(sizeof(struct Node));
  17.         if(!number)
  18.         {
  19.                 printf("对不起内存分配失败!\n");
  20.                 exit(1);
  21.         }

  22.         getInput(number);

  23.         if(*library != NULL)
  24.         {
  25.                 tail->next = number;
  26.                 number->next = NULL;
  27.         }
  28.         else
  29.         {
  30.                 *library = number;
  31.                 number->next = NULL;
  32.         }
  33.         tail = number;

  34. }

  35. void sort(struct Node **library)
  36. {
  37.         struct Node *current = *library;
  38.         while(current->next)
  39.         {
  40.                 struct Node *min = current;
  41.                 struct Node *p = current->next;
  42.                 while(p->next)
  43.                 {
  44.                         if(min->next->a > p->next->a)
  45.                                 min = p;
  46.                         p = p->next;
  47.                 }

  48.                 struct Node *temp = current->next;
  49.                 current->next = min->next;
  50.                 min->next = temp;
  51.                 temp = min->next->next;
  52.                 min->next->next = current->next->next;
  53.                 current->next->next = temp;

  54.                 current = current->next;
  55.         }

  56.         current = *library;
  57.         struct Node *p = current;
  58.         while(p->next)
  59.         {
  60.                 if(current->a > p->next->a)
  61.                         p = p->next;
  62.                 else
  63.                         break;
  64.         }
  65.         if(p == current && current->a < current->next->a)
  66.                 return;
  67.         *library = current->next;

  68.         current->next = p->next;
  69.         p->next = current;
  70. }

  71. void printflibrary(struct Node *library)
  72. {
  73.         struct Node *temp = library;
  74.         while(temp)
  75.         {
  76.                 printf("%d ", temp->a);
  77.                 temp = temp->next;
  78.         }
  79.         printf("\n");
  80. }

  81. int main()
  82. {
  83.         struct Node *library = NULL;
  84.         int ch = 10;
  85.         while(ch--)
  86.                 addlibrary(&library);
  87.         sort(&library);
  88.         printflibrary(library);
  89.         return 0;
  90. }

  91. /*
  92. 8 1 9 0 2 7 3 6 4 5
  93. 0 1 2 3 4 5 6 7 8 9
  94. 请按任意键继续. . .
  95. */
复制代码


void sort(struct Node **library)
sort的参数改成了二级指针,因为要修改main函数中的library变量,library是一级指针,所以这里的参数要二级指针,这样才能修改main函数中的那个library

sort(&library);
在main函数中这样使用sort函数

如果链表的第0个元素不存储数据,那么通过交换指针的方式排序是可行的
但是如果链表的第0个元素也存储数据,那么就不适合通过交换指针的方式排序

我不知道这个代码你能不能看懂
因为链表的第0个元素也存储数据,这个数据结构不适合通过交换指针的方式排序,非要这样做也不是不可以,就是像我这样,通过算法来适应数据结构,就导致了我把代码写成这样,很不好理解,说真的,我理解这个代码都够呛,但是我还是勉强能理解,毕竟这个代码是我写的
^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-4 22:51:19 | 显示全部楼层    本楼为最佳答案   
有2个问题

1.你用错算法了,你的这个算法没办法完成排序
参考这个,你应该能明白你的算法无法完成排序
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node
  4. {
  5.         int a;
  6.         struct Node *next;
  7. };

  8. void getInput(struct Node *number)
  9. {
  10.         scanf("%d", &number->a);

  11. }
  12. void addlibrary(struct Node **library)
  13. {
  14.         struct Node *number;
  15.         static struct Node *tail;

  16.         number = malloc(sizeof(struct Node));
  17.         if(!number)
  18.         {
  19.                 printf("对不起内存分配失败!\n");
  20.                 exit(1);
  21.         }

  22.         getInput(number);

  23.         if(*library != NULL)
  24.         {
  25.                 tail->next = number;
  26.                 number->next = NULL;
  27.         }
  28.         else
  29.         {
  30.                 *library = number;
  31.                 number->next = NULL;
  32.         }
  33.         tail = number;

  34. }

  35. void sort(struct Node *library)
  36. {
  37.         struct Node *current = library;
  38.         while(current)
  39.         {
  40.                 struct Node *min = current;
  41.                 struct Node *p = current->next;
  42.                 while(p)
  43.                 {
  44.                         if(min->a > p->a)
  45.                                 min = p;
  46.                         p = p->next;
  47.                 }

  48.                 int temp = current->a;
  49.                 current->a = min->a;
  50.                 min->a = temp;

  51.                 current = current->next;
  52.         }
  53. }

  54. void printflibrary(struct Node *library)
  55. {
  56.         struct Node *temp = library;
  57.         while(temp)
  58.         {
  59.                 printf("%d ", temp->a);
  60.                 temp = temp->next;
  61.         }
  62.         printf("\n");
  63. }

  64. int main()
  65. {
  66.         struct Node *library = NULL;
  67.         int ch = 10;
  68.         while(ch--)
  69.                 addlibrary(&library);
  70.         sort(library);
  71.         printflibrary(library);
  72.         return 0;
  73. }

  74. /*
  75. 9 1 0 2 8 3 7 4 6 5
  76. 0 1 2 3 4 5 6 7 8 9
  77. 请按任意键继续. . .
  78. */
复制代码



第2个问题
要修改一级指针,你需要二级指针
参考这个代码
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node
  4. {
  5.         int a;
  6.         struct Node *next;
  7. };

  8. void getInput(struct Node *number)
  9. {
  10.         scanf("%d", &number->a);

  11. }
  12. void addlibrary(struct Node **library)
  13. {
  14.         struct Node *number;
  15.         static struct Node *tail;

  16.         number = malloc(sizeof(struct Node));
  17.         if(!number)
  18.         {
  19.                 printf("对不起内存分配失败!\n");
  20.                 exit(1);
  21.         }

  22.         getInput(number);

  23.         if(*library != NULL)
  24.         {
  25.                 tail->next = number;
  26.                 number->next = NULL;
  27.         }
  28.         else
  29.         {
  30.                 *library = number;
  31.                 number->next = NULL;
  32.         }
  33.         tail = number;

  34. }

  35. void sort(struct Node **library)
  36. {
  37.         struct Node *current = *library;
  38.         while(current->next)
  39.         {
  40.                 struct Node *min = current;
  41.                 struct Node *p = current->next;
  42.                 while(p->next)
  43.                 {
  44.                         if(min->next->a > p->next->a)
  45.                                 min = p;
  46.                         p = p->next;
  47.                 }

  48.                 struct Node *temp = current->next;
  49.                 current->next = min->next;
  50.                 min->next = temp;
  51.                 temp = min->next->next;
  52.                 min->next->next = current->next->next;
  53.                 current->next->next = temp;

  54.                 current = current->next;
  55.         }

  56.         current = *library;
  57.         struct Node *p = current;
  58.         while(p->next)
  59.         {
  60.                 if(current->a > p->next->a)
  61.                         p = p->next;
  62.                 else
  63.                         break;
  64.         }
  65.         if(p == current && current->a < current->next->a)
  66.                 return;
  67.         *library = current->next;

  68.         current->next = p->next;
  69.         p->next = current;
  70. }

  71. void printflibrary(struct Node *library)
  72. {
  73.         struct Node *temp = library;
  74.         while(temp)
  75.         {
  76.                 printf("%d ", temp->a);
  77.                 temp = temp->next;
  78.         }
  79.         printf("\n");
  80. }

  81. int main()
  82. {
  83.         struct Node *library = NULL;
  84.         int ch = 10;
  85.         while(ch--)
  86.                 addlibrary(&library);
  87.         sort(&library);
  88.         printflibrary(library);
  89.         return 0;
  90. }

  91. /*
  92. 8 1 9 0 2 7 3 6 4 5
  93. 0 1 2 3 4 5 6 7 8 9
  94. 请按任意键继续. . .
  95. */
复制代码


void sort(struct Node **library)
sort的参数改成了二级指针,因为要修改main函数中的library变量,library是一级指针,所以这里的参数要二级指针,这样才能修改main函数中的那个library

sort(&library);
在main函数中这样使用sort函数

如果链表的第0个元素不存储数据,那么通过交换指针的方式排序是可行的
但是如果链表的第0个元素也存储数据,那么就不适合通过交换指针的方式排序

我不知道这个代码你能不能看懂
因为链表的第0个元素也存储数据,这个数据结构不适合通过交换指针的方式排序,非要这样做也不是不可以,就是像我这样,通过算法来适应数据结构,就导致了我把代码写成这样,很不好理解,说真的,我理解这个代码都够呛,但是我还是勉强能理解,毕竟这个代码是我写的
^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-4 23:02:19 | 显示全部楼层
人造人 发表于 2019-10-4 22:51
有2个问题

1.你用错算法了,你的这个算法没办法完成排序

恩恩就是想理解指针的用法,刚学算法小甲鱼讲的算法,总感觉链表这做一张要是不把单链表这个吃透了学起来总是有些困难,这不正在一点一点的实验呢,总感觉不做题看那些东西理解又不理解所以就找些题来做做,看看能不能记点东西,这个是我把带你学C带你飞那张的链表那个题改了一下。我仔细看看你给的这些例题争取能看懂了,怎么也得能从你这里学点东西不是,加油
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-21 17:15:01 | 显示全部楼层
人造人 发表于 2019-10-4 22:51
有2个问题

1.你用错算法了,你的这个算法没办法完成排序

第一种方式我理解了我按照自己的理解的方式写了一遍如果可以的话给指点一下谢谢了
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct Node
  4. {
  5.                 int a;
  6.                 struct Node *next;
  7. };

  8. void addlibrary(struct Node **);
  9. void getinput(struct Node *);
  10. void printflibrary(struct Node *);
  11. void srot(struct Node *);

  12. void getinput(struct Node *number)
  13. {
  14.                 scanf("%d", &number->a);
  15. }

  16. void addlibrary(struct Node **library)
  17. {
  18.                 struct Node *number;
  19.                 static struct Node *tail;

  20.                 number = (struct Node *)malloc(sizeof(struct Node));
  21.                 if(number == NULL)
  22.                 {
  23.                                 printf("内存分配失败!");
  24.                                 exit(1);
  25.                 }

  26.                 getinput(number);

  27.                 if(*library != NULL)
  28.                 {
  29.                                 tail->next = number;
  30.                                 number->next = NULL;
  31.                 }
  32.                 else
  33.                 {
  34.                                 *library = number;
  35.                                 number->next = NULL;
  36.                 }
  37.                 tail = number;
  38. }
  39. void srot(struct Node *library)
  40. {
  41.                 struct Node *temp;
  42.                 temp = library;
  43.                 while(temp != NULL)
  44.                 {
  45.                                 struct Node *first;
  46.                                 int old;
  47.                                 first = library;
  48.                                 while(first != NULL)
  49.                                 {
  50.                                                 if(temp->a < first->a)
  51.                                                 {
  52.                                                                 old =temp->a;
  53.                                                                 temp->a = first->a;
  54.                                                                 first->a = old;
  55.                                                 }
  56.                                                 first = first->next;
  57.                                 }
  58.                                 temp = temp->next;
  59.                 }


  60. }
  61. void printflibrary(struct Node *library)
  62. {
  63.                 struct Node *temp;

  64.                 temp = library;

  65.                 while(temp != NULL)
  66.                 {
  67.                                 printf("%d", temp->a);
  68.                                 temp = temp->next;
  69.                 }
  70. }



  71. int main()
  72. {
  73.                 struct Node *library = NULL;
  74.                 int ch = 10;
  75.                 while(ch)
  76.                 {
  77.                                 addlibrary(&library);
  78.                                 ch--;
  79.                 }
  80.                 srot(library);
  81.                 printflibrary(library);

  82.                 return 0;
  83. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-21 21:31:43 | 显示全部楼层
addlibrary改成这样怎么样?

  1. void addlibrary(struct Node **library)
  2. {
  3.         struct Node *number;
  4.         static struct Node *tail;

  5.         number = (struct Node *)malloc(sizeof(struct Node));
  6.         if(!number)
  7.         {
  8.                 printf("内存分配失败!");
  9.                 exit(1);
  10.         }

  11.         getinput(number);
  12.         number->next = NULL;

  13.         if(*library)
  14.                 tail->next = number;
  15.         else
  16.                 *library = number;
  17.         tail = number;
  18. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-21 21:36:10 | 显示全部楼层
  1. void srot(struct Node *library)
  2. {
  3.         struct Node *temp = library;
  4.         while(temp != NULL)
  5.         {
  6.                 struct Node *first = library;
  7.                 int old;
  8.                 while(first != NULL)
  9.                 {
  10.                         if(temp->a < first->a)
  11.                         {
  12.                                 old =temp->a;
  13.                                 temp->a = first->a;
  14.                                 first->a = old;
  15.                         }
  16.                         first = first->next;
  17.                 }
  18.                 temp = temp->next;
  19.         }
  20. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-22 14:49:14 | 显示全部楼层
人造人 发表于 2019-10-21 21:31
addlibrary改成这样怎么样?

嗯嗯这样改比我那个要效率高减少了寄存器的调用,不管*library是不是为空number->next = NULL谢谢指教
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-22 14:50:28 | 显示全部楼层

struct Node *temp = library;
我这里啰嗦了谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 14:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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