#include <stdio.h> #include <stdlib.h> #define MAX 10 #define ADD 10 #define OVER -2 #define ERROR -1 #define OK 1 typedef int ElemType; typedef struct SqList{ ElemType *elem; int length; int listsize; }SqList; //初始化 int InitList(SqList &L) { L.elem = (ElemType *)malloc(MAX * sizeof(ElemType)); if(!L.elem) { printf("申请空间失败\n"); exit(OVER); } L.length = 0; L.listsize = MAX; return OK; } //插入 int InsertList(SqList &L,int i,ElemType e) { if(i < 1 || i > L.length + 1) { printf("超出范围\n"); exit(OVER); } if(L.length >= L.listsize) { ElemType * newbase = (ElemType *)realloc(L.elem,(L.listsize + ADD) * sizeof(ElemType)); if(!newbase) { printf("空间不足\n"); exit(OVER); } L.elem = newbase; L.listsize += ADD; } ElemType *q,*p; q = &L.elem[i - 1]; for(p = &L.elem[L.length - 1];p >= q;p --) { *(p + 1) = *p; } *q = e; L.length ++; return OK; } //查询 int GetElem(SqList L,int i,ElemType &e) { if(i < 1 || i > L.length) { printf("查无结果\n"); exit(OVER); } e = L.elem[i - 1]; return OK; } //递增排序 int MergeList(SqList La,SqList Lb,SqList &Lc) { int i = 1,j = 1; int k = 0; ElemType e1,e2; while(i <= La.length && j <= Lb.length) { GetElem(La,i,e1); GetElem(Lb,j,e2); if(e1 <= e2) { InsertList(Lc,++k,e1); ++i; } else { InsertList(Lc,++k,e2); ++j; } } while(i <= La.length) { GetElem(La,i ++,e1); InsertList(Lc,++k,e1); } while(j <= Lb.length) { GetElem(Lb,j ++,e2); InsertList(Lc,++k,e2); } return OK; } int main() { SqList La,Lb,Lc; ElemType e; int i = 1; int j; if(InitList(La) == i && InitList(Lb) == i && InitList(Lc) == i) { printf("初始化成功\n"); printf("请为La赋3个递增值:"); for(i = 1;i <= 3;i ++) { scanf("%d",&e); InsertList(La,i,e); } printf("请为La赋4个递增值:"); for(i = 1;i <= 4;i ++) { scanf("%d",&e); InsertList(Lb,i,e); } if(MergeList(La,Lb,Lc) == 1) { printf("操作成功\n"); for(j = 1;j <= La.length + Lb.length;j ++) { GetElem(Lc,j,e); printf("%d ",e); } } } }