题目描述

源代码
#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;
}
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*& L1, SqList*& L2, SqList*& L3)
{
InitList(L3);
int i = 0, j = 0;
while (i < L1->length && j < L2->length)
{
if (L1->data[i] < L2->data[j])
L3->data[L3->length++] = L1->data[i++];
else
L3->data[L3->length++] = L2->data[j++];
}
while (i < L1->length)
L3->data[L3->length++] = L1->data[i++];
while (j < L2->length)
L3->data[L3->length++] = L2->data[j++];
}
void DispList(SqList* L)
{
for (int i = 0; i < L->length; i++)
{
cout << L->data[i] << " ";
}
}
int main()
{
SqList* L1, *L2, *L3;
InitList(L1);
CreatList(L1);
InitList(L2);
CreatList(L2);
UnionList(L1, L2, L3);
DispList(L3);
return 0;
}