鱼C论坛

 找回密码
 立即注册
查看: 2901|回复: 3

[已解决]C语言单链表合并

[复制链接]
发表于 2022-10-2 14:30:27 | 显示全部楼层 |阅读模式

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

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

x
这个问题有大佬会的吗,谢谢了

知两个非递减序列线性顺序表LA和LB,LA: {1,3,9,9,12,13,18,21}   LB: {2,4,6,10,15,19,22,23,25}  
设计顺序线性表合并算法,合并LA和LB为LC,要求仍然为非递减序列,最后输出合并后的LC.
最佳答案
2022-10-3 10:57:54
  1. #include <stdio.h>
  2. struct node
  3. {
  4.         int data;
  5.         struct node * next;
  6. }*h1,*h2,*t1,*t2,a[100001],b[100001];
  7. struct node * merge(struct node* h1,struct node* h2)
  8. {
  9.         struct node *tail=NULL,*head=NULL;
  10.         while (h1&&h2)
  11.         {
  12.                 if (h1->data < h2->data)
  13.                 {
  14.                         if (!tail)
  15.                                 tail=head=h1;
  16.                         else
  17.                         {
  18.                                 tail->next=h1;
  19.                                 tail=h1;
  20.                         }
  21.                         h1=h1->next;
  22.                 }
  23.                 else
  24.                 {
  25.                         if (!tail)
  26.                                 tail=head=h2;
  27.                         else
  28.                         {
  29.                                 tail->next=h2;
  30.                                 tail=h2;
  31.                         }
  32.                         h2=h2->next;
  33.                 }
  34.         }
  35.         while (h1)
  36.         {
  37.                 tail->next=h1;
  38.                 tail=h1;       
  39.                 h1=h1->next;       
  40.         }
  41.         while (h2)
  42.         {
  43.                 tail->next=h2;
  44.                 tail=h2;
  45.                 h2=h2->next;
  46.         }
  47.         return head;
  48. }
  49. int main()
  50. {
  51.     int n,m;
  52.     scanf("%d%d",&n,&m);
  53.         for (int i=1;i<=n;++i)
  54.         {
  55.                 scanf("%d",&a[i].data);
  56.                 if (!h1)h1=t1=&a[i];
  57.                 else
  58.                 {
  59.                         t1->next=&a[i];
  60.                         t1=&a[i];
  61.                 }
  62.         }
  63.         for (int i=1;i<=m;++i)
  64.         {
  65.                 scanf("%d",&b[i].data);
  66.                 if (!h2)h2=t2=&b[i];
  67.                 else
  68.                 {
  69.                         t2->next=&b[i];
  70.                         t2=&b[i];
  71.                 }
  72.         }
  73.         struct node * res=merge(h1,h2);
  74.         while (res)
  75.         {
  76.                 printf("%d ",res->data);
  77.                 res=res->next;
  78.         }
  79. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 14:56:20 | 显示全部楼层
去搜搜 归并排序 , 有惊喜
这跟链表有个鬼的关系
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-2 16:54:55 | 显示全部楼层
本帖最后由 jackz007 于 2022-10-2 19:33 编辑

        楼主要的是这个吗?
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int LA[] ={1,3,9,9,12,13,18,21} , LB[] = {2,4,6,10,15,19,22,23,25} , LC[200] , i , j , k  ;
  5.         for(i = j = k = 0 ; k < 17 ; k ++) LC[k] = (LA[i] < LB[j] && i < 8) ? LA[i ++] : LB[j ++] ;
  6.         printf("%d" , LC[0])                                                                      ;
  7.         for(i = 1 ; i < 17 ; i ++) printf("\t%d" , LC[i])                                         ;
  8.         printf("\n")                                                                              ;
  9. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. 1       2       3       4       6       9       9       10      12      13
  4. 15      18      19      21      22      23      25

  5. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-3 10:57:54 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. struct node
  3. {
  4.         int data;
  5.         struct node * next;
  6. }*h1,*h2,*t1,*t2,a[100001],b[100001];
  7. struct node * merge(struct node* h1,struct node* h2)
  8. {
  9.         struct node *tail=NULL,*head=NULL;
  10.         while (h1&&h2)
  11.         {
  12.                 if (h1->data < h2->data)
  13.                 {
  14.                         if (!tail)
  15.                                 tail=head=h1;
  16.                         else
  17.                         {
  18.                                 tail->next=h1;
  19.                                 tail=h1;
  20.                         }
  21.                         h1=h1->next;
  22.                 }
  23.                 else
  24.                 {
  25.                         if (!tail)
  26.                                 tail=head=h2;
  27.                         else
  28.                         {
  29.                                 tail->next=h2;
  30.                                 tail=h2;
  31.                         }
  32.                         h2=h2->next;
  33.                 }
  34.         }
  35.         while (h1)
  36.         {
  37.                 tail->next=h1;
  38.                 tail=h1;       
  39.                 h1=h1->next;       
  40.         }
  41.         while (h2)
  42.         {
  43.                 tail->next=h2;
  44.                 tail=h2;
  45.                 h2=h2->next;
  46.         }
  47.         return head;
  48. }
  49. int main()
  50. {
  51.     int n,m;
  52.     scanf("%d%d",&n,&m);
  53.         for (int i=1;i<=n;++i)
  54.         {
  55.                 scanf("%d",&a[i].data);
  56.                 if (!h1)h1=t1=&a[i];
  57.                 else
  58.                 {
  59.                         t1->next=&a[i];
  60.                         t1=&a[i];
  61.                 }
  62.         }
  63.         for (int i=1;i<=m;++i)
  64.         {
  65.                 scanf("%d",&b[i].data);
  66.                 if (!h2)h2=t2=&b[i];
  67.                 else
  68.                 {
  69.                         t2->next=&b[i];
  70.                         t2=&b[i];
  71.                 }
  72.         }
  73.         struct node * res=merge(h1,h2);
  74.         while (res)
  75.         {
  76.                 printf("%d ",res->data);
  77.                 res=res->next;
  78.         }
  79. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 20:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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