Monday, March 30, 2015

Link list (Delete operation)

Delete operation
When we are deleting any node we have to follow a procedure

• Search for the location of the node you want to delete.
• Check the linked list if it is empty then display some message like”list is empty deletion not possible”.
• If we have to delete the first node then make the head pointer to point to the second node in the linked list.
• If you want to delete the last node then make the second last node point to NULL.
• If the deletion is for specific node the make previous node of that specific node point to the succeeding node.
1.      Delete first node
1.      Check the list, if list is empty then display an error message “deletion is not possible”.
2.      If the list has single node then delete that node and make linked list as empty list. 
3.      Otherwise 
     a.       Place the value of next address field of first node (which is the address of second node) in a variable temp.   
       b.      Delete the first node and release the space occupied by node. 
       c.       Make temp as the first node of the list.



Del first(void){
            Node *ptr;//check for empty list
            if(start = = Null)
               {
                  return;
             }
          //freeing the node
           else{
                 ptr = start;
                start = start -> next;
                free(ptr);
   
       }
}

2. Delete last node
1.      Check the list, if list is empty then display an error message “deletion is not possible”.
2.      If the list has single node then delete that node and make linked list as empty list.
3.      Otherwise
a.       Assign the address of second last node in a variable temp.
b.      Delete last node and release the memory space occupied by that node.
c.       Make temp as last node of the list and assign null at the next address field of last node.


No comments:

Post a Comment