在trie结构中添加功能无法正常工作

时间:2017-01-03 07:27:59

标签: c struct trie

我试图编写一个函数来在trie中添加单词,并计算它添加的次数。但它总是返回1。 有人能帮助我吗?

    struct node
{
    struct node* next;
    struct node* child;
    char data;
    int value;
};
typedef struct node Node;
Node* getNode(char data)
{
    Node* newptr;
    newptr = calloc(1,sizeof(Node));
    newptr->next = NULL;
    newptr -> child = NULL;
    newptr->data = data;
    newptr -> value = 0;
    return newptr;
}

Node* add(Node* parent,char* str)
{
    Node* current;
    current = parent->child;
    while (current != NULL && current->data != str[0])
    {
        current = current->next;
    }
    if (current == NULL)
    {
        current = getNode(str[0]);
        if (str[0] == 0)
        {
            current->value = 1;
            return current;
        }
        return add(current,str + 1);
    }
    if (str[0] == 0)
    {
        (current->value)++;
        return current;
    }
    return add(current,str+1);
}

cm:添加了getNode()和节点定义!

0 个答案:

没有答案