Thread: LinkedList
View Single Post
  #2 (permalink)  
Old 06-25-2008, 06:35 PM
dargueta dargueta is online now
Guru
 
Join Date: Oct 2007
Age: 18
Posts: 858
Last Blog:
Programs Under the Hoo...
Credits: 0
Rep Power: 13
dargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the roughdargueta is a jewel in the rough
Default Re: LinkedList

It looks like you're going in the right direction. However, you do have a few problems. Your search function doesn't do what I think it's supposed to; you're never comparing the node data to the searchItem pointer, and you can't compare unequivalent types. The node variable in that function isn't declared properly, either.

And by the way, I have no idea what the *list pointer is for either. You technically don't need it.

To remove an item from a linked list, do the following:
1) Search through the list until you find the node before the item you want.
2) Save the next pointer of item you want to delete.
3) Set the next pointer of the current node (the node that comes before the node to delete) to the next pointer of the node to be deleted.
4) Delete the node.

Code:
void deleteNthNode(unsigned int n)
{
    Node *curr = head;
    for(unsigned int i = 0; i < n; ++i)
        curr = curr->next;
    //save pointer to node to be deleted
    Node *temp = curr->next;
    //break link by bypassing the node we want to delete
    curr->next = temp->next;
    //delete undesired node
    delete temp;
}
Reply With Quote