Mar 12, 2011

Algorithm for the Inserting the element in the Simple Linked List


Function INSERT (TEMPHEAD,KEY)
[This Function Insert the element after the node, which have the information field equal to the X. And HEAD is the pointer variable, which points to the first element of the list]
1. [Allocate the Memory for the NEW node]
          NEW          NODE
2. [Set fields of the NEW node]
          INFO (NEW) = X
          LINK (NEW) = NULL
3. [Insertion as the first node]
          LINK (NEW) = TEMPHEAD
          TEMPHEAD = NEW
          Return (HEAD)
4. [Save the address of the first element of the list]
          SAVE = TEMPHEAD  
5. [Find the element after which we want to insert the element]
          Repeat while INFO (LINK (SAVE) ) != NULL
6. [Insert the element]
          LINK (NEW) = LINK (SAVE)
          LINK (SAVE) = NEW
7. [Return the address of the first element]
          Return (TEMPHEAD)

No comments:

Post a Comment