http://acm.hdu.edu.cn/showproblem.php?pid=1412
刚学数据结构 学了有序表合并 就想用链表写 (其实这是一道很水的题,自己无非是想加强一下自己的数据结构)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#define MAX 20005
using namespace std;
int a[10005],b[10005];
typedef struct
{
int *data;//存储空间基地址
int Length;
}SqList;
void InitList(SqList &L)
{
L.data=new int[MAX];
L.Length=0;
}
void creatList(SqList &L,int n)
{
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
for(i=0;i<n;i++)
L.data[i]=a[i];
L.Length=n;
}
int LocateElem(SqList L,int e)
{
int i;
for(i=0;i<L.Length;i++)
if(L.data[i]==e)
return i+1;
return 0;
}
void unionList(SqList &L1, SqList &L2, SqList &L3 )
{
int i,j;
InitList(L3);
for(i =0;i<L1.Length;i++)
{
L3.data[i]=L1.data[i];
}
for(j=0;j<L2.Length;j++)
{
if(!LocateElem(L1,L2.data[j]))
{
L3.data[i]=L2.data[j];
++i;
}
}
L3.Length=i;
}
int main()
{
int i;
int m,n;SqList L1,L2,L3;
while(cin>>m>>n)
{
InitList(L1);
creatList(L1,m);
InitList(L2);
creatList(L2,n);
unionList(L1,L2,L3);
for(i=0;i<L3.Length;i++)
{
b[i]=L3.data[i];
}
sort(b,b+L3.Length);
for(i=0;i<L3.Length-1;i++)
cout<<b[i]<<" ";
cout<<b[L3.Length-1]<<endl;
}
return 0;
}