Пример #1
0
/** Inserts a word into the wordDictionary. */
static void wordDictionaryInsert(WordDictionary* obj, char* word)
{
    while (*word != '\0') {
        int pos = *word - 'a' + 1;
        obj->letters[pos] = *word++;
        if (obj->subs[pos] == NULL) {
            obj->subs[pos] = wordDictionaryCreate();
        }
        obj = obj->subs[pos];
    }
    obj->letters[0] = '\0';
}
/** Inserts a word into the data structure. */
void addWord(struct WordDictionary* wordDictionary, char* word) {
    int i,lens;
    struct WordDictionary *temp,*t;
    temp=wordDictionary;
    lens=strlen(word);
    for(i=0;i<lens;i++){
        if(temp->next[word[i]-'a']==NULL){
            t=wordDictionaryCreate();
            temp->next[word[i]-'a']=t;
            temp=t;
        }else{
            temp=temp->next[word[i]-'a'];
        }
        if(i+1==lens) temp->isend=1;
    }
}
Пример #3
0
int main(void)
{
    char *word1 = "abc";
    char *word2 = "ab";
    char *word3 = "...";

    WordDictionary* obj = wordDictionaryCreate();
    wordDictionaryInsert(obj, word1);
    printf("%s\n", wordDictionarySearch(obj, word1) ? "true" : "false");
    printf("%s\n", wordDictionarySearch(obj, word2) ? "true" : "false");
    wordDictionaryInsert(obj, word2);
    printf("%s\n", wordDictionarySearch(obj, word2) ? "true" : "false");
    wordDictionaryInsert(obj, word2);
    printf("%s\n", wordDictionarySearch(obj, word3) ? "true" : "false");
    wordDictionaryFree(obj);
    return 0;
}