typedef int Status;
typedef int Bool;
typedef int ElemType;
using namespace std;
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
L.elem=(int *)malloc(LIST_INIT_SIZE *sizeof (int));
if(!L.elem) exit(-2);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
Status xiugai(SqList &L,int i,int e)
{
if(i<1||i>L.length+1)
return 0;
L.elem[i]=e;
}
Status ListInsert_Sq(SqList &L,int i,int e)
{
ElemType *newbase,*q,*p;
if(i<1||i>L.length+1)
return 0;
if(L.length>=L.listsize)
{
newbase = (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof (ElemType));
if (!newbase)
{
printf("OVERFLOW"); // 存储分配失败
return ERROR;
}
L.elem = newbase;// 新基址
L.listsize += LISTINCREMENT; // 增加存储容量
}
q = &(L.elem[i-1]);
for (p = &(L.elem[L.length-1]); p >= q; --p)
*(p+1) = *p;
*q = e;
++L.length;
return OK;
}
Status ListDelete_Sq(SqList &L,int i)
{
ElemType *p,*q;
if (( i < 1) || ( i > L.length))
return ERROR;
p = &(L.elem[i-1]);
q = L.elem+L.length-1;
for (++p; p <= q; ++p)
*(p-1) = *p;
L.length--;
return OK;
}
void sortList_Sq(SqList &L)
{
int len=L.length;
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(L.elem[i]>=L.elem[j])
{
int t=L.elem[i];
L.elem[i]=L.elem[j];
L.elem[j]=t;
}
}
}
}
void MergeList_Sq(SqList La, SqList Lb, SqList &Lc)
{
InitList_Sq(Lc);
int i=1,j=1,k = 0,La_len,Lb_len,ai,bj;
La_len = La.length;
Lb_len = Lb.length;
while ((i <= La_len) && (j <= Lb_len))
{
ai=La.elem[i];
bj=Lb.elem[j];
if (ai <= bj)
ListInsert_Sq(Lc, ++k, ai),++i;
else
ListInsert_Sq(Lc, ++k, bj),++j;
}
while (i <= La_len)
{
xiugai(La, i++, ai);
ListInsert_Sq(Lc, ++k, ai);
}
while (j <= Lb_len)
{
xiugai(Lb, j++, bj);
ListInsert_Sq(Lc, ++k, bj);
}
}
void chaji(SqList La,SqList Lb,SqList &Lc)
{
int len1= La.length,len2=Lb.length;
int i=0,j=0,k=1;
while(i<len1&&j<len2)
{
if(La.elem[i]<Lb.elem[j])
{
ListInsert_Sq(Lc,k++,La.elem[i]);
i++;
}
else if(La.elem[i]>Lb.elem[j])
j++;
else i++,j++;
}
while(i<len1)
{
ListInsert_Sq(Lc,k++,La.elem[i]);
i++;
}
}
int main()
{
SqList L,P,U,N,C;
InitList_Sq(L);
InitList_Sq(P);
InitList_Sq(C);
for(int i=1;i<=5;i++)
{
ListInsert_Sq(L,i,i);
cout<<L.elem[i-1]<<' ';
}
cout<<endl;
for(int i=1;i<=5;i++)
{
ListInsert_Sq(P,i,i+3);
cout<<P.elem[i-1]<<' ';
}
cout<<endl;
sortList_Sq(L);
sortList_Sq(P);
chaji(L,P,C);
int len=C.length;
for(int i=0;i<len;i++)
cout<<C.elem[i]<<' ';
cout<<endl;
return 0;
}