Thread: please help me..How can I delete node at location 1 from a single link list ?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Post please help me..How can I delete node at location 1 from a single link list ?

    Hello all,
    Please help me.
    I have write a serial link list program.Here i can delete node at any location except at location 1(head).


    head of link list is at location 1.
    Please tell me where i am going wrong ?
    please give me a hint.
    Code:
    void deletenode(node**ptr,int loc)
    {
         node *temp,*s,*r;
         s = *ptr;
         if(count(*ptr)<loc)
         {
            printf("\n out of list");                   
         }
         else
         {
             while(loc>1)
             {
              temp = s;
              s= s->next;         
              loc--;  
             }
             temp->next = s->next;
                
         }
             
    }
    Thanks,
    Gunjan

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Deleting the first node is a special case, because there is no node before it for which you have to change the ->next member. Just add a special case in there for if count == 1:
    Code:
    void deletenode(node**ptr,int loc)
    {
         int n_nodes;
         node *temp,*s,*r;
         s = *ptr;
         n_nodes = count(*ptr);  // use a temp variable so you don't have to call count() twice
         if (n_nodes < loc)
              // error
         else if (n_nodes == 1)
              // delete first node
         else
              // normal node delete

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you need to do
    *ptr = the second node in the list

    In the case of deleting the first node of the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Woah, major mistake in the code I posted. It should be:
    Code:
         if (n_nodes < loc)
              // error
         if (loc == 1)
              // delete first node
         else
              // normal node delete

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can also check to see if loc == 1 before you even count them, because there's no reason to count them if you are just deleting the first one.


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

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Hi all,
    Thanks all for ur quick reply
    i check condition in my code. and afte that i call the function del to delete the first node. But on display the elements of list it give garbage. Please suggest me where m going wrong.

    Quote Originally Posted by quzah View Post
    You can also check to see if loc == 1 before you even count them, because there's no reason to count them if you are just deleting the first one.

    /*Delete the first node*/
    Code:
    void del(node**ptr)
    {
         
         node *temp,*s;
         s = *ptr;
         temp = s;
         s=s->next;
         free(temp);     
    }
    Quzah.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think he did, in some weird quote he attributed to me.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete node in linked list..
    By aren34 in forum C Programming
    Replies: 3
    Last Post: 03-19-2011, 11:54 PM
  2. Delete a node from a list by a given criteria
    By budala in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 05:51 PM
  3. Linked list - Delete first node
    By budala in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 01:32 PM
  4. delete node in Linked List
    By ronenk in forum C Programming
    Replies: 8
    Last Post: 01-05-2005, 01:28 AM
  5. Link list and node problem
    By tom_mk in forum C++ Programming
    Replies: 0
    Last Post: 02-21-2002, 05:18 PM