我正在做一个项目,我从文件中读取单词,将它们添加到链接列表中,然后计算单词出现的频率。我的程序正在将单词读入链接列表,但它不会在每次出现重复单词时增加计数 - 计数保持在 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;
}
该程序的示例运行将是:
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新手!
问候