【发布时间】:2018-12-06 21:36:50
【问题描述】:
我想使用TRIE 加载字典。代码的前三部分按预期工作。但是我试图缩短代码的第三部分,然后它不起作用,没有将nodes添加到TRIE。
这里是声明部分:
#define ALPHABET_SIZE 27
// define a node for a TRIE.
struct node
{
_Bool end_of_word;
struct node *next[ALPHABET_SIZE];
};
// create a TRIE
struct node *root = malloc(sizeof(struct node));
// create a mover.
struct node *mover = root;
// read dictionary file
FILE *dictptr = fopen(dictionary, "r");
主要从这里开始:
//load dictionary word by word;
char c;
while ((c = fgetc(dictptr)) != EOF)
{
if (c == '\n')
{
mover->end_of_word = 1;
mover = root;
}
这里是我要优化的地方:
else
{
if (c == '\'')
{
mover->next[ALPHABET_SIZE - 1] = malloc(sizeof(struct node));
mover = &mover->next[ALPHABET_SIZE - 1];
}
else
{
mover->next[c - 97] = malloc(sizeof(struct node));
mover = &mover->next[c - 97];
}
// check if memory allocation is successful.
if (mover == NULL)
{
unload();
fprintf(stderr, "unable to allocate memory to new node.\n");
return false;
}
}
这是我优化的:
else
{
if (c == '\'')
{
mover = &mover->next[ALPHABET_SIZE - 1];
}
else
{
mover = &mover->next[c - 97];
}
mover = malloc(sizeof(struct node));
【问题讨论】:
-
如果你有例如
int a; if (some_condition) { a = 5; } else { a = 10; } a = 15;a在该代码之后的值是多少?在您的“优化”代码的上下文中考虑这一点。 -
谢谢。似乎通过将
mover分配给它的元素,我将该地址的副本从root分配给mover,而不是直接更改root中的地址。对吗? -
这似乎是正确的。
-
我试着从链表的角度做一个图表。似乎在将其子元素值分配给它时,我使
mover指向任何内容。这样,我打破了mover和rootTRIE 之间的联系。这种理解正确吗? -
这是实际代码吗?您分配大小(结构注释)但结构实际上被命名为“节点”
标签: c pointers data-structures malloc trie