65
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
- STEP 4: IF HEAD → DATA = ITEM
- STEP 5: REPEAT STEP 5 TO 7 UNTIL PTR->next != head
- STEP 6: if ptr → data = item
- STEP 7: I = I + 1
- STEP 8: PTR = PTR → NEXT
- STEP 9: EXIT
WRITE “EMPTY LIST”
GOTO STEP 8
END OF IF
WRITE i+1 RETURN [END OF IF]
write i+1
RETURN
End of IF
[END OF LOOP]
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