
慕後森
首先要确定你想采用線性表的順序存儲結構還是鍊式存儲結構。以順序存儲結構為例:#include #include #define ERROR 0#define OK 1typedef int Status;typedef int ElemType;//順序表測試用const int MaxSize=100; //100隻是示例性的資料,可以根據實際問題具體定義const int Increasement=10;typedef struct{ElemType *elem;int length;int listsize;}SeqList;Status InitList(SeqList &S,int n=MaxSize){//初始化線性表if(n<=0)n=MaxSize;S.elem =(ElemType *)malloc(n*sizeof(ElemType));if(S.elem ==NULL)exit(ERROR);S.length =0;S.listsize =n;return OK;}int ListLength(SeqList &S){ //求線性表的長度return S.length ;}void GetElem(SeqList &S,int i,ElemType &e){//按位查找,取線性表的第i個元素if(i<1 || i>S.length )exit(ERROR);e=S.elem [i-1];}Status ListInsert(SeqList &S,int i, ElemType e){ //線上性表中第i個位置插入值為e的元素if(i<1 || i>S.length+1 )return ERROR;if(S.length >=S.listsize ){ElemType *newbase=(ElemType *)realloc(S.elem,Increasement*sizeof(ElemType));if(newbase==NULL)exit(ERROR);S.elem =newbase;S.listsize =S.listsize+Increasement;}for(int j=S.length -1;j>=i-1;j--)S.elem [j+1]=S.elem [j];S.elem [i-1]=e;S.length++;return OK;}Status Output(SeqList S){//周遊線性表,按序号依次輸出各元素if(S.length ==0)return ERROR;for(int i=0;i=high+1; j--)r[j+1]=r[j];r[high+1]=e;}}void MergeList(SeqList La, SeqList Lb, SeqList &Lc) {// 已知順序表La和Lb中的元素按值非遞減排列。// 歸并La和Lb得到新的順序表Lc,Lc的元素也按值非遞減排列。int La_len, Lb_len;ElemType ai, bj;int i=1, j=1, k=0;InitList(Lc);La_len = ListLength(La);Lb_len = ListLength(Lb);while ((i <= La_len) && (j <= Lb_len)) { // La和Lb均非空GetElem(La, i, ai);GetElem(Lb, j, bj);if (ai <= bj) {ListInsert(Lc, ++k, ai);++i;} else {ListInsert(Lc, ++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);}} // MergeListvoid main(){SeqList La,Lb,Lc;InitList(La,3);InitList(Lb,5);ElemType data;printf("輸入順序表La的元素值:\n");for(int i=1;i<=3;i++){scanf("%d",&data);ListInsert(La, i, data);}fflush(stdin);printf("輸入順序表Lb的元素值:\n");for(i=1;i<=5;i++){scanf("%d",&data);ListInsert(Lb, i, data);}//La和Lb非遞減排序BinInsertSort(La.elem , 3);BinInsertSort(Lb.elem , 5);MergeList(La,Lb,Lc);Output(Lc);}