Thread: Help me finding the error in my delete node function

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42

    Post Help me finding the error in my delete node function

    I have written a delete node function to delete a node in a linked list by the index number. Here is the code:
    Code:
    void delete_node(struct node **start,int index_no)
    {    int counter=0;
          struct node *current=*start, *prev=NULL;//Current holds start and prev holds previous node of current
          
          while(current->next!=NULL)
           {
              current=current->next; //Traversing the list
    		prev->next=current;// updating the prev node accordingly
    	     counter++;//counter to keep track of the index
    		if(counter==index_no)//if user input and node no matches except the head node
    		{
                    printf("\n Hurrah! We caught the node!!");
                    
                    prev->next=current->next; //Making sure the prev node holds the address of the next to current ndoe
    			free(current); //deleting current
               }
    
    }
    }
    Please note that I know if the head node turns out to be the indexed position, there is no way to delete that node in the current coding. But please after correcting the present node add that part of the code separately.
    "1st allow yourself to make the basics clear and then step ahead to make a glorious victory over the subject and unleash your power to manipulate it !"
    Regards MISTU4U

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have set *prev to NULL and then you do prev->next = current.

    So, you request a member named "next" in NULL. You see the error here, don't you?

    It also seems that you did good in the procedure, before writing the code, which is taking a paper and a pen and actually draw your list and the actions your functions does.

    Do that again and try to delete the first node. You will find out what is happening.

    Repeat for last node. In general, when writing code for manipulating a list, check how they react in these three situations:
    • Manipulate the first node.
    • Manipulate a node in the middle.
    • Manipulate the last node.



    Also notice that when you do
    Code:
    prev->next=current;
    , assuming that prev does not point anywhere. It probably points to garbage. Only the "next" points to something.
    Last edited by std10093; 09-12-2013 at 08:09 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Did you have a specific question?

    Some things I noticed:

    - Why are you passing a pointer to pointer to start? If that's supposed to be the head node, you shouldn't have to modify it in this function (considering the code you provided isn't intended to be able to delete the head node)

    - "prev" isn't pointing to anything at line 8, so how can you assign a value to the member of a non-existent struct?

    - If you successfully free current, then you shouldn't be using that pointer anymore - but that is what your code does, when it returns to the top of the loop. Perhaps you intended to put a break or return in the body of that if statement?

    It would also be a good idea to:

    - add more error checking to the function (e.g. make sure the pointers passed aren't already NULL)

    - return a value, so you know if the delete was successful or not

  4. #4
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    your delete_node() has to process the following questions before actually deleting a node:

    Have to remove very first element ?
    It is also the last element (thus the only element) ?
    First element but not the only one ?
    Have to remove last element (but not the only element) ?
    Have to remove an element which is somewhere in the list (but not the first and/or last element) ?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    Quote Originally Posted by Matticus View Post
    Did you have a specific question?

    Some things I noticed:

    - Why are you passing a pointer to pointer to start? If that's supposed to be the head node, you shouldn't have to modify it in this function (considering the code you provided isn't intended to be able to delete the head node)

    - "prev" isn't pointing to anything at line 8, so how can you assign a value to the member of a non-existent struct?

    - If you successfully free current, then you shouldn't be using that pointer anymore - but that is what your code does, when it returns to the top of the loop. Perhaps you intended to put a break or return in the body of that if statement?

    It would also be a good idea to:

    - add more error checking to the function (e.g. make sure the pointers passed aren't already NULL)

    - return a value, so you know if the delete was successful or not
    everything you noticed and assumed are correct. Points to be noticed:

    1) I intend to delete the head node later,so I used pointer to pointer to start.

    2) As you suggested, I added a break in the if loop.

    Now please tell me,

    1)How to make "prev" node to point the just before node of "current". I intend to do that, but unable to code correctly.

    2) And how to return a value to check if the deletion was successful. Please provide a code snippet if possible.
    "1st allow yourself to make the basics clear and then step ahead to make a glorious victory over the subject and unleash your power to manipulate it !"
    Regards MISTU4U

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Here's something I slapped together. This is a simplification based on your initial requirements, and is lacking some important details. It is intended only to guide you to creating your own function that does what you need it to do. Note that it has not been tested, nor does it perform error checking.

    Code:
    void delete_node(struct node *start, int index_no)
    {
        int counter;
        struct node *current = start;
        struct node *prev = NULL;
    
        /* advance current to the desired index */
        /* prev will be pointing to the node before current */
        /* return zero if current index does not exist */
        for(counter = 0; counter < index_no; counter++)
        {
            prev = current;
            current = current->next;
            if(current == NULL)
                return 0;
        }
    
        /* store current "next" into previous "next" */
        /* free current node, and return one */
        prev->next = current->next;
        free(current);
        current = NULL;
    
        return 1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete olnly the last node
    By std10093 in forum C Programming
    Replies: 2
    Last Post: 01-13-2011, 12:21 PM
  2. delete last node
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 08:08 AM
  3. BST - delete node
    By marrk in forum C Programming
    Replies: 5
    Last Post: 12-20-2006, 10:46 AM
  4. Delete node
    By AmazingRando in forum C Programming
    Replies: 1
    Last Post: 09-23-2003, 04:44 PM
  5. Delete node!!!!!
    By khpuce in forum C Programming
    Replies: 3
    Last Post: 05-31-2003, 06:33 AM

Tags for this Thread