-->

Thursday, February 20, 2014

Traversing in Circular Linked List for Data Structure in'C'

Traversing in Circular Linked List for Data Structure in'C'

Traversing in Circular Linked List:

     To visit all the nodes of a circular linked list, start from the ROOT and visit first element. Then with the help  of link field visit the second element, similarly visit all the elements. The last node is recognized by the
ROOT address in its link field. So, set a pointer variable PTR with ROOT. Process PTR-->INFO and update PTR by LINK of PTR i.e. PTR<--PTR-->LINK. Repeat till the PTR-->LINK is equal to ROOT.

The algorithm is as follows:

       TRAVERSCLL (ROOT)
       PTR<--ROOT
       If PTR = NULL Then:
          Write: ‘Empty Circular linked list’; Exit.
       [End of If]
       Repeat While TRUE
         Apply process to PTR-->INFO
         If PTR-->LINK = ROOT Then:
           Break.      [To break the infinite loop]
         [End of If]
         PTR<--PTR-->LINK
       [End of while]
       Exit.

Algorithm to print the contents of a linked list:

       TRAVERELL (ROOT)
       PTR<--ROOT
       If PTR = NULL Then:
         Write: ‘Empty Circular linked list’;  Exit.
      [End of If]
      Repeat While TRUE                     [infinite loop]
        Write: PTR-->INFO
        If PTR-->LINK = ROOT Then:
          Break.                       [To break the infinite loop]
        [End of If]
        PTR<--PTR-->LINK
      [End of while]
      Exit.

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved