Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

Mar 12, 2011

Algorithm for the print the doubly list


Procedure PRINT (TEMPHEAD)
[This procedure print the element of the node in the LIFO and FIFO format and TEMPHEAD points the first element of the list]

1. [Check for the empty list]
          If TEMPHEAD = NULL
                   Then write (“Empty list”)
                   Return
2. [First in first out]
          Repeat while RPTR (TEMPHEAD)! = NULL
                   Write (INOF (TEMPHEAD))
3. [Last in first out]
          Repeat while TEMPHEAD! = NULL
                   Write (INFO (TEMPHEAD))
4. [Finished]                  
            Return                        

Algorithm for the deleting an element from the doubly linked list


Function DELETE (TEMPHEAD, KEY)
[This Function delete an element from the doubly list and returns the address of the first element TEMPHEAD is pointer which points the first element of the list and KEY specify info of the node which is to be deleted]

1. [Check for the empty list]
          If TEMPHEAD = NULL
                   Then write (“Empty list”)
                   Return
2. [Deletion of the first node]
          TEMP = TEMPHEAD
          RPTR (TEMPHEAD) = TEMPHEAD
          PRV (TEMPHEAD) = NULL
          Free (TEMP)
          Return (TEMPHEAD)
3. [Save the address of the first node]
          SAVE = TEMPHEAD
4. [Search for the desire node]
          Repeat while thru step 5 RPTR (SAVE)! = NULL
5. [Check for the information field]
          If INFO (RPTR (SAVE)) = KEY
                   Then   TEMP = RPTR (SAVE)
                               RPTR (SAVE) = RPTR (RPTR (SAVE))
                               LPTR (RPTR (SAVE)) = SAVE
                               Free (TEMP)
6. [Finished]
          Return (TEMPHEAD)

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)

Algorithm for the Creation of the Doubly linked list


Procedure CRETE(TEMPHEAD)
[This Procedure Create the Doubly linked list TEMPHEAD is the pointer variable which point to the first element of the list and LPTR and RPTR is the pointer field of the NODE which points the Previous and new Node of the list respectively.]

1. [Repeat thru step]
          Repeat while choice! = ‘n’
2. [Allocate the new Node]
            NEW          NODE
3. [Set field of new Node]
          INFO (NEW) = X
          LPTR (NEW) = RPTR (RPTR) = NULL
4. [Insert the element]
          RPTR (TEMPHEAD) = NEW
          LPTR (RPTR (TEMPHEAD)) = TEMPHEAD
          TEMPHEAD = RPTR (TEMPHEAD)      
5. [Read the Choice]
          Read (choice)
6. [Finished]

Algorithm for the print the list


Procedure PRINT (HEAD)
[This Procedure print the information field of the list and HEAD is the first element of the list]
1. [Repeat step thru]
          Repeat while LINK (HEAD) != NULL
2. [Print the Information]
          Write (INFO (HEAD))
3. [Finished]
          Return

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)

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)

Algorithm for the Creation of the Simple Linked List


Function CREATE(X, FIRST)
[Given X, a new element, and FISRT, a pointer to the first element of a Linked linear list whose typical node contains INFO and LINK fields as in above fig, this function inserts X.]

1. [Repeat thru step 5]
          Repeat while Choice! = ‘n’
         
2. [Allocate the New node]
          NEW          NODE
3. [Initialize the fields of new node]
          INFO (NEW) = X
          LINK (FIRST) = NEW
4. [Want to insert another node]
          Read (Choice)
5. [Set the LINK field of Last inserted element]
          LINK (FIRST) = NULL
6. [Finished]
          Return