76
Searching in singly linked list
Searching is performed in order to find the location of a particular element in the list. Searching any element in the list needs traversing through the list and make the comparison of every element of the list with the specified element. If the element is matched with any of the list element then the location of the element is returned from the function.
Algorithm
- Step 1: SET PTR = HEAD
- Step 2: Set I = 0
- STEP 3: IF PTR = NULL
- STEP 4: REPEAT STEP 5 TO 7 UNTIL PTR != NULL
- STEP 5: if ptr → data = item
- STEP 6: I = I + 1
- STEP 7: PTR = PTR → NEXT
- STEP 8: EXIT
WRITE “EMPTY LIST”
GOTO STEP 8
END OF IF
write i+1
End of IF
[END OF LOOP]
C function
Output
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?1 Enter the item 34 Node inserted 1.Create 2.Search 3.Exit 4.Enter your choice?2 Enter item which you want to search? 34 item found at location 1
Next Topic#