鱼C论坛

 找回密码
 立即注册
查看: 2423|回复: 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
#include <stdio.h>
struct node
{
        int data;
        struct node * next;
}*h1,*h2,*t1,*t2,a[100001],b[100001];
struct node * merge(struct node* h1,struct node* h2)
{
        struct node *tail=NULL,*head=NULL;
        while (h1&&h2)
        {
                if (h1->data < h2->data)
                {
                        if (!tail)
                                tail=head=h1;
                        else
                        {
                                tail->next=h1;
                                tail=h1;
                        }
                        h1=h1->next;
                }
                else
                {
                        if (!tail)
                                tail=head=h2;
                        else
                        {
                                tail->next=h2;
                                tail=h2;
                        }
                        h2=h2->next;
                }
        }
        while (h1)
        {
                tail->next=h1;
                tail=h1;        
                h1=h1->next;        
        }
        while (h2)
        {
                tail->next=h2;
                tail=h2;
                h2=h2->next;
        }
        return head;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
        for (int i=1;i<=n;++i)
        {
                scanf("%d",&a[i].data);
                if (!h1)h1=t1=&a[i];
                else
                {
                        t1->next=&a[i];
                        t1=&a[i];
                }
        }
        for (int i=1;i<=m;++i)
        {
                scanf("%d",&b[i].data);
                if (!h2)h2=t2=&b[i];
                else
                {
                        t2->next=&b[i];
                        t2=&b[i];
                }
        }
        struct node * res=merge(h1,h2);
        while (res)
        {
                printf("%d ",res->data);
                res=res->next;
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 14:56:20 | 显示全部楼层
去搜搜 归并排序 , 有惊喜
这跟链表有个鬼的关系
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

int main(void)
{
        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  ;
        for(i = j = k = 0 ; k < 17 ; k ++) LC[k] = (LA[i] < LB[j] && i < 8) ? LA[i ++] : LB[j ++] ;
        printf("%d" , LC[0])                                                                      ;
        for(i = 1 ; i < 17 ; i ++) printf("\t%d" , LC[i])                                         ;
        printf("\n")                                                                              ;
}
        编译、运行实况:
D:\[00.Exerciese.2022]\C>g++ -o x x.c

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

D:\[00.Exerciese.2022]\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-3 10:57:54 | 显示全部楼层    本楼为最佳答案   
#include <stdio.h>
struct node
{
        int data;
        struct node * next;
}*h1,*h2,*t1,*t2,a[100001],b[100001];
struct node * merge(struct node* h1,struct node* h2)
{
        struct node *tail=NULL,*head=NULL;
        while (h1&&h2)
        {
                if (h1->data < h2->data)
                {
                        if (!tail)
                                tail=head=h1;
                        else
                        {
                                tail->next=h1;
                                tail=h1;
                        }
                        h1=h1->next;
                }
                else
                {
                        if (!tail)
                                tail=head=h2;
                        else
                        {
                                tail->next=h2;
                                tail=h2;
                        }
                        h2=h2->next;
                }
        }
        while (h1)
        {
                tail->next=h1;
                tail=h1;        
                h1=h1->next;        
        }
        while (h2)
        {
                tail->next=h2;
                tail=h2;
                h2=h2->next;
        }
        return head;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
        for (int i=1;i<=n;++i)
        {
                scanf("%d",&a[i].data);
                if (!h1)h1=t1=&a[i];
                else
                {
                        t1->next=&a[i];
                        t1=&a[i];
                }
        }
        for (int i=1;i<=m;++i)
        {
                scanf("%d",&b[i].data);
                if (!h2)h2=t2=&b[i];
                else
                {
                        t2->next=&b[i];
                        t2=&b[i];
                }
        }
        struct node * res=merge(h1,h2);
        while (res)
        {
                printf("%d ",res->data);
                res=res->next;
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-20 11:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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