Math Dash的c语言有序表的合并

代码如下:
 

#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
9

0
2
4
4
5
5
6
6
7
8

0
0
2
4
4
4
5
5
6
6
6
7
7
8
9
9
请按任意键继续. . .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值