Search Perticular Element : Singly Linked List



Searching and Traversing for Data in Singly Linked List
Way 1 : This Function only tells that whether data is present or not

  • This function return 1 if data found successfully.
  • Returns 0 if data is not present
int search(int num)
{
int flag = 0;
struct node *temp;
temp = start;
  while(temp!=NULL)
  {
    if(temp->data == num)
       return(1); //Found
    temp = temp->next;
  }
if(flag == 0)
    return(0); // Not found
}

Way 2 : This Function Returns the node.

  • If data or element is not present then return starting node
  • Otherwise return the exact node.
struct node * search(int num)
{
int flag = 0;
struct node *temp;
temp = start;
  while(temp!=NULL)
  {
    if(temp->data == num)
       return(temp); //Found
    temp = temp->next;
  }
  if(flag == 0)
     return(start); // Not found
}