Mar 12, 2011

Algorithm for the insert an element in the doubly list


Function INSERT (TEMPHEAD, KEY)
[This Function inserts an element after the node which the info filed equals to the KEY and the returns the address of the first node]

1. [Allocate the memory for the new node]
          NEW          NODE
          INFO (NEW) = X
2. [Insertion as the first node]
          RPTR (NEW) = TEMPHEAD
          LPTR (NEW) = NULL
          RPTR (TEMPHEAD) = NEW
          TEMPHEAD = NEW
          Return (TEMPHEAD)
3. [Save address of the first node]
          SAVE = TEMPHEAD
4. [Find the element after which we want to insert the element]
          Repeat thru step while RPTR (SAVE)! = NULL
5. [Check for the desire position]
          If INFO (RPTR (SAVE)) = KEY
                   Then
                             RPTR (NEW) = RPTR (SAVE)
                             LPTR (NEW) = SAVE
                             LPTR (RPTR (SAVE)) = NEW
                             RPTR (SAVE) = NEW
6. [Finished]
          Return (TEMPHEAD)

No comments:

Post a Comment