我目前有这个算法:
char** mergeLists(char **a, char **b, int sizeA, int sizeB, int *lSize)
{
char **list = malloc( sizeof(char *) );
int pA = 0, pB = 0, listSize = 0;
while (pA != sizeA && pB != sizeB)
{
list = realloc(list, sizeof(char *) * (++listSize) );
if (strcmp(a[pA], b[pB]) < 0)
{
list[listSize-1] = a[pA];
pA++;
}
else
{
list[listSize-1] = b[pB];
pB++;
}
}
*lSize = listSize;
return list;
}
但它似乎有一些错误。我目前正在运行这个测试:
char *l1[6];
l1[0] = "a";
l1[1] = "b";
l1[2] = "c";
l1[3] = "d";
l1[4] = "e";
l1[5] = "f";
char *l2[6];
l2[0] = "aa";
l2[1] = "ba";
l2[2] = "ca";
l2[3] = "da";
l2[4] = "ea";
l2[5] = "fa";
int s;
char **l = mergeLists(l1, l2, 6, 6, &s);
int i = 0;
for(i = 0; i < s; i++)
printf("%s\n", l[i]);
它正在打印:
a
aa
b
ba
c
ca
d
da
e
ea
f
第二个列表的最后一项丢失了。我认为在while循环的条件下可能是一些错误。
我会很感激一些帮助。谢谢你。