题目描述
样例输入
2
1 3
3
5 3 7
样例输出
1 3 5 7
源代码
#include<iostream>
#include<malloc.h>
using namespace std;
typedef int ElemType;
const int MaxSize = 1000; //定义最大储存数
class SqList
{
public:
int data[MaxSize]; //结构体数组
int length; // 长度
};
void InitList(SqList*& L) //初始化线性表
{
L = (SqList*)malloc(sizeof(SqList)); //分配空间
L->length = 0; //使长度为0
}
void CreatList(SqList*& L) //建立线性表
{
int n, i, e;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> e;
L->data[i] = e;
L->length++;
}
}
int ListLength(SqList* L)
{
return (L->length);
}
int LocateElem(SqList* L, ElemType e)
{
int i = 0;
while (i < L->length && L->data[i] != e)
i++;
if (i >= L->length)
return 0;
else
return i + 1; //找到返回他的逻辑序号
}
bool GetElem(SqList* L, int i, ElemType& e)
{
if (i<1 || i>L->length) return false;
e = L->data[i - 1];
return true;
}
bool ListInsert(SqList*& L, int i, ElemType e)
{
int j;
if (i<1 || i>L->length+1 || L->length == MaxSize)
return false;
i--;
for (j = L->length; j > i; j--)
{
L->data[j] = L->data[j - 1];
}
L->data[i] = e;
L->length++;
return true;
}
void UnionList(SqList * & LA, SqList *& LB, SqList *& LC)
{
int i, lena;
InitList(LC);
ElemType e;
for (i = 1; i <= ListLength(LA); i++)
{
GetElem(LA, i, e);
ListInsert(LC, i, e);
}
lena = ListLength(LA);
for (i = 1; i <= ListLength(LB); i++)
{
GetElem(LB, i, e);
if (!LocateElem(LA, e))
{
ListInsert(LC, ++lena, e);
}
}
}
void DispList(SqList* L)
{
for (int i = 0; i < L->length; i++)
{
cout << L->data[i] << " ";
}
cout << endl;
}
int main()
{
SqList* L1, *L2, *L3;
InitList(L1);
CreatList(L1);
InitList(L2);
CreatList(L2);
UnionList(L1, L2, L3);
DispList(L3);
return 0;
}