Counting Frequency using linked list

This is a discussion on Counting Frequency using linked list within the C Programming forums, part of the General Programming Boards category; My current code is now Code: #include <stdio.h> #include <ctype.h> typedef struct node { char alpha; int freq; struct node* ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    27

    Unhappy Counting Frequency using linked list

    My current code is now
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    typedef struct node
    {
        char alpha;
        int freq;
        struct node* next;
    
    }NODE;
    
    int main()
    {
        FILE* pInfile = NULL;
        char buffer[500];
        char string[500];
    
        NODE *start = NULL; //list
        NODE *temp;         //temporary node to store data
        NODE *temp2;        //for adding node
        NODE *current;      //node to traverse with
        int i = 0;
        int bCheck = 0;
    
        pInfile = fopen("data.txt","r");
    
        if(pInfile == NULL)
    	{
            printf("Error: cannot open file");
    	}
    
        while ( fgets(buffer,127,pInfile)!= NULL )
        {
            for(i = 0; buffer[i] != '\0' ; i++)
            {
                printf("%d..",i);
                buffer[i] = tolower(buffer[i]);
                printf("%c\n",buffer[i]);
                bCheck = 0;
    
                if(start == NULL)
                {
                    start = (NODE*)malloc(sizeof(NODE));
                    start->alpha = buffer[i];
                    start->freq = 1;
                    start->next = NULL;
    
                    current = start;
                    //start = current;
                }
    
                else if(start != NULL)
                {
                    while(bCheck == 0)
                    {
                        current = start;
                        if((isalpha(buffer[i]))||(isspace(buffer[i])))
                        {
                            printf("is alpha\n");
                            if(current->alpha == buffer[i])
                            {
                                printf("test3\n");
                                current->freq++;
                                bCheck = 1;
                            }
                            else if((current->alpha != buffer[i])&&(current->next == NULL))
                            {
                                printf("test4\n");
                                temp = (NODE*)malloc(sizeof(NODE));
                                temp->alpha = buffer[i];
                                temp->freq = 1;
                                //temp->next = NULL;
    
        printf("TEMP char: %c\t freq: %d\n\n",temp->alpha,temp->freq);
    
                                current->next = temp;
                                //temp->next = NULL;
    
                                /*temp2 = start;
                                while(temp2->next != NULL)
                                {
                                    temp2 = temp2->next;
                                }
                                temp2->next = temp;*/
    
        if(temp->next==NULL)
        printf("2TEMP char: %c\t freq: %d\n\n",temp2->next->alpha,temp2->next->freq);
                                free(temp);
        current = start;
        while(current != NULL)
        {
        printf("current char: %c\t freq: %d\n\n",current->alpha,current->freq);
        current = current->next;
        }
    
    
                                bCheck = 1;
                            }
                            else
                            {
                                printf("test5\n");
                                printf("buffer[i] = %c\n",buffer[i]);
                                printf("current->alpha = %c\n",current->alpha);
                                current = current->next;
                                system("pause");
                            }
                        }
                        else
                        {
                            printf("not alpha\n");
                            break;
                        }
    printf("bCheck = %d\n",bCheck);
                    }
    
                }
    
            }
    
        }
    }
    My problem is in the else if part
    i cannot set temp->next to null because when i set it, it crashes.
    and when i don't set it and print it, it shows only the first letter and not the next letter added in
    for example my text is "It seems that..."
    it prints out

    0..i
    1..t
    is alpha
    test4
    TEMP char: t freq: 1
    current char: i freq: 1
    current char: sth that is not an alphabet freq: 4788232

    then the program crashes !!!

    Please help me :'(
    Last edited by ChoCo; 03-10-2010 at 07:49 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    current = (NODE*)malloc(sizeof(NODE));
                current = start;
    Why are you throwing away what you allocate as soon as you allocate it?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    A bad coding policy I wanted to mention: Just ignore the while and for loops for a second and imagine what the first run through your loop looks like:

    Code:
    /* at the beginning of the code */
    NODE *start = NULL;
     
    /* ...................................... */
    
    /*later in the code*/
    
    current = start;
    So current = NULL. First time you try to access current your program is going to explode. lol... not literally of course

    It's true that you initialize start at some point but it's just bad logic to assign pointers to point to unallocated memory.

    Also, stop casting malloc. There is absolutely no need to do so, if you are using standard C.
    Last edited by claudiu; 03-09-2010 at 11:56 PM.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    i don't really understand what you mean ??? = =

    but i try taking out the allocate thing of current and it's still crashed...

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, so basically you are allocating useless memory. That's the first problem.

    You say:

    Code:
                current = (NODE*)malloc(sizeof(NODE));
                /* current now points to some allocated memory block */
    
                current = start;
                /* current now points to start, which points to NULL, as a result the 
                    memory block allocated is lost in memory and can no longer be freed because
                    you just killed the last pointer pointing to it (current) */
    Fix the logic of your program by making current point to start AFTER you have allocated memory for start.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    I have move the current thing in here, and the program doesn't crash anymore )
    Code:
                if(start == NULL)
                {
                    printf("test1\n");
                    start = (NODE*)malloc(sizeof(NODE));
                    start->alpha = buffer[i];
                    start->freq = 1;
                    start->next = NULL;
                    current = start;
                    printf("%c\n",start->alpha);
                    system("pause");
                }
    but there are warning popping up for the line
    Code:
    temp2 = temp2->next;
    and the others that are similar

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What does the warning say exactly?

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    it's said sth like incompatible type of pointers

    but i think i've figured out my problem in the structure, it should be
    Code:
    typedef struct node
    {
        char alpha;
        int freq;
        struct node* next;
    
    }NODE;
    but now when the program finished and i want to print the frequency of all i counted from this code
    Code:
        current = start;
        while((current->next) != NULL)
        {
            printf(" char: %c\n freq: %d\n\n",current->alpha,current->freq);
            current = current->next;
        }
    it only prints out
    char: i
    freq: 1

    why???

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've still got that line backwards. Assign current to start, not start to current.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    why does i assign current to start??
    i'm confused now = =a

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well my guess is that your program only prints that because there is only one node in the list. Check if your code for adding nodes to the list ever runs, or is just dead code. Have some printfs around there. from what it looks like, you are never entering that branch. Also, your printing while() loop should run until current != NULL not until current->next != NULL. As it is now, you skip printing the last node, because the condition current->next != NULL will be true for the last node.

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    Code:
                        else
                        {
                            current = current->next;
                         }
    my current pointer in here doesn't seem to move anywhere but staying at the start.
    can anyone help me, why is it like this?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Clean it up. No point in allocating a node that you may not use.
    Code:
    while input
        node = inlist( list, character )
        if node
            node->count++
        else
            list = addtolist( list, character )
    There you go.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    my problem now is that i don't know what's wrong about my adding node part T_T;

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Choco what exactly is your program supposed to do?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  3. Delet Frequency in linked list
    By swishiat.com in forum C Programming
    Replies: 16
    Last Post: 12-13-2003, 02:05 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21