C Program to search an element in a linked list

To find an element in a linked list

C Program to search an element in a linked list. In this article, we will find an element just like we find an element in an array which is the most common operation performed on any data structure.

For Example:-

  • Input: 5 -> 6 -> 9 -> 3 -> 47
  • Search Item: 9
  • Output: Element found position 3
list

Methods Discussed

We will discuss two methods in this post –

  • Method 1: Iterative
  • Method 2: Recursive

Structure for creating node in the linked list as follows:-

struct node 
{ 
    int data; // Data 
    struct node *next; // For address of next node
};

Implementation for C Program to search an element in a linked list:-

  • Initialize head = Null
  • Add a few items to the Linked List
  • Take input from the user for the item he wants to search
  • Linearly traverse the Linked List from head to the end until you hit the null node
  • For each node check if its data value == item user wants to search
  • Return index of node where data was found else return -1
C Program to search an element in a linked list

Method 1 (Iterative)

Run
#include<stdio.h>
#include<stdlib.h>

struct Node
{
  int data;
  struct Node *next;
};

void display (struct Node *node)
{

  //as linked list will end when Node is Null
  while (node != NULL)
    {
      printf ("%d ", node->data);
      node = node->next;
    }
  printf ("\n");
}

int searchElement (struct Node *head, int item)
{
  struct Node *current = head;	// Initialize current
  int index = 0;
  // traverse till then end of the linked list
  while (current != NULL)
    {
      if (current->data == item)
	{
	  return index;
	}
      current = current->next;
      index++;
    }
  return -1;
}

int main ()
{
  int item;

  //creating 4 pointers of type struct Node
  //So these can point to address of struct type variable
  struct Node *head = NULL;
  struct Node *node2 = NULL;
  struct Node *node3 = NULL;
  struct Node *node4 = NULL;

  // allocate 3 nodes in the heap 
  head = (struct Node *) malloc (sizeof (struct Node));
  node2 = (struct Node *) malloc (sizeof (struct Node));
  node3 = (struct Node *) malloc (sizeof (struct Node));
  node4 = (struct Node *) malloc (sizeof (struct Node));


  head->data = 10;		// data set for head node 
  head->next = node2;		// next pointer assigned to address of node2 

  node2->data = 15;
  node2->next = node3;

  node3->data = 20;
  node3->next = node4;

  node4->data = 25;
  node4->next = NULL;

  printf ("Linked List: ");
  display (head);

  printf ("Enter element to search: ");
  scanf ("%d", &item);

  int index = searchElement (head, item);

  if (index == -1)
    printf ("Item not found");
  else
    printf ("Item found at position: %d", index + 1);

  return 0;
}

Output

Linked List: 10 15 20 25 
Enter element to search: 20
Item found at position: 3

Method 2 (Recursive)

Run
#include<stdio.h>
#include<stdlib.h>

struct Node
{
  int data;
  struct Node *next;
};

void display (struct Node *node)
{

  //as linked list will end when Node is Null
  while (node != NULL)
    {
      printf ("%d ", node->data);
      node = node->next;
    }
  printf ("\n");
}

int searchElement (struct Node *head, int item, int index)
{
  // Base case
  if (head == NULL)
    return -1;

  // If data is present in current node, return true
  if (head->data == item)
    return index;

  // not present here will check for next position
  // in next recursive iteration
  index++;

  // Recur for remaining list
  return searchElement (head->next, item, index);
}

int main ()
{
  int item;

  //creating 4 pointers of type struct Node
  //So these can point to address of struct type variable
  struct Node *head = NULL;
  struct Node *node2 = NULL;
  struct Node *node3 = NULL;
  struct Node *node4 = NULL;

  // allocate 3 nodes in the heap 
  head = (struct Node *) malloc (sizeof (struct Node));
  node2 = (struct Node *) malloc (sizeof (struct Node));
  node3 = (struct Node *) malloc (sizeof (struct Node));
  node4 = (struct Node *) malloc (sizeof (struct Node));


  head->data = 10;		// data set for head node 
  head->next = node2;		// next pointer assigned to address of node2 

  node2->data = 15;
  node2->next = node3;

  node3->data = 20;
  node3->next = node4;

  node4->data = 25;
  node4->next = NULL;

  printf ("Linked List: ");
  display (head);

  printf ("Enter element to search: ");
  scanf ("%d", &item);

  int index = searchElement (head, item, 0);

  if (index == -1)
    printf ("Item not found");
  else
    printf ("Item found at position: %d", index + 1);

  return 0;
}

Output

Linked List: 10 15 20 25 
Enter element to search: 10
Item found at position: 1

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java