Mar 12, 2011

Algorithm for the delete the element from the list


Function DEL (TEMPHEAD, KEY)
 [This Function Delete the Node whose information fields equals to the KEY. And TEMPHEAD is the pointer which points the first element of the list and function returns the address of the first node]          

1. [Check for the empty list]
          If TEMPHEAD = NULL
                   Then write (“Empty List”)
2. [Deletion of the first node]
          SAVE = TEMPHEAD
          TEMPHEAD = LINK (TEMPHEAD)
          Free (SAVE)       
          Return (TEMPHEAD)
3. [Save the address of the first node of the list]
          SAVE = TEMPHEAD
4. [Find the Node which to be deleted]
          Repeat while INFO (LINK (SAVE))! = KEY 
5. [Delete the node]
          TEMP = LINK (SAVE)
          LINK (SAVE) = LINK (LINK (SAVE))
           Free (TEMP)
6. [Finished]
          Return (TEMPHEAD)

No comments:

Post a Comment