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 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路