Home » Searching in Circular Singly Linked List

Searching in Circular Singly Linked List

by Online Tutorials Library

Searching in circular singly linked list

Searching in circular singly linked list needs traversing across the list. The item which is to be searched in the list is matched with each node data of the list once and if the match found then the location of that item is returned otherwise -1 is returned.

The algorithm and its implementation in C is given as follows.

Algorithm

  • Step 1: SET PTR = HEAD
  • Step 2: Set I = 0
  • STEP 3: IF PTR = NULL
  • WRITE “EMPTY LIST”
    GOTO STEP 8
    END OF IF

  • STEP 4: IF HEAD → DATA = ITEM
  • WRITE i+1 RETURN [END OF IF]

  • STEP 5: REPEAT STEP 5 TO 7 UNTIL PTR->next != head
  • STEP 6: if ptr → data = item
  • write i+1
    RETURN
    End of IF

  • STEP 7: I = I + 1
  • STEP 8: PTR = PTR → NEXT
  • [END OF LOOP]

  • STEP 9: EXIT

C Function

Output

1.Create  2.Search  3.Exit  4.Enter your choice?1    Enter the item  12    Node Inserted    1.Create  2.Search  3.Exit  4.Enter your choice?1    Enter the item  23    Node Inserted    1.Create  2.Search  3.Exit  4.Enter your choice?2    Enter item which you want to search?  12  item found at location 1  

Next TopicDoubly Linked List

You may also like