Search is one of the most common operation on performed any data structure. In this post I will explain how to search an element in linked list (iterative and recursive) using C program. I will explain both ways to search, how to search an element in linked list using loop and recursion.
Write a C program to create a function to search an element in linked list. If element exists in the linked list then, it should return its index otherwise -1.
Required knowledge
Singly linked list, Pointers, Structures, Dynamic memory allocation
Let us suppose the node structure as
Where head
is a pointer to first node.
Note: To keep this post short, readable and quick to consume. I am not going to explain logic to create and traverse list. You can read a detailed post on how to create and traverse a linked list.
How to search an element in linked list?
Search an element in linked list is fairly similar to how you search an element in arrays. Here we will learn to perform linear search on singly linked list.
Step by step descriptive logic to search an element in linked list.
- Input element to search from user. Store it in some variable say keyToSearch.
- Declare two variable one to store index of found element and other to iterate through list. Say index = 0; and
struct node *curNode = head;
- If curNode is not
NULL
and its data is not equal to keyToSearch. Then, increment index and move curNode to its next node. - Repeat step 3 till
curNode != NULL
and element is not found, otherwise move to 5th step. - If curNode is not
NULL
, then element is found hence return index otherwise -1.
Program to search an element in linked list
You can also write the above search logic using recursive function. But, before you must have at-least basic knowledge of recursion and how to solve problems using recursive function.
Read more: Recursive function exercises and solutions in C.
How to search an element in linked list using recursion?
Before writing recursive search function. Let us first define our recursive function.
int searchRecursive(int key, struct node *curNode, int index);
Our recursive function return index of element if found in linked list otherwise returns -1. It accepts three parameters
key element to search in the linked list.
curNode pointer to current node to check for element.
index current element index.
Let us define our recursive search base conditions.
- If
curNode == NULL
, means no more element to search further and should return -1. - If
curNode->data == key
, element found hence return current index. - None of the above two conditions satisfies, then move curNode to next node, increment index and make recursive call to search further. Say
searchRecursive(key, curNode->next, index + 1);
.
Program to search an element in linked list using recursion
Enter number of node to create: 5 Enter data of node 1: 10 Enter data of node 2: 20 Enter data of node 3: 25 Enter data of node 4: 30 Enter data of node 5: 35 Data in list: 10, 20, 25, 30, 35, Enter element to search: 20 20 found in the list at position 2