代码如下:
#include <stdio.h>
#include <stdlib.h>
struct SquList
{
int *data;
int length;
};
main();
void jie_jian_system();
struct SquList *a0();
struct SquList *a1();
struct SquList *a2(struct SquList c,struct SquList d);
main()
{
jie_mian_system();
return 0;
}
void jie_mian_system()
{
srand((unsigned)time(NULL));
struct SquList *a;
a=a0();//*创建第一个有序表
struct SquList *b;
b=a1();//*创建第二个有序表
int c;
for(c=0;c<b->length;c++)
{
printf("%d\n",b->data[c]);
}
printf("\n");
for(c=0;c<a->length;c++)
{
printf("%d\n",a->data[c]);
}
struct SquList *h;
h=a2(*a,*b);//*合并两个有序表
int o;
printf("\n");
for(o=0;o<h->length;o++)
{
printf("%d\n",h->data[o]);
}
system("pause");
}
struct SquList *a0()
{
struct SquList *a;
a=(struct SquList*)malloc(10*sizeof(struct SquList));
a->data=(int*)malloc(10*sizeof(int));
for(a->length=0;a->length<10;a->length++)
{
a->data[a->length]=rand()%10;
}
int c,d;
for(c=0;c<10;c++)
{
for(d=0;d<9;d++)
{
if(a->data[d]>a->data[d+1])
{
int e;
e=a->data[d];
a->data[d]=a->data[d+1];
a->data[d+1]=e;
}
}
}
return a;
}
struct SquList *a1()
{
struct SquList *b;
b=(struct SquList*)malloc(6*sizeof(struct SquList));
b->data=(int*)malloc(6*sizeof(int));
for(b->length=0;b->length<6;b->length++)
{
b->data[b->length]=rand()%10;
}
int g,f;
g=0;
for(f=0;f<6;f++)
{
for(g=0;g<5;g++)
{
if(b->data[g]>b->data[g+1])
{
int e;
e=b->data[g];
b->data[g]=b->data[g+1];
b->data[g+1]=e;
}
}
}
return b;
}
struct SquList *a2(struct SquList c,struct SquList d)
{
struct SquList *a,*b;
a=&c;
b=&d;
int i=a->length+b->length;
struct SquList *h;
h=(struct SquList*)malloc(sizeof(struct SquList));
h->data=(int*)malloc(i*sizeof(int));
h->length=i;
int j=0,k=0,l=0;
while((j<a->length)&&(k<b->length))
{
if(a->data[j]>=b->data[k])
{
h->data[l]=b->data[k];
k++;
}
else
{
h->data[l]=a->data[j];
j++;
}
l++;
}
int m,n;
for(m=k;m<b->length;m++)
{
h->data[l]=b->data[m];
l++;
}
for(n=j;n<a->length;n++)
{
h->data[l]=a->data[n];
l++;
}
return h;
}
程序运行结果如下:
0
4
6
7
9
90
2
4
4
5
5
6
6
7
80
0
2
4
4
4
5
5
6
6
6
7
7
8
9
9
请按任意键继续. . .