描述
巳知线性表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();
}