#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define INFEASIBLR -1
using namespace std;
typedef int Status;
typedef int ElemType;
int k;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SQList;
Status InitList_Sq(SQList &L, int n)
{
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(! L.elem)
exit(1); 存储分配失败
L.length = 0;
L.listsize = LIST_INIT_SIZE;
cout << "请输入" << n << "个表中的数据" << endl;
for(int i = 0;i < n;i++)
{
cin >> L.elem[i];
++L.length;
if(L.length>= L.listsize){
ElemType *newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT)* sizeof(ElemType));
if(!newbase) exit(0);
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
}
return true;
}
Status ListInsert_Sq(SQList &L,int i,ElemType e)
{
//在顺序线性表L中第i个位置之前插入新的元素e
//i的合法值为1<=i<=ListLength_Sq(L)+1
if(i <1 || i> L.length + 1)
return false; //i值不合法
if(L.length >= L.listsize) //当前存储空间已满,增加分配
{
ElemType *newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT )* sizeof(ElemType));
if(!newbase)
exit(1); //存储分配失败
L.elem = newbase;//新基址
L.listsize += LISTINCREMENT; //增加存储容量
}
ElemType *q = &(L.elem[i-1]);//q为插入位置
for(ElemType *p = &(L.elem[L.length-1]);p>=q;--p)
*(p+1) = *p; //插入位置及之后的元素右移
*q = e; //插入e
++L.length; //表长增1
ElemType l = *q; //用一个自定义的变量 返回指针q中所指代的值
return l;
}
void union(List &La, List Lb
)
{
int La_len = ListLeng(La);
int Lb_len = ListLeng(Lb);
for(int i = 1;i <= Lb_len; i++){
GetElem(Lb, i, e);
if(!LocateElem(La, e, equal))
ListInsert(La, ++La_len, e);
}
}
viod MergeList(Lisit La, List Lb, List &Ld){
initList(Ld);
int i, j;
i = j = 1;
k = 0;
int La_len = ListLength(La);
int Lb_len = ListLength(Lb);
while((i <= La_len) && (j <= Lb_len)){
GetElem(La, i, ai);
GetElem(Lb, j, bj);
if(ai <= bj){
ListInsert(Ld, ++k, ai);
++i;
}
else {
ListInsert(Ld, ++k, bj);
++j;
}
}
while(i <= La_len){
GetElem(La, i++ ,ai);
ListInsert(Lc, ++k, ai);
}
while(j <= Lb_len){
GetElem(Lb, j++, bj);
ListInsert(Lc, ++k, bj);
}
}
Status display(SQList &L,int m){
for(int i =0;i <= m;i++){
cout << L.elem[i] << endl;
}
return true;
}
int main(){
cout << "请输入你要插入La线性表的数量na" << endl;
int na; cin >> na;
InitList(La, na);
cout << "请输入你要插入Lb线性表的数量nb" << endl;
int nb; cin >> nb;
InitList(Lb, nb);
initList(Lc);
union(La, Lb);
cout << "Lc为La,Lb表中的数据为:" << endl;
display_sq(Lc, k);
MerList(La, Lb, Ld);
cout << "Lb非递减表中的数据为:" << endl;
display_sq(Ld, k);
return 0
}