#include <iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType *elem; //指向数据元素的基地址
int length; //线性表的当前长度
}SqList;
Status InitList_Sq(SqList *L)
{ //构造一个空的顺序表L
L-> elem=new ElemType[MAXSIZE];
if(! L-> elem) exit(OVERFLOW);
L-> length=0;
return OK;
}
Status CreateList_Sq(SqList &L,int n)
{
int i;
if(!L.elem||n<0||n>MAXSIZE) return ERROR;
for(i=1;i<=n;i++)
cin>>L.elem[i-1];
L.length=n;
return OK;
}
void TraverseList_Sq(SqList &L)
{
int i;
cout<<L.elem[0];
for(i=1;i<=L.length-1;i++)
{
cout<<','<<L.elem[i];
}
cout<<endl;
}
void ListInsert_Sq(SqList &L,int e)
{
L.elem[L.length] = e;
L.length++;
}
int ListLength(SqList L)
{
return L.length;
}
void GetElem(SqList L, int i, int &e)
{
e = L.elem[i - 1];
}
bool LocateElem(SqList L, int e)
{
int i;
for (i = 0; i < L.length; i++)
if (e == L.elem[i])
return true;
return false;
}
void MergeList_Sq(SqList L,SqList p,SqList q)
{
int i=1,j=1,e,f;
//下面是主要的算法
while((i<=ListLength(L))&&(j<=ListLength(p))){
GetElem(L,i,e);GetElem(p,j,f);
if(e<=f){
ListInsert_Sq(q,e);i++;
}else {
ListInsert_Sq(q,f);j++;
}
}
while(i<=L.length){
GetElem(L,i,e);
ListInsert_Sq(q,e);
i++;
}
while(j<=p.length){
GetElem(p,j,f);
ListInsert_Sq(q,f);
j++;
}
TraverseList_Sq(q);
}
int main() {
SqList LA, LB,LC;
int m,n;
InitList_Sq(&LA);
InitList_Sq(&LB);
InitList_Sq(&LC);
//提示:请输入有序表LA的元素个数
cin>>n;
//提示:输入有序表LB中的元素
CreateList_Sq(LA, n);
TraverseList_Sq(LA);
//提示:请输入有序表LB的元素个数
cin>>m;
//提示:输入有序表LB中的元素
CreateList_Sq(LB, m);
TraverseList_Sq(LB);
//在此处调用有序表合并函数MergeList_Sq
MergeList_Sq(LA,LB,LC);
//提示:LA和LB合并后的集合LC为:
//TraverseList_Sq(LC);
return 0;
}
/*
思路:::如果LA和LB是两个已经按照递减排列的顺序表,可以进行用两个数分别循环到两个表的最后,在这期间,c=a(a<=b);
c=b(a>b);如果LA先到达末尾,则将剩余的LB表中的元素直接插入到LC即可,如果LB表先到大末尾,同理。
*/