cant get this delete node working

This is a discussion on cant get this delete node working within the C Programming forums, part of the General Programming Boards category; I am having trouble with this, it works... kind of except for deleting the head and the node it deletes ...

  1. #1
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85

    cant get this delete node working

    I am having trouble with this, it works... kind of except for deleting the head and the node it deletes prints as 0 !!
    Code:
    list_t
    *delete_value (list_t *list, int value) {
    	node_t *ptr, *prev, *tmp;
    	
    	ptr = list->head;
    	
    		while (ptr!=NULL)
    	{
    	
    	if (list->head->data==value)
    		{
    			tmp=list->head;
    			
    			if (list->head->next!=NULL)
    			{
    				list->head = list->head->next;
    				free(tmp);
    				ptr = list->head;
    				prev = ptr;
    			}
    			else 
    			{
    				list->head = list->foot = NULL;
    				free(tmp);
    			}
    		}
    
    		prev = ptr;	
    	
    		if (ptr->data==value)
    		{
    			tmp = prev;
    			prev = ptr->next;
    			free(tmp);
    			ptr = prev;
    		}
    		if (ptr->data==value && ptr->next==NULL)
    		{
    			tmp = prev;
    			prev = NULL;
    			free(tmp);
    			ptr = prev;
    		}
    		ptr = ptr->next;
    	}
    	return list;
    }
    can anyone tell me what im doing wrong please!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    node *delnodebyvalue( node *n, data_t v )
    {
        node *d = n;
        if( d )
        {
            node *p = d;
            for( ; d && p; p = d, d = d->next )
            {
                if( d->value == v )
                    break;
            }
            
            if( d == p ) /* head */
            {
                d = d->next;
                free( p );
                p = NULL;
                n = d;
            }
            else
            if( d )
            {
                p->next = d->next;
                free( d );
                d = p;
            }
        }
        return n /* d */;
    }
    I haven't tested this, but that looks about right. That looks better.

    I find your added layer of 'list' as a wrapper too hard to read this late in the evening for me. It's easier for me to just write a new one than it is to try and figure out what you've done wrong. At a quick glance, I'd say it's probably somewhere here:
    Code:
    if (ptr->data==value)
    		{
    			tmp = prev;
    			prev = ptr->next;
    			free(tmp);
    			ptr = prev;
    		}
    		if (ptr->data==value && ptr->next==NULL)
    		{
    			tmp = prev;
    			prev = NULL;
    			free(tmp);
    			ptr = prev;
    		}
    Either you're being clever here, or you've missed something here, since what your code here might do is free ptr twice, since you don't have an else there. Like I said though, you may just be doing something clever that I'm too tired to catch.


    Quzah.
    Last edited by quzah; 04-25-2007 at 02:57 AM. Reason: Better return value.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Can u show your struct definition please.

    I can see some wried stuff there.

    ssharish2005

  4. #4
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    I think quzah may be right, I will impliment that solution. The compiler showed a segmentation fault from the same area of code quzah referenced.

    Here are the struct definitions:
    Code:
    typedef int data_t;
    
    typedef struct node node_t;
    
    struct node {
    	data_t data;
    	node_t *next;
    };
    
    typedef struct {
    	node_t *head;
    	node_t *foot;
    } list_t;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. 8 Puzzle game solver with the Best-First algoritm
    By LordMX in forum C Programming
    Replies: 17
    Last Post: 08-11-2008, 11:00 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM
  5. why is the input not working
    By mackol in forum C Programming
    Replies: 7
    Last Post: 11-07-2002, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21