Deleting A Node From A Linked List

This is a discussion on Deleting A Node From A Linked List within the C++ Programming forums, part of the General Programming Boards category; My program has a node I want to delete and my nodes don't seem to be connecting properly when I ...

  1. #1
    Anonymous Freak
    Guest

    Deleting A Node From A Linked List

    My program has a node I want to delete and my nodes don't seem to be connecting properly when I print. Does anyone know why this delete function doesn't work properly.
    Code:
    struct  NodeType;		// Forward declaration
    struct NodeType			//double link node 
    {
        int component;
        NodeType*		link;
    	NodeType*		backlink;
    };
    
    
    private:
        NodeType* head;
    
    
    
    void ClosedList::Delete(int item)
    {
    	NodeType* delPtr;
    	NodeType* currPtr;
    
    	if (item == head->component)
    	{
    		delPtr = head;
    		head = head->link;
    	}
    	else
    	{
    		currPtr = head;
    		while (currPtr->link->component != item)
    		{
    			currPtr = currPtr->link;
    		}	
    		delPtr = currPtr->link;
    		currPtr->link = currPtr->link->link;
    	}
    
    
    	delete delPtr;
    
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I'm not sure if this will solve your problem, but you need to make sure to set the backlink as well for the node AFTER the deleted link. So if its the head, you need:
    Code:
    if (item == head->component)
    {
    delPtr = head;
    head->link->backlink=NULL;
    head = head->link;
    }
    or if its not the head:
    Code:
    delPtr = currPtr->link;
    currPtr->link->link->backlink=currPtr
    currPtr->link = currPtr->link->link;

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Another potential problem is the fact that you never check your list for null pointers (IE: the end of the list).

    If you reach the end of the list, and you still haven't found the match, this will happen:

    currPtr == end of list
    currPtr->link == NULL
    currPtr->link->component == crashes your program

    See:
    Code:
     while (currPtr->link->component != item)
    Nuke!

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

  4. #4
    Anonymous Freak
    Guest

    A Comment

    Why do I need to check for a null pointer if it's a closed linked list? It doesn't make sense.

  5. #5
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    is it circular? you didnt mention such in any earlier posting.
    hello, internet!

  6. #6
    Anonymous Freak
    Guest

    It Works

    Thanks it works.

  7. #7
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: A Comment

    Originally posted by Anonymous Freak
    Why do I need to check for a null pointer if it's a closed linked list? It doesn't make sense.
    I prefer to be safe myself, but hey, knock yourself out. And as it was pointed out, nothing in your code or example stated this was a circular list.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 11:01 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  3. Problems with deleting a node in a singly linked list
    By omishompi in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2006, 06:27 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  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