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
  •   WRITE "EMPTY LIST"
      GOTO STEP 8
      END OF IF

  • STEP 4: REPEAT STEP 5 TO 7 UNTIL PTR != NULL
  • STEP 5: if ptr → data = item
  •   write i+1
     End of IF

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

  • STEP 8: EXIT

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#




Latest Courses