• 欢迎光临~

# 线性表A，B顺序存储合并

### 输出格式:

``代码如下:``
```#include<iostream>
#define MAX 1000
using namespace std;

typedef struct
{
int* elem;
int length;
}SqList;

SqList input(SqList& s)
{
s.elem = new int[MAX];
s.length = 0;
int a, i = 0;
cin >> a;
while (a != -1)
{
if (i == 0)
{
s.elem[i] = a;
cin >> a;
i++;
continue;
}

if (s.elem[i - 1] != a)
{
s.elem[i] = a;
i++;
}
cin >> a;
}
s.length = i;
return s;
}

void add(SqList& a, SqList& b, SqList& c)
{
if (a.length == 0 || b.length == 0) return;
int i = a.length - 1, j = b.length - 1, k = 0;
c.elem = new int[MAX];
c.length = 0;
while (i >= 0 && j >= 0)
{
if (a.elem[i] == b.elem[j])
{
c.elem[k] = a.elem[i];
i--;
j--;
k++;
}
else if (a.elem[i] < b.elem[j])
{
c.elem[k] = a.elem[i];
i--;
k++;
}
else
{
c.elem[k] = b.elem[j];
j--;
k++;
}
}
//A表剩余的元素;
while (i >= 0)
{

c.elem[k] = a.elem[i];
i--;
k++;
}
//B表剩余的元素;
while (j >= 0)
{
c.elem[k] = b.elem[j];
j--;
k++;
}
c.length = k;
}

void print(SqList& c)
{
cout << c.elem[0];
for (int i = 1; i < c.length; i++)
{
cout << "," << c.elem[i];
}
}
int main()
{
SqList A, B, C;
A = input(A);
B = input(B);