数据结构:顺序表的合并(C语言)

2025-04-05 09:08:21
推荐回答(1个)
回答1:

#include

#define NUM 20

void p1(int x[] , int y[] , int i , int j);

void p2(int x[] , int y[] , int i , int j);

void p3(int x[] , int y[] , int i , int j);

void p1(int x[] , int y[] , int i , int j)

{

int m,n,a,b;

int k = 0 ,g = 0;

int c[NUM];

for(m = 0 ; m < i ; m ++ )

{ for(n = 0 ; n < j ; n ++ )

{

if(x[m] == y[n]) {x[m] = 0 ; y[n] = 0;break;} //相同元素赋值0

}

}

for( a = 0 ; a < i ; a++)

{

if( x[a] != 0 ) c[k++] = x[a] ;

else continue;

}

for( b = 0 ; b < j ; b++)

{

if( y[b] != 0 ) c[k++] = y[b] ;

else continue;

}

while(c[g])

{printf("%d ",c[g++]);}

printf("\n");

}

void p2(int x[] , int y[] , int i , int j)

{ int m, n,a,b,q ,temp;

int c[NUM];

for( m = 0 ; m < i ; m++)

{c[m] = x[m];}

for( n = 0 ; n < j ; n ++)

{c[n+m] = y[n];}

for( a = 0 ; a < n ; a++)

for(b = 0 ; b < n ; b ++)

{

if(c[b]>c[b+1]) {temp = c[b] ; c[b] = c[b+1] ; c[b+1] = temp ; }

}

for( q = 0 ; q < n+m ; q ++)

{printf("%d ",c[q]);

}
printf("\n");
}

void p3(int x[] , int y[] , int i , int j)

{int m, n,a,b,q ,temp;

int c[NUM];

for( m = 0 ; m < i ; m++)

{c[m] = x[m];}

for( n = 0 ; n < j ; n ++)

{c[n+m] = y[n];}

for( a = 0 ; a < n ; a++)

for(b = 0 ; b < n ; b ++)

{

if(c[b]
}

for( q = 0 ; q < n+m ; q ++)

{printf("%d ",c[q]);

}

}

int main()

{

int a[4]={3,5,8,11};

int b[7]={2,6,8,9,11,15,20};

// p1(a,b,4,7);

p2(a,b,4,7);

// p3(a,b,4,7);

return 0;

}