2个回答
展开全部
代码写好了。
经测试VC下通过。
#include <stdio.h>
int merge(int *a,int n1,int *b,int n2,int *s)
{
int i=0,j=0,k=0;
while(i<n1&&j<n2)
{
if(a[i]>b[j]){
if(k&&s[k-1]==b[j])//有重复元素
j++;
else
s[k++]=b[j++];
}
else{
if(k&&s[k-1]==a[i])//有重复元素
i++;
else
s[k++]=a[i++];
}
}
while(i<n1){
if(k&&s[k-1]==a[i])
i++;
else
s[k++]=a[i++];
}
while(j<n2){
if(k&&s[k-1]==b[j])//有重复元素
j++;
else
s[k++]=b[j++];
}
return k;
}
int main()
{
int a[100],b[100],s[200],n1,n2,i,n3;
printf("输入第一个数组元素个数:");
scanf("%d",&n1);
printf("输入%d个升序元素(空格隔开):",n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("输入第二个数组元素个数:");
scanf("%d",&n2);
printf("输入%d个升序元素(空格隔开):",n2);
for(i=0;i<n2;i++)
scanf("%d",&b[i]);
n3=merge(a,n1,b,n2,s); //a和b数组的元素合并到s数组里去(并消除重复元素),并返回合并后数组的元素个数
printf("合并后元素数组为:");
for(i=0;i<n3;i++)
printf("%d ",s[i]);
printf("\n");
return 0;
}
附测试数据一组:
5
3 50 50 100 100
3
8 50 80
输出为:3 8 50 80 100
经测试VC下通过。
#include <stdio.h>
int merge(int *a,int n1,int *b,int n2,int *s)
{
int i=0,j=0,k=0;
while(i<n1&&j<n2)
{
if(a[i]>b[j]){
if(k&&s[k-1]==b[j])//有重复元素
j++;
else
s[k++]=b[j++];
}
else{
if(k&&s[k-1]==a[i])//有重复元素
i++;
else
s[k++]=a[i++];
}
}
while(i<n1){
if(k&&s[k-1]==a[i])
i++;
else
s[k++]=a[i++];
}
while(j<n2){
if(k&&s[k-1]==b[j])//有重复元素
j++;
else
s[k++]=b[j++];
}
return k;
}
int main()
{
int a[100],b[100],s[200],n1,n2,i,n3;
printf("输入第一个数组元素个数:");
scanf("%d",&n1);
printf("输入%d个升序元素(空格隔开):",n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("输入第二个数组元素个数:");
scanf("%d",&n2);
printf("输入%d个升序元素(空格隔开):",n2);
for(i=0;i<n2;i++)
scanf("%d",&b[i]);
n3=merge(a,n1,b,n2,s); //a和b数组的元素合并到s数组里去(并消除重复元素),并返回合并后数组的元素个数
printf("合并后元素数组为:");
for(i=0;i<n3;i++)
printf("%d ",s[i]);
printf("\n");
return 0;
}
附测试数据一组:
5
3 50 50 100 100
3
8 50 80
输出为:3 8 50 80 100
展开全部
#include
#include
/*线性表*/
struct TLink {
int data;
struct TLink * next;
};/*end struct TLink*/
/*生成新元素*/
struct TLink * new_item(int number)
{
struct TLink * r = 0;
r = (struct TLink *)malloc(sizeof(struct TLink));
r->data = number;
r->next = 0;
return r;
}/*end new_item*/
/*在线性表中查询数据*/
struct TLink * lookup(struct TLink * root, int number)
{
struct TLink * h = root;
while(h) {
if (h->data == number) return h;
h = h->next ;
}/*end lookup*/
return 0;
}
/*在线性表中追加一个数据*/
void append(struct TLink * * root, int number)
{
struct TLink * r = 0, * n = 0;
if (!root) return ;
/*不记录重复元素*/
if (lookup(*root, number)) return;
/*如果表为空则新建表*/
r = *root;
if (!r) {
*root = new_item(number);
return ;
}/*end if*/
/*为保证为有序线性表,如果数据比表头还小则作为表头*/
if (number r->data ) {
n = new_item(number);
n->next = r;
*root = n;
return ;
}/*end if*/
/*在有序线性表中查找位置插入元素*/
while(r) {
n = r->next ;
/*如果已经是表尾则直接追加*/
if (!n) {
n = new_item(number);
r->next = n;
return ;
}/*end if*/
/*在中央某处插入*/
if (number n->data ) {
r->next = new_item(number);
r->next->next = n;
return ;
}/*end if*/
r = n;
}/*end while*/
}/*end append*/
/*打印有序线性表*/
void print(struct TLink * root)
{
struct TLink * r = root;
printf("【");
while(r) {
printf("%d ", r->data );
r = r->next ;
}/*end while*/
printf("\b】\n");
}/*end print*/
/*将有序线性表h1合并至有序线性表h0,并销毁线性表h1*/
void merge(struct TLink ** h0, struct TLink ** h1)
{
struct TLink * h = 0, * k = 0;
if (!h0 || !h1) return ;
h = *h1;
while(h) {
append(h0, h->data );
k = h;
h = h->next ;
free(k);
}/*end h*/
h1 = 0;
}
int main(void)
{
int i = 0; struct TLink * x=0, *y = 0;
int a[] = {8,4,3,9,5,1};
int b[] = {7,2,1,5,6,0};
printf("原数据为:\n数组A:【");
for(i = 0; i < 6; i++) {
printf("%d ", a[i]);
append(&x, a[i]);
}/*next*/
printf("\b】\n数组B:【");
for(i = 0; i < 6; i++) {
printf("%d ", b[i]);
append(&y, b[i]);
}/*next*/
printf("\b】\n转换为有序线性表\nA:");
print(x);
printf("B:");
print(y);
printf("AB合并后为:");
merge(&x, &y);
print(x);
return 0;
}
#include
/*线性表*/
struct TLink {
int data;
struct TLink * next;
};/*end struct TLink*/
/*生成新元素*/
struct TLink * new_item(int number)
{
struct TLink * r = 0;
r = (struct TLink *)malloc(sizeof(struct TLink));
r->data = number;
r->next = 0;
return r;
}/*end new_item*/
/*在线性表中查询数据*/
struct TLink * lookup(struct TLink * root, int number)
{
struct TLink * h = root;
while(h) {
if (h->data == number) return h;
h = h->next ;
}/*end lookup*/
return 0;
}
/*在线性表中追加一个数据*/
void append(struct TLink * * root, int number)
{
struct TLink * r = 0, * n = 0;
if (!root) return ;
/*不记录重复元素*/
if (lookup(*root, number)) return;
/*如果表为空则新建表*/
r = *root;
if (!r) {
*root = new_item(number);
return ;
}/*end if*/
/*为保证为有序线性表,如果数据比表头还小则作为表头*/
if (number r->data ) {
n = new_item(number);
n->next = r;
*root = n;
return ;
}/*end if*/
/*在有序线性表中查找位置插入元素*/
while(r) {
n = r->next ;
/*如果已经是表尾则直接追加*/
if (!n) {
n = new_item(number);
r->next = n;
return ;
}/*end if*/
/*在中央某处插入*/
if (number n->data ) {
r->next = new_item(number);
r->next->next = n;
return ;
}/*end if*/
r = n;
}/*end while*/
}/*end append*/
/*打印有序线性表*/
void print(struct TLink * root)
{
struct TLink * r = root;
printf("【");
while(r) {
printf("%d ", r->data );
r = r->next ;
}/*end while*/
printf("\b】\n");
}/*end print*/
/*将有序线性表h1合并至有序线性表h0,并销毁线性表h1*/
void merge(struct TLink ** h0, struct TLink ** h1)
{
struct TLink * h = 0, * k = 0;
if (!h0 || !h1) return ;
h = *h1;
while(h) {
append(h0, h->data );
k = h;
h = h->next ;
free(k);
}/*end h*/
h1 = 0;
}
int main(void)
{
int i = 0; struct TLink * x=0, *y = 0;
int a[] = {8,4,3,9,5,1};
int b[] = {7,2,1,5,6,0};
printf("原数据为:\n数组A:【");
for(i = 0; i < 6; i++) {
printf("%d ", a[i]);
append(&x, a[i]);
}/*next*/
printf("\b】\n数组B:【");
for(i = 0; i < 6; i++) {
printf("%d ", b[i]);
append(&y, b[i]);
}/*next*/
printf("\b】\n转换为有序线性表\nA:");
print(x);
printf("B:");
print(y);
printf("AB合并后为:");
merge(&x, &y);
print(x);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询