鱼C论坛

 找回密码
 立即注册
查看: 2814|回复: 15

[技术交流] 链表翻转初稿

[复制链接]
发表于 2022-5-8 12:09:40 | 显示全部楼层 |阅读模式

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

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

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

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

  8. int main()
  9. {
  10.         struct Node * node1;
  11.         struct Node * node2;
  12.         struct Node * node3;
  13.         struct Node * node4;
  14.         struct Node * node5;
  15.         struct Node * pmove;
  16.         node1=(struct Node*)malloc(sizeof(struct Node*));
  17.         node2=(struct Node*)malloc(sizeof(struct Node*));
  18.         node3=(struct Node*)malloc(sizeof(struct Node*));
  19.         node4=(struct Node*)malloc(sizeof(struct Node*));
  20.         node5=(struct Node*)malloc(sizeof(struct Node*));
  21.         node1->data=1;
  22.         node1->next=node2;
  23.         node2->data=2;
  24.         node2->next=node3;
  25.         node3->data=3;
  26.         node3->next=node4;
  27.         node4->data=4;
  28.         node4->next=node5;
  29.         node5->data=5;
  30.         node5->next=NULL;
  31.         //输出原始链表
  32.         pmove=node1;
  33.         while (pmove)
  34.         {
  35.              printf("%4d  ",pmove->data);
  36.              pmove=pmove->next;
  37.         }
  38.         printf("\n");
  39.         //为了翻转链表,我们需要四个指针
  40.         struct Node * cur;
  41.         struct Node * next;
  42.         struct Node * pre;   //还有一个是入口指针,前面有的
  43.         //1节点
  44.         pre=cur=node1;
  45.         next=cur->next;
  46.         cur->next=0;
  47.         //2节点
  48.         cur=next;
  49.         next=cur->next;
  50.         cur->next=pre;
  51.         pre=cur;
  52.         //3节点
  53.         cur=next;
  54.         next=cur->next;
  55.         cur->next=pre;
  56.         pre=cur;
  57.         //4节点
  58.         cur=next;
  59.         next=cur->next;
  60.         cur->next=pre;
  61.         pre=cur;
  62.         //5节点
  63.         cur=next;
  64.         next=cur->next;
  65.         cur->next=pre;
  66.         pre=cur;
  67.        //输出翻转后链表
  68.         pmove=cur;
  69.         while (pmove)
  70.         {
  71.              printf("%4d  ",pmove->data);
  72.              pmove=pmove->next;
  73.         }
  74.         printf("\n");
  75.         return 0;
  76. }

  77. /*
  78. 附效果图:
  79.    PS D:\001> ./w1
  80.    1     2     3     4     5  
  81.    5     4     3     2     1
  82. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-8 13:05:12 | 显示全部楼层
第二稿:
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  8. int main()
  9. {
  10.         struct Node * node1;
  11.         struct Node * head;
  12.         head=(struct Node*)malloc(sizeof(struct Node*));
  13.         head->data=0;
  14.         head->next=NULL;
  15.         //初始化链表 ,貌似这个是头插法吧
  16.         for(int x=0;x<10;x++)
  17.         {
  18.                 node1=(struct Node*)malloc(sizeof(struct Node*));
  19.                 node1->data=x;
  20.                 node1->next=head;
  21.                 head=node1;
  22.         }
  23.         //输出原始链表
  24.         struct Node *  pmove=head;
  25.         while (pmove)
  26.         {
  27.              if(pmove->next==NULL) break;
  28.              printf("%4d  ",pmove->data);
  29.              pmove=pmove->next;
  30.         }
  31.         printf("\n");
  32.         //为了翻转链表,我们需要四个指针
  33.         struct Node * cur;
  34.         struct Node * next;
  35.         struct Node * pre;   //还有一个是入口指针,前面有的
  36.         
  37.         pre=cur=head;
  38.         next=cur->next;
  39.         cur->next=0;
  40.         while(1)
  41.         {
  42.                 cur=next;
  43.                 next=cur->next;
  44.                 cur->next=pre;
  45.                 pre=cur;
  46.                 if (next==NULL) break;
  47.         }
  48.        //输出翻转后链表
  49.         pmove=cur->next;
  50.         while (pmove)
  51.         {
  52.              printf("%4d  ",pmove->data);
  53.              pmove=pmove->next;
  54.         }
  55.         printf("\n");
  56.         return 0;
  57. }

  58. /*
  59. 输出案例:
  60. PS D:\001> ./w1
  61.    9     8     7     6     5     4     3     2     1     0  
  62.    0     1     2     3     4     5     6     7     8     9
  63. PS D:\001>
  64. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-5-8 13:29:36 | 显示全部楼层

这个版本用了尾插法,和头插法区别还是蛮大的(不像有些资料上说的没啥区别)
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  8. int main()
  9. {
  10.         struct Node * node1;
  11.         struct Node * head;
  12.         head=(struct Node*)malloc(sizeof(struct Node*));
  13.         head->data=0;
  14.         head->next=NULL;
  15.         struct Node *  pmove=head;
  16.         struct Node *  head2=head;
  17.         //初始化链表 ,准备使用尾插法
  18.         for(int x=0;x<10;x++)
  19.         {
  20.                 node1=(struct Node*)malloc(sizeof(struct Node*));
  21.                 node1->data=x;
  22.                 node1->next=NULL;
  23.                 head->next=node1;
  24.                 head=node1;
  25.         }
  26.         //输出原始链表
  27.         pmove=pmove->next;
  28.         while (pmove)
  29.         {
  30.              printf("%4d  ",pmove->data);
  31.              pmove=pmove->next;
  32.         }
  33.         printf("\n");
  34.         //为了翻转链表,我们需要四个指针
  35.         struct Node * cur;
  36.         struct Node * next;
  37.         struct Node * pre;   //还有一个是入口指针,前面有的
  38.         
  39.         pre=cur=head2;
  40.         next=cur->next;
  41.         cur->next=0;
  42.         while(1)
  43.         {
  44.                 cur=next;
  45.                 next=cur->next;
  46.                 cur->next=pre;
  47.                 pre=cur;
  48.                 if (next==NULL) break;
  49.         }
  50.        //输出翻转后链表
  51.         pmove=cur;
  52.         while (pmove)
  53.         {
  54.              if(pmove->next==NULL) break;
  55.              printf("%4d  ",pmove->data);
  56.              pmove=pmove->next;
  57.         }
  58.         printf("\n");
  59.         return 0;
  60. }

  61. /*
  62. 输出案例:
  63. PS D:\001> ./w1
  64.    0     1     2     3     4     5     6     7     8     9  
  65.    9     8     7     6     5     4     3     2     1     0
  66. PS D:\001>
  67. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-5-9 16:19:49 | 显示全部楼层
wp231957 发表于 2022-5-8 13:29
这个版本用了尾插法,和头插法区别还是蛮大的(不像有些资料上说的没啥区别)

半途翻转链表(不够完美,但是终于依靠自己的代码  能翻了)
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  8. int main()
  9. {
  10.         struct Node * node1;
  11.         struct Node * head;
  12.         head=(struct Node*)malloc(sizeof(struct Node*));
  13.         head->data=0;
  14.         head->next=NULL;
  15.         //初始化链表 ,貌似这个是头插法吧
  16.         for(int x=0;x<10;x++)
  17.         {
  18.                 node1=(struct Node*)malloc(sizeof(struct Node*));
  19.                 node1->data=x;
  20.                 node1->next=head;
  21.                 head=node1;
  22.         }
  23.         //输出原始链表
  24.         struct Node *  pmove=head;
  25.         while (pmove)
  26.         {
  27.                 if(pmove->next==NULL) break;
  28.                 printf("%4d  ",pmove->data);
  29.                 pmove=pmove->next;
  30.         }
  31.         printf("\n");
  32.         //获取出口节点和入口节点
  33.         struct Node * ennode=NULL;
  34.         struct Node * exitnode=NULL;
  35.         pmove=node1;
  36.         int i=1;
  37.         while (pmove)
  38.         {
  39.                 if(i==3-1) ennode=pmove;   //要在3节点翻转 这里应该减一
  40.                 if(i==6)   exitnode=pmove;
  41.                 pmove=pmove->next;
  42.                 i++;
  43.         }
  44.         //半途翻转链表
  45.         struct Node * cur;
  46.         struct Node * next;
  47.         struct Node * pre;   
  48.         //入口节点
  49.         cur=ennode;  //此处ennode=node2
  50.         pre=next=ennode->next;   //此处需要保存真正的入口地址
  51.         cur->next=exitnode;     //跳转到需要翻转的首节点
  52.         //入口节点下一节点
  53.         cur=next;
  54.         next=next->next;
  55.         cur->next=exitnode->next;   //跳转不需翻转的节点
  56.         pre=cur;
  57.         while(1)
  58.         {
  59.                 cur=next;
  60.                 next=next->next;
  61.                 cur->next=pre;
  62.                 pre=cur;
  63.                 if (cur==exitnode) break;
  64.         }
  65.         //输出翻转后链表
  66.         pmove=head;
  67.         while (pmove)
  68.         {
  69.                 if(pmove->next==NULL) break;
  70.                 printf("%4d  ",pmove->data);
  71.                 pmove=pmove->next;
  72.         }
  73.         printf("\n");
  74.         return 0;
  75. }

  76. /*
  77. 附效果图:
  78. PS D:\001> ./w1
  79.    9     8     7     6     5     4     3     2     1     0  
  80.    9     8     4     5     6     7     3     2     1     0
  81. PS D:\001>
  82. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-10 10:37:32 | 显示全部楼层
wp231957 发表于 2022-5-9 16:19
半途翻转链表(不够完美,但是终于依靠自己的代码  能翻了)

这里有个单链表循环链表的样例:
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  8. //带头节点的 头插法
  9. struct Node * init_link(struct Node * s)
  10. {
  11.     struct Node * head=s;
  12.     struct Node * tmp=NULL;
  13.     for(int x=10;x<20;x++)
  14.     {
  15.         struct Node * node1=NULL;
  16.         node1=(struct Node*)malloc(sizeof(struct Node*));
  17.         node1->data=x;
  18.         if(x==10) tmp=node1;
  19.         node1->next=head;
  20.         head=node1;
  21.     }
  22.     tmp->next=head;      //构成循环链表,同时绕过头节点
  23.     return head;
  24. }

  25. int main()
  26. {
  27.         struct Node * node1;
  28.         struct Node * head;
  29.         head=(struct Node*)malloc(sizeof(struct Node*));
  30.         head->data=0;
  31.         head->next=NULL;
  32.         //输出原始链表
  33.         struct Node *  pmove=init_link(head);
  34.         int i=0;
  35.         while (pmove)
  36.         {
  37.                 if(pmove->next==NULL) break;
  38.                 printf("%4d  ",pmove->data);
  39.                 pmove=pmove->next;
  40.                 i++;
  41.                 if(i>15) break;     //为了测试循环的效果  我们选择了进行16次循环 看一下效果
  42.         }
  43.         printf("\n");
  44.       
  45.         return 0;
  46. }

  47. /*
  48. 附效果图:
  49. PS D:\001> ./w3
  50.   19    18    17    16    15    14    13    12    11    10    19    18    17    16    15    14  
  51. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-10 10:41:22 | 显示全部楼层
wp231957 发表于 2022-5-10 10:37
这里有个单链表循环链表的样例:

不知道循环链表有啥用,估计是为了双循环链表打基础吧,看一下他的遍历退出条件
  1. #include <stdio.h>
  2. #include <malloc.h>

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

  8. //带头节点的 头插法
  9. struct Node * init_link(struct Node * s)
  10. {
  11.     struct Node * head=s;
  12.     struct Node * tmp=NULL;
  13.     for(int x=10;x<20;x++)
  14.     {
  15.         struct Node * node1=NULL;
  16.         node1=(struct Node*)malloc(sizeof(struct Node*));
  17.         node1->data=x;
  18.         if(x==10) tmp=node1;
  19.         node1->next=head;
  20.         head=node1;
  21.     }
  22.     tmp->next=head;      //构成循环链表,同时绕过头节点
  23.     return head;
  24. }

  25. int main()
  26. {
  27.         struct Node * node1;
  28.         struct Node * head;
  29.         head=(struct Node*)malloc(sizeof(struct Node*));
  30.         head->data=0;
  31.         head->next=NULL;
  32.         //输出原始链表
  33.         struct Node *  pmove=init_link(head);
  34.         struct Node * tmp=pmove;
  35.         while (pmove)
  36.         {
  37.                 printf("%4d  ",pmove->data);
  38.                 if(pmove->next==tmp) break;     //这里是循环链表的遍历退出条件
  39.                 pmove=pmove->next;
  40.         }
  41.         printf("\n");
  42.       
  43.         return 0;
  44. }

  45. /*
  46. 附效果图:
  47. PS D:\001> ./w3
  48.   19    18    17    16    15    14    13    12    11    10  
  49. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-10 13:19:18 | 显示全部楼层
wp231957 发表于 2022-5-10 10:41
不知道循环链表有啥用,估计是为了双循环链表打基础吧,看一下他的遍历退出条件

前面都错了,也不改了,就那么地吧

这个版,功能是把一个链表插入另一个链表中间

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

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

  8. //带头节点的 头插法
  9. struct Node * init_link(struct Node * s)
  10. {
  11.     struct Node * head=s;
  12.     for(int x=10;x<20;x++)
  13.     {
  14.         struct Node * node1=NULL;
  15.         node1=(struct Node*)malloc(sizeof(struct Node));
  16.         node1->data=x;
  17.         node1->next=head;
  18.         head=node1;
  19.     }
  20.     return head;
  21. }

  22. //带头节点的 头插法
  23. struct Node * init_link2(struct Node * s)
  24. {
  25.     struct Node * head=s;
  26.     for(int x=100;x<110;x++)
  27.     {
  28.         struct Node * node1=NULL;
  29.         node1=(struct Node*)malloc(sizeof(struct Node));
  30.         node1->data=x;
  31.         node1->next=head;
  32.         head=node1;
  33.     }
  34.     return head;
  35. }


  36. int main()
  37. {
  38.         struct Node * node1;
  39.         struct Node * head;
  40.         head=(struct Node*)malloc(sizeof(struct Node));
  41.         head->data=0;
  42.         head->next=NULL;
  43.         //输出原始链表
  44.         struct Node *  pmove=init_link(head);
  45.         struct Node *  ins_ennode=NULL;
  46.         struct Node *  ins_exitnode=NULL;
  47.         int i=1;
  48.         struct Node *  hhead=pmove;
  49.         while (pmove)
  50.         {
  51.                // printf("%4d  ",pmove->data);
  52.                 if(pmove->next==NULL) break;     
  53.                 if(i==3)
  54.                 {
  55.                     ins_ennode=pmove;             //保存链表断开后的指针
  56.                     ins_exitnode=pmove->next;     //保存链表断开后的指针
  57.                     break;
  58.                 }
  59.                 i++;
  60.                 pmove=pmove->next;
  61.         }
  62.         struct Node * head2;
  63.         head2=(struct Node*)malloc(sizeof(struct Node));
  64.         head2->data=0;
  65.         head2->next=NULL;
  66.         struct Node *  pmove2=init_link2(head2);
  67.         struct Node *  sec_head=pmove2;
  68.         while (pmove2)
  69.         {
  70.              //printf("%4d  ",pmove2->data);
  71.              if(pmove2->next->next==NULL)    //把头节点绕出去
  72.              break;     
  73.              pmove2=pmove2->next;
  74.         }
  75.         struct Node *  sec_tail=pmove2;
  76.         //下面我们要做得就是把第二个链表插入第一个链表的第三个节点的后面
  77.         ins_ennode->next=sec_head;
  78.         sec_tail->next=ins_exitnode;
  79.         //遍历插入链表后的链表 并输出
  80.         while (hhead)
  81.         {
  82.                 if(hhead->next==NULL) break;     
  83.                 printf("%4d  ",hhead->data);
  84.                 hhead=hhead->next;
  85.         }
  86.         return 0;
  87. }

  88. /*
  89. 附效果图:
  90. PS D:\001> ./w3
  91.   19    18    17   109   108   107   106   105   104   103   102   101   100    16    15    14    13    12    11    10  
  92. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-11 09:47:23 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-11 09:51 编辑
wp231957 发表于 2022-5-10 13:19
前面都错了,也不改了,就那么地吧

这个版,功能是把一个链表插入另一个链表中间

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

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

  8. //带头节点的 头插法
  9. struct Node * init_link(struct Node * s)
  10. {
  11.     struct Node * head=s;
  12.     for(int x=10;x<20;x++)
  13.     {
  14.         struct Node * node1=NULL;
  15.         node1=(struct Node*)malloc(sizeof(struct Node));
  16.         node1->data=x;
  17.         node1->next=head;
  18.         head=node1;
  19.     }
  20.     return head;
  21. }


  22. int main()
  23. {
  24.         struct Node * node1;
  25.         struct Node * head;
  26.         head=(struct Node*)malloc(sizeof(struct Node));
  27.         head->data=0;
  28.         head->next=NULL;
  29.         //输出原始链表
  30.         head=init_link(head);
  31.         struct Node* p=head,*t=NULL;
  32.         while (p)
  33.         {
  34.                
  35.                 printf("%4d  ",p->data);
  36.                 p=p->next;
  37.         }

  38.         printf("\n");
  39.         p=t;
  40.         while (head)
  41.         {
  42.                         t=head->next;
  43.                         head->next=p;
  44.                         p=head;
  45.                         head=t;
  46.         }
  47.         head=p;
  48.         while (p)
  49.         {
  50.                
  51.                 printf("%4d  ",p->data);
  52.                 p=p->next;

  53.         }
  54.         while (head)
  55.         {
  56.                         t=head->next;
  57.                         free(head);
  58.                         head=t;
  59.         }
  60.         return 0;
  61. }
复制代码
  1. 19    18    17    16    15    14    13    12    11    10     0
  2.    0    10    11    12    13    14    15    16    17    18    19
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-11 09:52:02 From FishC Mobile | 显示全部楼层
jhq999 发表于 2022-5-11 09:47

谢谢,我现在用不了电脑,我狠好奇,你另一个链表在哪里???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-11 09:52:54 From FishC Mobile | 显示全部楼层
jhq999 发表于 2022-5-11 09:47

你这是倒序!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-11 09:54:30 From FishC Mobile | 显示全部楼层
jhq999 发表于 2022-5-11 09:47

能帮忙写一段冒泡排序吗,??我一直好奇,究竟是排指针啊,还是移动数据呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-11 12:02:35 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-11 12:12 编辑
wp231957 发表于 2022-5-11 09:54
能帮忙写一段冒泡排序吗,??我一直好奇,究竟是排指针啊,还是移动数据呢


不是链表反转吗,怎么还需要排序?
排序的话,感觉最好交换数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-11 12:19:12 From FishC Mobile | 显示全部楼层
jhq999 发表于 2022-5-11 12:02
不是链表反转吗,怎么还需要排序?
排序的话,感觉最好交换数据

翻转思路已经清晰了,就不用再研究了
你的思路我还需要研究一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-11 12:55:05 | 显示全部楼层
本帖最后由 jhq999 于 2022-5-11 16:24 编辑
wp231957 发表于 2022-5-11 12:19
翻转思路已经清晰了,就不用再研究了
你的思路我还需要研究一下

  1. struct Node *end=NULL,*q;//////////没测试只是个思路
  2.                 for (p=head; p->next; p=p->next)
  3.                 {
  4.                         for (q=head;q->next!=end;q=q->next)
  5.                         {
  6.                                 if (q->data>q->next->data)
  7.                                 {
  8.                                         Node t=*q;
  9.                                         q->data=q->next->data;
  10.                                         q->next->data=t.data;
  11.                                 }
  12.                         }
  13.                         end=q;
  14.                 }
复制代码

指针太麻烦了
  1.        
  2.                 struct Node list;       
  3.                 list.next=head;
  4.                
  5.                 while(1)
  6.                 {
  7.                         int flag=1;
  8.                         for (prv=&list,q=list.next;q->next!=end;prv=q,q=q->next)
  9.                         {
  10.                                 if (q->data>q->next->data)
  11.                                 {
  12.                                         flag=0;
  13.                                         prv->next=q->next;
  14.                                         q->next=prv->next->next;
  15.                                         prv->next->next=q;
  16.                                         q=prv->next;

  17.                                 }
  18.                         }
  19.                         //if (q!=list.next->next)
  20.                         if(flag)
  21.                         {
  22.                                 head=list.next;
  23.                                 break;
  24.                         }
  25.                         end=q;
  26.                 }
  27.                 head=list.next;
复制代码

评分

参与人数 1鱼币 +8 收起 理由
wp231957 + 8 表示感谢

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-11 17:11:56 | 显示全部楼层

成功了,成功了,谢谢,那个交换指针的方案,我还是不要研究了(估计我研究不了)

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <time.h>
  4. #include <stdlib.h>

  5. struct Node
  6. {
  7.         int data;
  8.         struct Node *next;
  9. };


  10. //带头节点的 头插法
  11. struct Node * init_link(struct Node * s)
  12. {
  13.     srand((int)time(NULL));
  14.     struct Node * head=s;
  15.     for(int x=10;x<20;x++)
  16.     {
  17.         struct Node * node1=NULL;
  18.         node1=(struct Node*)malloc(sizeof(struct Node));
  19.         node1->data=rand()%10000;
  20.         node1->next=head;
  21.         head=node1;
  22.     }
  23.     return head;
  24. }


  25. int main()
  26. {
  27.         struct Node * head;
  28.         head=(struct Node*)malloc(sizeof(struct Node));
  29.         head->data=0;
  30.         head->next=NULL;
  31.         head=init_link(head);
  32.         //输出原始链表
  33.         struct Node *pmove=head;
  34.         while (pmove)
  35.         {
  36.              if(pmove->next==NULL) break;     
  37.              printf("%4d  ",pmove->data);
  38.              pmove=pmove->next;
  39.         }
  40.         printf("\n");
  41.         //准备排序
  42.         struct Node *end=NULL,*q,*p;
  43.         for (p=head; p->next; p=p->next)
  44.         {
  45.                 for (q=head;q->next!=end;q=q->next)
  46.                 {
  47.                         if (q->data>q->next->data)
  48.                         {
  49.                                 int  t=q->data;
  50.                                 q->data=q->next->data;
  51.                                 q->next->data=t;
  52.                         }
  53.                 }
  54.                 end=q;
  55.         }
  56.         //输出排序后链表               
  57.         pmove=head->next;
  58.         while (pmove)
  59.         {
  60.              printf("%4d  ",pmove->data);
  61.              pmove=pmove->next;
  62.         }
  63.         return 0;
  64. }

  65. /*
  66. 样例输出:
  67. PS D:\001> ./w4
  68. 8227  9821  3215   548  4427  2807   119  6152  7995  7628  
  69. 119   548  2807  3215  4427  6152  7628  7995  8227  9821
  70. */
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-13 15:18:26 | 显示全部楼层
wp231957 发表于 2022-5-11 17:11
成功了,成功了,谢谢,那个交换指针的方案,我还是不要研究了(估计我研究不了)

如果仅仅是为了逆序输出一个链表  还可以更简单些:
  1. void reverse_link(struct Node * head)
  2. {
  3.     //这里正常是if(head==NULL) return;  这里我们使用带头节点的链表,所以把头节点绕过去
  4.     if(head->next==NULL) return;  
  5.     reverse_link(head->next);
  6.     printf("%8d",head->data);
  7. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 08:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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