knight_166 2022-09-26 20:59 采纳率: 100%
浏览 34
已结题

链表,想用两个顺序链表使其合成为一个顺序表

问题遇到的现象和发生背景

初学链表,想用两个顺序链表line与line2合成一个lin,使其合成为一个顺序表,假设line与line2无重复元素,为啥lin输出不了。

用代码块功能插入代码,请勿粘贴截图
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int a;
    struct node* llink;
    struct node* rlink;
};
struct q
{
    int max;
    node* head;
};
q line,line2;
int init(q* h)
{
    h->head = (node*)malloc(sizeof(node));
    if (!h->head)
        return 0;
    h->head->rlink = NULL;
    h->head->llink = NULL;
    return 1;
}
void newnode(int key, q* h)
{
    node* m,*n;
    m = h->head;
    if (m == NULL)
    {
        m->a = key;
        m->llink = NULL;
    }
    else 
    {
        while (m->llink != NULL)
        {
            m = m->llink;
        }
        n = (node*)malloc(sizeof(node));
        n->a = key;
        m->llink = n;
        n->rlink = m;
        n->llink = NULL;
    }
}
void out(q h)
{
    node* m;
    m = h.head;
    while(m->llink != NULL)
    {
        m = m->llink;
        printf("%d ", m->a);
    }
}
q lin;
void donode(q* h,q*lin)
{
    node* m, * n,*l;
    m = h->head->llink;
    n = line2.head->llink;
    l = lin->head;
    while (1 )
    {
        if (m->a < n->a)
        {
            l = m;
            m = m->llink;
        }else
        {
            l = n;
            n = n->llink;
        }
        if (n == NULL)
        {
            l->llink = m;
            break;
        }
        if (m == NULL)
        {
            l->llink = n;
            break;
        }
        l = l->llink;
        l = (node*)malloc(sizeof(node));
    }
}
int main()
{
    int n;
    scanf("%d", &n);
    int t=init(&line);
    if (t == 1) printf("success!\n");
    else printf("Fail!\n");
    for (int i = 1; i <= n; i++)
    {
        int key;
        scanf("%d", &key);
        newnode(key, &line);
    }
    scanf("%d", &n);
     t = init(&line2);
    if (t == 1) printf("success!\n");
    else printf("Fail!\n");
    for (int i = 1; i <= n; i++)
    {
        int key;
        scanf("%d", &key);
        newnode(key, &line2);
    }
    t = init(&lin);
    if (t == 1) printf("success!\n");
    donode(&line,&lin);
    out(lin);
    return 0;
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-09-26 21:32
    关注

    修改如下,修改处见注释,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    struct node
    {
        int a;
        struct node* llink;
        struct node* rlink;
    };
    struct q
    {
        int max;
        node* head;
    };
    q line,line2;
    q Llin;  //q lin; 修改
    int init(q* h)
    {
        h->head = (node*)malloc(sizeof(node));
        if (!h->head)
            return 0;
        h->head->rlink = NULL;
        h->head->llink = NULL;
        return 1;
    }
    void newnode(int key, q* h)
    {
        node* m,*n;
        m = h->head;
        if (m == NULL)
        {
            m->a = key;
            m->llink = NULL;
        }
        else
        {
            while (m->llink != NULL)
            {
                m = m->llink;
            }
            n = (node*)malloc(sizeof(node));
            n->a = key;
            m->llink = n;
            n->rlink = m;
            n->llink = NULL;
        }
    }
    void out(q h)
    {
        node* m;
        m = h.head;
        while(m->llink != NULL)
        {
            m = m->llink;
            printf("%d ", m->a);
        }
    }
    
    void donode(q* h,q* lin)
    {
        node* m, * n, *l;
        m = h->head->llink;
        n = lin->head->llink; //n = line2.head->llink;
        l = Llin.head;        //修改
        while (1 )
        {
            if (m->a < n->a)
            {
                l->llink = m;//修改
                l = m;
                m = m->llink;
            }else
            {
                l->llink = n;//修改
                l = n;
                n = n->llink;
            }
            if (n == NULL)
            {
                l->llink = m;
                break;
            }
            if (m == NULL)
            {
                l->llink = n;
                break;
            }
            //l = l->llink;       //修改
            //l = (node*)malloc(sizeof(node));//修改
        }
    }
    int main()
    {
        int n;
        scanf("%d", &n);
        int t=init(&line);
        if (t == 1) printf("success!\n");
        else printf("Fail!\n");
        for (int i = 1; i <= n; i++)
        {
            int key;
            scanf("%d", &key);
            newnode(key, &line);
        }
        scanf("%d", &n);
         t = init(&line2);
        if (t == 1) printf("success!\n");
        else printf("Fail!\n");
        for (int i = 1; i <= n; i++)
        {
            int key;
            scanf("%d", &key);
            newnode(key, &line2);
        }
        t = init(&Llin); //修改
        if (t == 1) printf("success!\n");
        donode(&line,&line2); //修改
        out(Llin);      //修改
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月12日
  • 已采纳回答 10月4日
  • 创建了问题 9月26日

悬赏问题

  • ¥15 ios 新安装app收不到fcm推送
  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥15 应用商店如何检测在架应用内容是否违规?