算法设计 无序表LA=(2,5,1),LB=(4,3),将LA、LB归并为有序表LC(

算法设计 无序表LA=(2,5,1),LB=(4,3),将LA、LB归并为有序表LC(
算法设计
已知 :无序表LA=(2,5,1),LB=(4,3)
求:将LA、LB归并为有序表LC(利用线性表的基本操作完成)
(提示:创建空表LC;将LA、LB中所有元素,插入到LC中去,每次插入选择恰当的位置,保证LC有序 )
etimes001 1年前 已收到1个回答 举报

michael0104 幼苗

共回答了8个问题采纳率:87.5% 举报

你要写伪码还是C代码

1年前 追问

8

etimes001 举报

C代码

举报 michael0104

//传入LA,LB, LA、LB 元素个数,返回LC
int* merge_array(int *LA, int LA_Size, int *LB, int LB_Size)
{
int *LC= malloc((LA_Size + LB_Size)*sizeof(int)); //结果数组
int i=0;//i 用来指示LA当前元素序号
int j=0;//j 用来指示LB当前元素序号
int k=0;//k 为LC中该元素位置
while (i < LA_Size && j < LB_Size)
{
LC[k++] = LA[i] < LB[j] ? LA[i++] : LB[j++];
}
while (i < LA_Size)
{
LC[k++] = LA[i++];
}
while (j < LB_Size)
{
LC[k++] = LB[j++];
}
return LC;
}
修改几个拼写错误,经过gcc4.3编译正常,请采纳~~

etimes001 举报

malloc' : undeclared identifier
'LA_size' : undeclared identifier
'LB_size' : undeclared identifier
'initializing' : cannot convert from 'int' to 'int *'
qq.obj - 1 error(s), 0 warning(s)

举报 michael0104

#include
...
'LA_size' 和 'LB_size'开始打错了,请用上面的代码
如果你用的是vs或clang
请吧int *LC= malloc((LA_Size + LB_Size)*sizeof(int));
改成int *LC=(int *) malloc((LA_Size + LB_Size)*sizeof(int));

etimes001 举报

error C2065: 'malloc' : undeclared identifier
这个是什么意思,是我系统原因么?我用的是 visual c++ 6.0

举报 michael0104

这是我的全部程序

输出很正常,算法复杂度O(nlogn)

etimes001 举报

谢谢啦~我采用你倒数第二次那个好了~错误最少了,你的全部程序里出现乱码了,我有点看不懂……总觉得好对不起你的样子。。。

举报 michael0104

额,去B的windows, 那你记得首先要对LA和LB排个序
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.016 s. - webmaster@yulucn.com