好,所以我定义我的结构是这样的。特里数据结构C
struct trie {
struct trie *child[26];
int count;
char letter;
};
问题是当我尝试用词语填充我的词条时,我得到了段错误。 我被告知,问题是孩子变量没有指向任何东西,并将它们设置为NULL会解决这个问题。另外创建第二个结构将是实现这一目标的好方法。我是C编程新手,对如何创建第二个结构来实现这一点感到困惑。任何帮助将非常感激。
int addWordOccurrence(const char* word)
{
struct trie *root;
root = (struct trie *)malloc(sizeof(struct trie*));
struct trie *initRoot=root;
int count;
int x=strlen(word);
printf("%d",x);
int i;
for(i=0; i<x; i++)
{
int z=word[i]-97;
if(word[i]=='\n')
{
z=word[i-1]-97;
root->child[z]->count++;
root=initRoot;
}
root->child[z] = (struct trie *)malloc(sizeof(struct trie));
root->child[z]->letter=word[i];
root->child[z]=root;
}
return 0;
}
你必须在'child'指针分配内存。你知道'malloc' /'calloc'?或者你创建其他'trie's并把它们放在你能告诉我们你的代码? – birryree
这是C或C++?这些问题的答案会疯狂地不同。 –
其中,是代码填充你的trie? –