鱼C论坛

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

[已解决]C语言求助_链表_合并有序列表(leetcode21题)

[复制链接]
发表于 2020-4-2 23:45:14 | 显示全部楼层 |阅读模式

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

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

x

链表10

链表10


不明白自己的代码为什么会无限循环,一共有三个合并代码,第一个常规思路,第二个递归,第三个是谭浩强那本书的答案(看不懂),但是人家谭老的代码运行很流畅
  1. <font face="Times New Roman">#include<stdio.h>
  2. #define LEN sizeof(struct Student *)

  3. struct Student
  4. {
  5.         int num;
  6.         float score;
  7.         struct Student *next;
  8. };

  9. int n;

  10. struct Student *creat()
  11. {
  12.         struct Student *head,*p1,*p2;
  13.         
  14.         p1=p2=(struct student *)malloc(LEN);
  15.         scanf("%d %f",&p1->num,&p1->score);
  16.         head=NULL;
  17.         n=0;
  18.         while(p1->num!=0)
  19.         {
  20.                 n++;
  21.                 if(n==1) head=p1;
  22.                 else p2->next=p1;
  23.                 p2=p1;
  24.                 p1=(struct student *)malloc(LEN);
  25.                 scanf("%d %f",&p1->num,&p1->score);
  26.         }
  27.         p2->next=NULL;
  28.         return head;
  29. }

  30. void print(struct Student *p)
  31. {
  32.         while(p!=NULL)
  33.         {
  34.                 printf("%-8d%-8.2f\n",p->num,p->score);
  35.                 p=p->next;
  36.         }
  37. }
  38. #if(0)
  39. struct Student *combine(struct Student *a,struct Student *b)
  40. {
  41.         struct Student *res,*p;
  42.         res=p;
  43.         while(a!=NULL&&b!=NULL)
  44.         {
  45.                 if(a->num>b->num)
  46.                 {
  47.                         p->next=b;
  48.                         b=b->next;
  49.                 }
  50.                 else
  51.                 {
  52.                         p->next=a;
  53.                         a=a->next;
  54.                 }
  55.                 p=p->next;
  56.         }
  57.         if(a==NULL) p->next=b;
  58.         if(b==NULL) p->next=a;
  59.         
  60.         return res->next;
  61. }
  62. #endif
  63. #if(0)
  64. struct Student *combine(struct Student *a,struct Student *b)
  65. {
  66.         struct Student *res;
  67.         if(a==NULL) return b;
  68.         if(b==NULL) return a;
  69.         
  70.         if(a->num<b->num)
  71.         {
  72.                 res=a;
  73.                 res->next=combine(a->next,b);
  74.         }
  75.         else
  76.         {
  77.                 res=a;
  78.                 res->next=combine(a,b->next);
  79.         }
  80.         
  81.         return res;
  82. }
  83. #endif
  84. #if(1)
  85. struct Student *combine(struct Student *ah,struct Student *bh)
  86. {
  87.         struct Student *pa1,*pa2,*pb1,*pb2;

  88.         pa1=pa2=ah;
  89.         pb1=pb2=bh;
  90.         
  91.         do
  92.         {
  93.                 while((pb1->num>pa1->num)&&(pa1->next!=NULL))
  94.                 {
  95.                         pa2=pa1;
  96.                         pa1=pa1->next;
  97.                 }
  98.                 if(pb1->num<=pa1->num)
  99.                 {
  100.                         if(ah==pa1) ah=pb1;
  101.                         else pa2->next=pb1;
  102.                         pb1=pb1->next;
  103.                         pb2->next=pa1;
  104.                         pa2=pb2;
  105.                         pb2=pb1;
  106.                 }
  107.         }while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));
  108.         if((pb1!=NULL)&&(pb1->num>pa1->num)&&pa1->next==NULL) pa1->next=pb1;
  109.         return ah;
  110. }
  111. #endif
  112. int main()
  113. {
  114.         struct Student *a,*b,*pt;
  115.         
  116.         printf("请输入链表a:\n");
  117.         a=creat();
  118.         printf("请输入链表b:\n");
  119.         b=creat();
  120.         
  121.         pt=combine(a,b);
  122.         print(pt);
  123.         return 0;
  124. }</font>
复制代码
最佳答案
2020-4-3 12:00:07
1,你自己的代码呢,都忘了给指针分配内存了。
  1. res=p=(struct student *)malloc(n);
复制代码
另外你这样排序不了了,和冒泡排序相比少了个循环。
2,谭浩强代码解释看图,它排序的前提是两个链表已经是排序好的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-2 23:52:36 | 显示全部楼层
加了一些注释

  1. <font face="Times New Roman">#include<stdio.h>
  2. #define LEN sizeof(struct Student *)

  3. struct Student
  4. {
  5.         int num;
  6.         float score;
  7.         struct Student *next;
  8. };

  9. int n;
  10. //从键盘输入链表
  11. struct Student *creat()
  12. {
  13.         struct Student *head,*p1,*p2;
  14.         
  15.         p1=p2=(struct student *)malloc(LEN);
  16.         scanf("%d %f",&p1->num,&p1->score);
  17.         head=NULL;
  18.         n=0;
  19.         while(p1->num!=0)
  20.         {
  21.                 n++;
  22.                 if(n==1) head=p1;
  23.                 else p2->next=p1;
  24.                 p2=p1;
  25.                 p1=(struct student *)malloc(LEN);
  26.                 scanf("%d %f",&p1->num,&p1->score);
  27.         }
  28.         p2->next=NULL;
  29.         return head;
  30. }
  31. //打印输出链表
  32. void print(struct Student *p)
  33. {
  34.         while(p!=NULL)
  35.         {
  36.                 printf("%-8d%-8.2f\n",p->num,p->score);
  37.                 p=p->next;
  38.         }
  39. }
  40. //第一种方法,常规思路
  41. #if(0)
  42. struct Student *combine(struct Student *a,struct Student *b)
  43. {
  44.         struct Student *res,*p;
  45.         res=p;
  46.         while(a!=NULL&&b!=NULL)
  47.         {
  48.                 if(a->num>b->num)
  49.                 {
  50.                         p->next=b;
  51.                         b=b->next;
  52.                 }
  53.                 else
  54.                 {
  55.                         p->next=a;
  56.                         a=a->next;
  57.                 }
  58.                 p=p->next;
  59.         }
  60.         if(a==NULL) p->next=b;
  61.         if(b==NULL) p->next=a;
  62.         
  63.         return res->next;
  64. }
  65. #endif
  66. //递归方法
  67. #if(0)
  68. struct Student *combine(struct Student *a,struct Student *b)
  69. {
  70.         struct Student *res;
  71.         if(a==NULL) return b;
  72.         if(b==NULL) return a;
  73.         
  74.         if(a->num<b->num)
  75.         {
  76.                 res=a;
  77.                 res->next=combine(a->next,b);
  78.         }
  79.         else
  80.         {
  81.                 res=a;
  82.                 res->next=combine(a,b->next);
  83.         }
  84.         
  85.         return res;
  86. }
  87. #endif
  88. //谭浩强代码
  89. #if(1)
  90. struct Student *combine(struct Student *ah,struct Student *bh)
  91. {
  92.         struct Student *pa1,*pa2,*pb1,*pb2;

  93.         pa1=pa2=ah;
  94.         pb1=pb2=bh;
  95.         
  96.         do
  97.         {
  98.                 while((pb1->num>pa1->num)&&(pa1->next!=NULL))
  99.                 {
  100.                         pa2=pa1;
  101.                         pa1=pa1->next;
  102.                 }
  103.                 if(pb1->num<=pa1->num)
  104.                 {
  105.                         if(ah==pa1) ah=pb1;
  106.                         else pa2->next=pb1;
  107.                         pb1=pb1->next;
  108.                         pb2->next=pa1;//到这里看不懂了
  109.                         pa2=pb2;
  110.                         pb2=pb1;
  111.                 }
  112.         }while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));
  113.         if((pb1!=NULL)&&(pb1->num>pa1->num)&&pa1->next==NULL) pa1->next=pb1;
  114.         return ah;
  115. }
  116. #endif
  117. int main()
  118. {
  119.         struct Student *a,*b,*pt;
  120.         
  121.         printf("请输入链表a:\n");
  122.         a=creat();
  123.         printf("请输入链表b:\n");
  124.         b=creat();
  125.         
  126.         pt=combine(a,b);//调用合并函数
  127.         print(pt);//打印结果
  128.         return 0;
  129. }</font>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-3 12:00:07 | 显示全部楼层    本楼为最佳答案   
1,你自己的代码呢,都忘了给指针分配内存了。
  1. res=p=(struct student *)malloc(n);
复制代码
另外你这样排序不了了,和冒泡排序相比少了个循环。
2,谭浩强代码解释看图,它排序的前提是两个链表已经是排序好的。
temp.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-3 13:11:21 | 显示全部楼层
百里狂生 发表于 2020-4-3 12:00
1,你自己的代码呢,都忘了给指针分配内存了。另外你这样排序不了了,和冒泡排序相比少了个循环。
2,谭浩 ...

感谢,确实昨天睡觉的时候突然想到自己没有分配内存。
图画的很清楚,现在已经理解谭老的代码了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-3 13:30:50 | 显示全部楼层
分配内存后第一种常规思路问题解决。

递归那里是我的代码写错了。。。。。
else里面应该是  res=b.

..................


另外,谭老师代码是有缺陷的。。。。真的觉得他的代码太鸡肋。不想改(其实是可能不会改)

附上谭老的不完美的运行情况

2.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-3 15:57:59 | 显示全部楼层
ALiowys 发表于 2020-4-3 13:30
分配内存后第一种常规思路问题解决。

递归那里是我的代码写错了。。。。。

这应该是do while()的条件问题。
结束循环有两种情况:
1,pa1->next==NULL && pb1->num>pa1->num (a链表已经到最后一个元素,且此时b链表的首元素比这个元素要大)。
2,pb1 == NULL。(只可能a中存在元素比b中所有元素都大才会出现)
但是,如果pb1==NULL的话,pb1->num>pa1->num 比较就会出错。苦思无果,所以只好把第2种情况的判断放在循环体中。
  1.      do
  2.         {
  3.                 while((pb1->num>pa1->num)&&(pa1->next!=NULL))
  4.                 {
  5.                         pa2=pa1;
  6.                         pa1=pa1->next;
  7.                 }
  8.                 if(pb1->num<=pa1->num)
  9.                 {
  10.                         if(ah==pa1) ah=pb1;
  11.                         else pa2->next=pb1;
  12.                         pb1=pb1->next;
  13.                         pb2->next=pa1;
  14.                         pa2=pb2;
  15.                         pb2=pb1;
  16.                 }
  17.                 if(pb1==NULL) break;
  18.         }while(!(pa1->next==NULL && pb1->num>pa1->num));
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-3 20:57:35 | 显示全部楼层
百里狂生 发表于 2020-4-3 15:57
这应该是do while()的条件问题。
结束循环有两种情况:
1,pa1->next==NULL && pb1->num>pa1->num (a链 ...

他这里解题的前提设定应该就是“按顺序间隔错开”,
所以这里代码的精髓应该是想让我这类的初学者理解您上面用来解释链表之间比较的代码的图的逻辑。
而且我不认为不能把他这段代码改完美就不能很好理解链表。
期待与您的下次交流。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-7 15:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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