好,所以我定義我的結構是這樣的。特里數據結構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? –