Removing an item from a linked list

This is a discussion on Removing an item from a linked list within the C++ Programming forums, part of the General Programming Boards category; So I'm building this linked list, with all the adding, and deleting of Nodes done in the Node class themself ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    12

    Removing an item from a linked list

    So I'm building this linked list, with all the adding, and deleting of Nodes done in the Node class themself (managed by a List class). Now I've got the methods working to add and remove elements to the list (in this case a name that's an array of characters), but I can't quite figure out what to write for a loop to check if the string the user types is actually in the list. While the loop I have in the beginning of the method now allows me to type in any name that's not in the list without giving me errors, it now only allows the first non-dummy Node to be deleted. I'm kind of stumped, any suggestions?



    Code:
    void Node::remove(char n[])    
        {
        if(strcmp(link->name,n)!=0)   // checks to see if character asked to remove exists in the list
            {
            return;
            }    
                    
        if (strcmp(link->name,n)==0)
            {
            Node *temp = new Node(n,link);
    	    link = link->link;
    	    temp->link = NULL;
    	    delete temp;
    	    temp == NULL;
            }    
            
        else
            link->remove(n);		    
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Well link->remove(n); never happens because the two strcmp calls which precede it cover all possible cases.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    12
    Ah, good point, now I feel dumb, of course the 'else' won't be seen if the string asked to remove either is or isn't one in the list Problem fixed. I appreciate the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need urgent help in linked list
    By Cassandra in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2009, 08:47 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 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