非递减有序集合合并

描述
巳知线性表LA和线性表LB中的数据元素按值非递减有序排列,现要求将LA和LB归并为一个新的线性表LC,且LC中的元素仍按值非递减有序排列。

输入
三行,第一行A,B集合的个数n,m
第二行:集合A的数据;
第三行:集合B的数据。
输出
二行,第一行,集合C的个数k
第二行:集合C的数据。
样例输入
11 12
2 4 6 7 8 9 12 34 56 78 89
3 5 7 9 12 34 56 98 234 456 789 1234
样例输出
18
2 3 4 5 6 7 8 9 12 34 56 78 89 98 234 456 789 1234
提示
n,m<255

#include <iostream>
#define max 105
using namespace std;
struct node{
    int data;
    node *prior,*next;
};
class line{
    node *first;
    int sum=0;
public:
    line();
    line(int c[],int n);
    void inset(int x);
    void dele(int x);
    void println();
};
line::line()
{
    first=new node;
    first->prior=nullptr;
    first->next=nullptr;
}
line::line(int a[],int n)
{

    first=new node;
    first->prior=nullptr;
    first->next=nullptr;
    node *s;
    s=new node;
    s->data=a[0];
    s->prior=first;
    s->next=first->next;
    first->next=s;
    sum++;
    for(int i=1;i<n;i++){
            inset(a[i]);
    }
}
void line::inset(int x){
    node *p=nullptr;
    p=first->next;
    while(p->data<x&&p->next!=nullptr){
          p=p->next;
    }
    if(p->data<x){
       node *s=nullptr;
       s=new node;
       s->data=x;
       s->next=p->next;
       p->next=s;
       s->prior=p;
       sum++;
    }
    if(p->data==x) return;
    if(p->data>=x){
       node *s=nullptr;
       s=new node;
       s->data=x;
       s->prior=p->prior;
       s->next=(p->prior)->next;
       (p->prior)->next=s;
       p->prior=s;
       sum++;
    }
}
void line::println()
{
    cout<<sum<<endl;
    node *p=first->next;
    while(p!=nullptr)
        {
            cout<<p->data<<" ";
            p=p->next;
        }
    cout<<endl;
}
int main()
{
    int a;
    int n1,n2;
    cin>>n1>>n2;
    int b1[n1],b2[n2];
    for(int i=0;i<n1;i++){
            cin>>b1[i];
    }
    for(int i=0;i<n2;i++){
            cin>>b2[i];
    }
    line line1{b1,n1};
    for(int i=0;i<n2;i++){
        line1.inset(b2[i]);
    }
    line1.println();
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值