栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

6.14 链表合并

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

6.14 链表合并

Link: 4273. 链表合并 - AcWing题库

转置短链表采用 进栈的方法实现 。

输出两个长链表 在输出一个短链表

循环控制如下

代码没按思路走

#include
#include
#include
#include
#include
using namespace std;

typedef struct node{
    int a,next;
}node;

const int maxn=1e5+10;

node a[maxn];

node l1[maxn];
node l2[maxn];
int l1n,l2n;
stack< pair > st;

void fun(int x,int y){
    for(int i=x;i!=-1;i=a[i].next){
        st.push({i,a[i].a});
    }
    int i=y;

    for(;i!=-1;){
        if(a[i].next!=-1)
            printf("%05d %d %05dn",i,a[i].a,a[i].next);
        else
            printf("%05d %d -1n",i,a[i].a);
        i=a[i].next;if(i==-1) return ;

        if(st.size()){
            auto it=st.top();st.pop();
            printf("%05d %d %05dn",i,a[i].a,it.first);
            i=a[i].next;
  
            if(i!=-1)
                printf("%05d %d %05dn",it.first,it.second,i);
            else
                printf("%05d %d -1n",it.first,it.second);
        }else{
            if(a[i].next!=-1)
                printf("%05d %d %05dn",i,a[i].a,a[i].next);
            else
                printf("%05d %d -1n",i,a[i].a);
            i=a[i].next;
        }


    }


int main(){
    l1n=l2n=0;
    int h1,h2,n;
    cin>>h1>>h2>>n;
    int ad;
    for(int i=0;i
        cin>>ad;
        cin>>a[ad].a>>a[ad].next;
    }
    for(int i=h1;i!=-1;i=a[i].next){
        l1n++;
    }
    for(int i=h2;i!=-1;i=a[i].next){
        l2n++;
    }
    if(l1n
        fun(h1,h2);
    }else{
        fun(h2,h1);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977355.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号