结构中的C增量计数器

结构中的C增量计数器,c,pointers,auto-increment,C,Pointers,Auto Increment,我正在做一个项目,从文件中读取单词,将它们添加到一个链接列表中,然后计算单词出现的频率。我的程序正在将单词读入链表,但它不会在每次出现重复单词时增加计数-计数保持在1。我不打算粘贴我的全部代码,只粘贴应用的部分 struct node { struct node *next; char word[60]; int wordCount; }; 和推送功能: void push_front (struct node **list, char * n) { assert (

我正在做一个项目,从文件中读取单词,将它们添加到一个链接列表中,然后计算单词出现的频率。我的程序正在将单词读入链表,但它不会在每次出现重复单词时增加计数-计数保持在1。我不打算粘贴我的全部代码,只粘贴应用的部分

struct node {
    struct node *next;
    char word[60];
    int wordCount;
};
和推送功能:

void push_front (struct node **list, char * n) {

assert (list);
int count = 0;

struct node *temp = (struct node *)malloc (sizeof (struct node));

if (!temp) {
    fprintf (stderr, "Out of memory\n");
    exit (1);
}
if (list == NULL) {
    temp->next = *list;
    strcpy(temp->word, n);
    temp->wordCount+=1;
    *list = temp;
} else {
    while (list) {
        if (temp->word == n) {
            temp->wordCount+=1;
            exit(1);
        } else {
            temp->next = *list;
            strcpy(temp->word, n);
            temp->wordCount+=1;
            *list = temp;
            break;
        }
    }
}
return;
}
void push_front (struct node **list, char * n) {    
    assert (list);
    struct node *temp = *list;

    while (temp) {
        if (strcmp(temp->word, n) == 0) {
            temp->wordCount+=1;
            return;
        }
        temp = temp->next;
    }

    temp = (struct node *)malloc (sizeof (struct node));
    if (!temp) {
        fprintf (stderr, "Out of memory\n");
        exit (1);
    }
    temp->next = *list;
    strcpy(temp->word, n);
    temp->wordCount=1;
    *list = temp;
    return;

}
该程序的运行示例如下:

Word [0] = you, 1
Word [1] = you, 1
Word [2] = are, 1
Word [3] = a, 1
Word [4] = whipped, 1
Word [5] = what, 1
Word [6] = what, 1
Word [7] = you, 1
Word [8] = world, 1
Word [9] = you, 1
Word [10] = hello, 1
现在,正如您所看到的,每行末尾的计数器保持在1,但是对于每个重复的单词,它应该递增,并且重复的单词也不应该添加到链接列表中。我很抱歉,我是C的新手


关于以下比较

if (temp->word == n) { 
是指针(地址)的比较,而不是字符串的比较

C中的字符串比较不应按上述方式进行

您可以使用
中的
strcmp
#include

您的函数包含一些需要修复的bug。我重新设计了您的功能:

void push_front (struct node **list, char * n) {

assert (list);
int count = 0;

struct node *temp = (struct node *)malloc (sizeof (struct node));

if (!temp) {
    fprintf (stderr, "Out of memory\n");
    exit (1);
}
if (list == NULL) {
    temp->next = *list;
    strcpy(temp->word, n);
    temp->wordCount+=1;
    *list = temp;
} else {
    while (list) {
        if (temp->word == n) {
            temp->wordCount+=1;
            exit(1);
        } else {
            temp->next = *list;
            strcpy(temp->word, n);
            temp->wordCount+=1;
            *list = temp;
            break;
        }
    }
}
return;
}
void push_front (struct node **list, char * n) {    
    assert (list);
    struct node *temp = *list;

    while (temp) {
        if (strcmp(temp->word, n) == 0) {
            temp->wordCount+=1;
            return;
        }
        temp = temp->next;
    }

    temp = (struct node *)malloc (sizeof (struct node));
    if (!temp) {
        fprintf (stderr, "Out of memory\n");
        exit (1);
    }
    temp->next = *list;
    strcpy(temp->word, n);
    temp->wordCount=1;
    *list = temp;
    return;

}
在main()中,应该以这种方式调用函数

void main() {

    node *head = NULL;

    push_front (&head, "toto");
    push_front (&head, "titi");
    push_front (&head, "toto");
    push_front (&head, "titi");

    node *tmp;
    int i=0;
    for (tmp=head; tmp!=NULL; tmp = tmp->next)
        printf("Word[%d] = %s, %d\n", i++, tmp->word, tmp->wordCount);

}
我对其进行了测试,执行结果如下:

$ ./test
Word[0] = titi, 2
Word[1] = toto, 2
不会有帮助,因为您无法将字符串值与
=
运算符进行比较,请使用库函数strcmp,您将得到正确的结果。此外,请参见:

else {
    while (list) {
        if (temp->word == n) {
            temp->wordCount+=1;
            exit(1);
        } else {
            temp->next = *list;
            strcpy(temp->word, n);
            temp->wordCount+=1;
            *list = temp;
            break;
        }
    }
}
当您进入else案例时,您没有将
temp
list
链接。那样做。
我还认为它应该是:
while(*list)

不是一个完全相同的副本,但也许这会让我们思考一些事情?谢谢,我已经使用了这种技术,但仍然没有乐趣。我不知道我错过了什么!谢谢,我已经试过了。谢谢你的指针。@drizzy我试图重新编写你的函数,它包含了一些错误。为此,它提供了巨大的帮助!