I'm doing a project where I read words from a file, add them into a linked list and then count the frequencies in which the words occur. My program is reading the words into the linked list, but it's not incrementing the count every time a duplicate word occurs- the count is staying at 1. I'm not going to paste my entire code, just the parts that apply.
struct node {
struct node *next;
char word[60];
int wordCount;
};
And the push function:
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;
}
An example run of this program would be:
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
Now, as you can see the counter at the end of each line is staying at 1, however with each duplicate word it should increment, and duplicate words should also not be added to the linked list. I'm sorry about this, I'm new to C!
Regards