Thread: Is there something wrong with this code?

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    Post Is there something wrong with this code?

    Hi folks, I have another little problem.

    I've been working on a program that's supposed to compare two text files (of lists of words) and see if they have all the same words (regardless of what order they're in).

    What I've done is turned one of the lists into a linked list and then read in one word of the other file and go through the linked list and delete that word. Since I know the files have the same words I should get an empty linked list back.

    But there is a problem with this part of my code.
    Oh and it's in a while loop which is scanning in each word of the other file.
    Code:
    /*comparing scanned in word to current->word while searching down linked list*/
        for(current=current->next; current!=NULL; current=current->next){
            printf("%d ", j); /*j is just for me to check if the loop here works.  It does.*/
            j++;
            if(strcmp(current->data, word)==0){
                deletenode(current, current->data);
            }
        }
    It compiles but it as soon as the program is done printing j, I get an error. I can't really spot wht the error is. All I know is that it isn't really working.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    First of all check if you want to pass something in the function call by value or call by reference.

    If you are sure about this one, post some more code - you posted to little code.
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    This is how i defined the linked list:
    Code:
    /*structure of linked list*/
    struct node{
      char data[9];
      struct node *next;
    };
    typedef struct node NODE;
    and this is the deletenode function:
    Code:
    void deletenode(NODE *temp1, char string[9])
    {
        NODE *temp2;
    
        for(;temp1->next;temp1=temp1->next){
            if(strcmp(temp1->next->data, string)==0){
                temp2=temp1->next;
                temp1->next=temp1->next->next; 
                free(temp2); 
                return;
            }
        }
        printf("deletenode(%s) - data not found in list, could not delete", string);
    }
    I tried using the deletenode function in a smaller example and it seems to work fine.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Ok, i 'll just make a try to help you, without being very sure if my reccomentations will work.

    About your delete function. Why does it need the for loop and the if statement? I think it doesn't. it should just delete the node. The for and the if were in the main().

    In main now(code of first post), i think it would be better to write:
    Code:
    current=current->next; // i don't know if you really need this
    while( current!=NULL ){
            printf("%d ", j); /*j is just for me to check if the loop here works.  It does.*/
            j++;
            if(strcmp(current->data, word)==0)
                deletenode(current, current->data);
    else
    current=current->next;      
        }
    I again tell you that i am not very sure about these changes but give them a try
    Loading.....
    ( Trying to be a good C Programmer )

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    Hey money? the code you suggested actually worked (yay, thanks) but only for the first word I scanned in. so for example in the second list my first word was "house" and then it went through the linked list and deleted "house" but it didn't continue to loop to the next word which is "among". would my problem be in this loop or the larger one outside of this one?

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    I think the prolem is in your delete function. ( i don't know what changes you made to it.. )

    Here is how the delete function should be:
    Code:
    void deletenode( NODE *current )
    {
    NODE *temp;  //not really needed
    temp = current;  //not really needed
    current= current->nextPtr;
    free( temp );     //not really needed
    }
    and the function call:
    Code:
    if(strcmp(current->data, word)==0)
                deletenode(current);
    I think that this functionn and the while loop i posted should work together.
    Check again if you need to pass something to the delete function call by value or call by reference.
    Loading.....
    ( Trying to be a good C Programmer )

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    money, why all the "not really needed" comments in you delete function? If you remove those lines, I presume you'll have a memory leak (guessing here, as I haven't seen all the code).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM