标题:懂赫夫曼树的大哥请进!-->feng1256转移
取消只看楼主
论坛
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:1372
专家分:0
注 册:2006-3-27
 问题点数:0 回复次数:0 
懂赫夫曼树的大哥请进!-->feng1256转移

下面是求编码函数,为什么用#if 0和endif 之间的代码替换静态数组就不行呢?用静态数组可以求的结果,怎么要是改成动态char **c这种形式程序就死啊,退不出来啊!


static void Coding(Huffman T[], int N)
{
char c[20][20] = {'\0'};
int i, j, k, step, father;

#if 0
char **c;
if (((c = (char**)malloc(sizeof(char*) * N))) == NULL)
{
exit(1);
}

for (i = 0; i < N; i++)
{
if (((c[i] = (char*)malloc(sizeof(char) * (N + 1)))) == NULL)
{
exit(1);
}
}

for (i = 0; i < N; i++)
{
for (j = 0; j < N + 1; j++)
{
c[i][j] = '\0';
}
}
#endif

for (i = 0; i < N; i++) /* get every node code */
{
step = i, k = 0;
father = T[step].parent;

while (father != -1)
{
if (T[father].lchild == step)
{
c[i][k++] = '0';
}
else
{
c[i][k++] = '1';
}

step = father;
father = T[step].parent;
}
}

printf("The Coding Is:\n");
for (j = 0; j < N; j++)
{
printf("%s\n", c[j]);
}
}

搜索更多相关主题的帖子: 赫夫曼 char malloc sizeof father 
2006-05-25 19:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-67172-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.126229 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved