#include "stdio.h"
#include "stdlib.h"
# define OVERFLOW -2
# define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
# define List_init_size 100
# define Listincrement 10
typedef struct //定义顺序表
{
ElemType* elem;
int length;
int listsize;
}sqlist;
Status InitList(sqlist& L) //初始化顺序表
{
L.elem = (ElemType*)malloc(List_init_size * sizeof(ElemType));
if (!L.elem) exit(OVERFLOW);
L.length = 0;
L.listsize = List_init_size;
return OK;
}
Status sqlistinput(sqlist& L, int n) //输入函数
{
int i = 0;
if (n > L.listsize) return ERROR;
for (; i < n; i++)
{
scanf("%d", &L.elem[i]);
}
L.length = n;
return OK;
}
Status sqlistoutput(sqlist L) //输出函数
{
int i;
for (i = 0; i < L.length; i++)
printf("%d ", L.elem[i]);
printf("\n");
return OK;
}
void mergelist_sq(sqlist la, sqlist lb, sqlist& lc) //合并函数
{
ElemType* pa, * pb, * pc, * pa_last, * pb_last;
pa = la.elem;
pb = lb.elem;
lc.listsize = lc.length = la.length + lb.length;
pc = lc.elem = (ElemType*)malloc(lc.listsize * sizeof(ElemType));
if (!lc.elem) exit(OVERFLOW);
pa_last = la.elem + la.length - 1;
pb_last = lb.elem + lb.length - 1;
while ((pa <= pa_last) && (pb <= pb_last))
if (*pa <= *pb) {*pc++ = *pa++;}
else *pc++ = *pb++;
while (pa <= pa_last) *pc++ = *pa++;
while (pb <= pb_last) *pc++ = *pb++;
}
Status sortsqlist(sqlist& L) //排序函数
{
int i, j, len;
ElemType t;
len = L.length;
for (i = len - 1; i >= 1; i--)
for (j = 0; j < i; j++)
{
if (L.elem[j] > L.elem[j + 1])
{
t = L.elem[j];
L.elem[j] = L.elem[j + 1];
L.elem[j + 1] = t;
}
}
return OK;
}
void main() //主函数
{
sqlist la, lb, lc;
int n, m;
InitList(la);
InitList(lb);
printf("请输入线性表la的元素个数 n:\n");
scanf("%d", &n);
printf("请输入线性表la的 n 个元素\n");
sqlistinput(la, n);
printf("请输入线性表lb的元素个数:m\n");
scanf("%d", &m);
printf("请输入线性表la的 m 个元素:\n");
sqlistinput(lb, m);
sortsqlist(la);
sortsqlist(lb);
mergelist_sq(la, lb, lc);
printf("la和lb合并后的顺序表lc为:\n");
sqlistoutput(lc);
}