3

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

drizzy
  • 139
  • 2
  • 15
  • Not an exact duplicate but maybe this will give things to think about? http://stackoverflow.com/questions/3655728/increment-value-of-int-being-pointed-to-by-pointer – John Apr 11 '13 at 16:20
  • Thanks, I have incrementing using that technique but still no joy. I have no idea what I'm missing here! – drizzy Apr 11 '13 at 16:24

2 Answers2

4

The following comparison

if (temp->word == n) { 

is a comparison of pointers (addresses) and not comparison of strings

String comparison in C should not done in the above way

You could use the strcmp from #include <string.h>:

if (strcmp(temp->word, n)==0) {

And your function contains some bugs to fix. I re-work your function:

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;

}

in the main() your function should called in this way

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);

}

I tested it and the execution gave the following output:

$ ./test
Word[0] = titi, 2
Word[1] = toto, 2
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0
if (temp->word == n)

Will not help,because you cant compare string values with an == operator,use library function strcmp and you'll get the right result.Also, here:

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;
        }
    }
}

When you go into the else case , you haven't linked temp with list. Do that. I also think it should be : while(*list)

10111
  • 47
  • 10