Home » Searching in Doubly Linked List

Searching in Doubly Linked List

by Online Tutorials Library

Searching for a specific node in Doubly Linked List

We just need traverse the list in order to search for a specific element in the list. Perform following operations in order to search a specific operation.

  • Copy head pointer into a temporary pointer variable ptr.
  • declare a local variable I and assign it to 0.
  • Traverse the list until the pointer ptr becomes null. Keep shifting pointer to its next and increasing i by +1.
  • Compare each element of the list with the item which is to be searched.
  • If the item matched with any node value then the location of that value I will be returned from the function else NULL is returned.

Algorithm

  • Step 1: IF HEAD == NULL
  •  &nbspWRITE “UNDERFLOW”
    &nbspGOTO STEP 8
     [END OF IF]

  • Step 2: Set PTR = HEAD
  • Step 3: Set i = 0
  • Step 4: Repeat step 5 to 7 while PTR != NULL
  • Step 5: IF PTR → data = item
  • return i
    [END OF IF]

  • Step 6: i = i + 1
  • Step 7: PTR = PTR → next
  • 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  90    Node Inserted    1.Create  2.Search  3.Exit  4.Enter your choice?2    Enter item which you want to search?  90    item found at location 1  

Next TopicDoubly Linked List

You may also like