两个顺序表的合并问题
#include<stdio.h>
#define MAXSIZE 300
typedef struct{
int length;
int *p;
}Sqlist;
void SXB(Sqlist &L){
L.length=0;
L.p=new int[MAXSIZE];
}
void insert(Sqlist &L,int n){
if(n>MAXSIZE) printf("input error!");
int i;
for(i=0;i<n;i++)
scanf("%d",&L.p[i]);
L.length=n;
}
void merge(Sqlist &LA,Sqlist &LB,Sqlist &LC,int n){
int *qa,*qb,*qc;
int ia,ib,ic;
LC.length=LA.length+LB.length;
qa=LA.p;qb=LB.p;qc=LC.p;
ia=0,ib=0,ic=0;
while(ia<LA.length&&ib<LB.length){
if(qa[ia]<qb[ib]){
qc[ic++]=qa[ia++];
}
else if(qb[ib]<qa[ia]){
qc[ic++]=qb[ib++];
}
else{
qc[ic++]=qa[ia++];
qb[ib++];
}
}
while(ia<LA.length)
qc[ic++]=qa[ia++];
while(ib<LB.length)
qc[ic++]=qb[ib++];
LC.length=ic;
}
void dayin(Sqlist L,int a){
int i;
for(i=0;i<a;i++)
printf("%d ",L.p[i]);
}
void main(){
Sqlist LA,LB,LC;
int n;
SXB(LA);SXB(LB);SXB(LC);
printf("往LA,LB中插入多少个元素:");
scanf("%d",&n);
printf("往LA中插入的元素有:");
insert(LA,n);
printf("往LB中插入的元素有:");
insert(LB,n);
merge(LA,LB,LC,n);
dayin(LC,LC.length);
}
标签:顺序,两个,int,void,合并,length,MAXSIZE,Sqlist
From: https://blog.csdn.net/laocooon/article/details/137430511