1 //将线性表La和Lb合并到新表Lc中 2 #include <stdio.h> 3 #define Maxsize 100 4 #define ERROR 0 5 #define OK 1 6 typedef struct{ 7 int data[Maxsize]; 8 int length; 9 }Sqlist; 10 //声明函数 11 void InsertList(Sqlist *Lc,int n);//将元素n插入到线性表Lc 12 int GetElem(Sqlist *L,int n);//返回线性表L中第N个元素 13 void Insert(Sqlist *Lc);//初始化线性表 14 15 int main(void){ 16 Sqlist La,Lb,Lc; 17 int La_len,Lb_len,i,j,ai,bj; 18 printf("请输入La的长度"); 19 scanf("%d",&La_len); 20 La.length=La_len; 21 printf("请输入Lb的长度"); 22 scanf("%d",&Lb_len); 23 Lb.length=Lb_len; 24 printf("请输入La的元素,用空格隔开"); 25 for(i=0;i<La_len;i++) 26 scanf("%d",&La.data[i]); 27 28 printf("请输入Lb的元素,用空格隔开"); 29 for(i=0;i<Lb_len;i++) 30 scanf("%d",&Lb.data[i]); 31 Insert(&Lc); 32 33 i=1;j=1; 34 while((i<=La.length)&&(j<=Lb.length)){ //La和Lb非空 35 ai=GetElem(&La,i); 36 bj=GetElem(&Lb,j); 37 if(ai<=bj) { 38 InsertList(&Lc,ai); 39 i++; 40 } 41 else{ 42 InsertList(&Lc,bj); 43 j++; 44 } 45 } 46 while(i<=La.length){ 47 ai=GetElem(&La,i); 48 InsertList(&Lc,ai); 49 i++; 50 } 51 while(j<=Lb.length){ 52 bj=GetElem(&Lb,j); 53 InsertList(&Lc,bj); 54 j++; 55 } 56 57 //遍历Lc 58 for(i=0;i<Lc.length;i++) 59 printf("%d ",Lc.data[i]); 60 return 0; 61 } 62 void Insert(Sqlist *Lc){ 63 Lc->length=0; 64 } 65 void InsertList(Sqlist *Lc,int n){ 66 int size; 67 Lc->length++; 68 size=Lc->length; 69 Lc->data[size-1]=n; 70 } 71 int GetElem(Sqlist *L,int n){ 72 return L->data[n-1]; 73 }
来源:https://www.cnblogs.com/suqf/p/9733312.html