Thread: Removing a string - linked list

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    Removing a string - linked list

    I'm trying to go search through my linked list for a passed string and if it matches, remove it...but obviously link everything back together properly. This is what I have so far but when i pass it to my display function, which is properly working, it goes into an endless loop. Any pointers greatly appreciated

    Code:
    void llRemoveString(LinkedList** ll, char* string) {
        LinkedList* newNode = (LinkedList*)malloc(sizeof(LinkedList));
        newNode->value = string;
        LinkedList* n = *ll;
        
        while(n->next != NULL) {
            n = n->next;
            if(n->next->value == newNode->value){
                newNode->next = n->next;
                n->next = newNode;
            }
        }
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    *Another* thread about the same thing? I don't know why I'm even bothering to try to help, but I'll be nice.

    Why are you creating a new node in a function apparently designed to remove nodes? That looks a lot more like a function to insert a node than one to remove a node.

    As to why it infinitely loops, you end up trying to do two insertions of newNode, and end up creating a circularly-linked list of one element, because if your if statement was ever true for one loop (which is less likely than you think because you're doing pointer comparison not string comparison) your if() statement is guaranteed to be true the next iteration of the loop as well, causing newNode to point to itself.

    Even if you had those problems solved your list would be horribly broken if value was not unique in the list, because you'd try to insert the same new node more than once in the list.
    Last edited by Cat; 02-19-2013 at 05:40 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by Cat View Post
    *Another* thread about the same thing? I don't know why I'm even bothering to try to help, but I'll be nice.

    Why are you creating a new node in a function apparently designed to remove nodes? That looks a lot more like a function to insert a node than one to remove a node.

    As to why it infinitely loops, you end up trying to do two insertions of newNode, and end up creating a circularly-linked list of one element, because if your if statement was ever true for one loop (which is less likely than you think because you're doing pointer comparison not string comparison) your if() statement is guaranteed to be true the next iteration of the loop as well, causing newNode to point to itself.

    Even if you had those problems solved your list would be horribly broken if value was not unique in the list, because you'd try to insert the same new node more than once in the list.
    Ok, so in my if statement, how do I compare the value at n's next node to the passed string? Is there a function for that? I'm used to java I'm sorry, I don't know many of the functions in C quite yet


    Code:
    void llRemoveString(LinkedList** ll, char* string) {
        LinkedList* n = *ll;
         
        while(n->next != NULL) {
             if(n->next->value == *string){
                 n->next = n->next->next;
             }
            n = n->next;
        }
    }
    Last edited by johngoodman; 02-19-2013 at 07:03 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    strcmp(3): compare two strings - Linux man page

    Something like:
    Code:
    strcmp(n->next->value, string)
    That will return 0 if they're equal, and non-zero if they're different. Note, you don't want the * in front of string. strcmp takes a char pointer, to the first char in the string. *string dereferences the pointer, giving you a single char. strcmp does not like that.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by anduril462 View Post
    strcmp(3): compare two strings - Linux man page

    Something like:
    Code:
    strcmp(n->next->value, string)
    That will return 0 if they're equal, and non-zero if they're different. Note, you don't want the * in front of string. strcmp takes a char pointer, to the first char in the string. *string dereferences the pointer, giving you a single char. strcmp does not like that.
    Awesome, that fixed it! Thank you guys for the help, I think I'm starting to pick up on this linked list stuff more now

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    How would I take care of these memory leaks then? I'd like to maybe create another function and pass "LL" to it, delete it, and free it. But not sure if that'd take care of all the leaks

  7. #7
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by johngoodman View Post
    How would I take care of these memory leaks then?
    Simple answer: call free() once for each object returned from malloc(). Basically, you should free the node (and probably the string it holds) in the node removal function.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    How would you go about deleting a linked list? just assign it to NULL?

  9. #9
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Would assigning a pointer to NULL call free() once for every object allocated via malloc()? C is not Java. To delete a linked list you have to free all the nodes and (probably) what they contain. THEN you can set the head pointer to NULL to indicate empty list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing node from linked list
    By popnfresh12321 in forum C Programming
    Replies: 6
    Last Post: 10-12-2012, 09:11 AM
  2. Removing from linked list
    By camel-man in forum C Programming
    Replies: 4
    Last Post: 10-10-2011, 05:30 PM
  3. Removing odd indicies from linked list
    By Ryan. in forum C Programming
    Replies: 8
    Last Post: 07-11-2010, 01:09 AM
  4. removing duplicates from a linked list
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2003, 10:27 PM
  5. removing nodes from linked list
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2003, 02:24 PM