集合合并(线性表)

已知集合A与集合B,且第个集合内数据是唯一的。求A,B集合合并成新的集合C,要求C集合内的数据也是唯一的。并指出C集合的个数。

输入
三行,第一行分别为集合A,B的个数
第二行为A集合的数据
第三行为B集合的数据
输出
两行
第一行集合C的个数
第二行为C集合的数据
样例输入
4 5
12 34 56 78
34 67 89 34 76
样例输出
7
12 34 56 78 67 89 76
提示
数据小于30000

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=10000;
template<class T>
struct node
{
   int data;
   //node<T> *r;
    node<T>* next;
};
template<class T>
class lian
{
   node<T> *first;
   node<T> *r;
   public:
   lian();
   lian(T a[],int n);
   void cha(int a);
   void show();
   //void zhishan(T n);
   //void weishan(int n);
};
template <class T>
lian<T>::lian()
{
    first=new node<T>;
    first->next=NULL;
}
template <class T>
lian<T>::lian(T a[],int n)
{
    first=new node<T>;
    //node<T> *r=first;
    r=first;
     node<T> *s=NULL;
    for(int i=0;i<n;i++)
    {
        s=new node<T>;
        s->data=a[i];
        r->next=s;
        r=s;
    }
    r->next=NULL;
}
template <class T>
void lian<T>::cha(int a)
{
    node<T> *s;
    s=new node<T>;
    s->data=a;
     node<T> *p;
    p=first->next;
    while(p!=NULL)
    {
       if(p->data==a)return;
        p=p->next;
    }
    r->next=s;
    r=s;
    r->next=NULL;
}
template <class T>
void lian<T>::show()
{
    node<T> *p;
    p=first->next;
    int t=0,h[maxn];
    while(p!=NULL)
    {
        h[t]=p->data;
        p=p->next;
        t++;
    }
    cout<<t<<endl;
    for(int i=0;i<t;i++)
    {
        cout<<h[i]<<" ";
    }
    cout<<endl;
}
int main()
{
   int a[maxn],b[maxn];
   int x,y;
   cin>>x>>y;
   for(int i=0;i<x;i++)
   {
       cin>>a[i];
   }
   for(int i=0;i<y;i++)
   {
       cin>>b[i];
   }
   lian<int> k(a,x);
 for(int i=0;i<y;i++)
 {
     k.cha(b[i]);
 }
   k.show();

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值