C Program For Searching An Element In A Linked List
Last Updated :
16 Feb, 2023
Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise.
bool search(Node *head, int x)
For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then function should return false. If key to be searched is 14, then the function should return true.
Iterative Solution:
1) Initialize a node pointer, current = head.
2) Do following while current is not NULL
a) current->key is equal to the key being searched return true.
b) current = current->next
3) Return false
Following is iterative implementation of above algorithm to search a given key.
C
// Iterative C program to search an
// element in linked list
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
// Link list node
struct Node
{
int key;
struct Node* next;
};
/* Given a reference (pointer to pointer) to
the head of a list and an int, push a new
node on the front of the list. */
void push(struct Node** head_ref,
int new_key)
{
// Allocate node
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
// Put in the key
new_node->key = new_key;
// Link the old list of the new node
new_node->next = (*head_ref);
// Move the head to point to the new node
(*head_ref) = new_node;
}
// Checks whether the value x is present
// in linked list
bool search(struct Node* head, int x)
{
// Initialize current
struct Node* current = head;
while (current != NULL)
{
if (current->key == x)
return true;
current = current->next;
}
return false;
}
// Driver code
int main()
{
// Start with the empty list
struct Node* head = NULL;
int x = 21;
// Use push() to construct list
// 14->21->11->30->10
push(&head, 10);
push(&head, 30);
push(&head, 11);
push(&head, 21);
push(&head, 14);
search(head, 21)? printf("Yes") : printf("No");
return 0;
}
Output:
Yes
Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Recursive Solution:
bool search(head, x)
1) If head is NULL, return false.
2) If head's key is same as x, return true;
3) Else return search(head->next, x)
Following is the recursive implementation of the above algorithm to search a given key.
C
// Recursive C program to search an
// element in linked list
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
// Link list node
struct Node
{
int key;
struct Node* next;
};
/* Given a reference (pointer to pointer) to
the head of a list and an int, push a new
node on the front of the list. */
void push(struct Node** head_ref,
int new_key)
{
// Allocate node
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
// Put in the key
new_node->key = new_key;
// Link the old list of the new node
new_node->next = (*head_ref);
// Move the head to point to the new node
(*head_ref) = new_node;
}
// Checks whether the value x is present
// in linked list
bool search(struct Node* head, int x)
{
// Base case
if (head == NULL)
return false;
// If key is present in current
// node, return true
if (head->key == x)
return true;
// Recur for remaining list
return search(head->next, x);
}
// Driver code
int main()
{
// Start with the empty list
struct Node* head = NULL;
int x = 21;
// Use push() to construct list
// 14->21->11->30->10
push(&head, 10);
push(&head, 30);
push(&head, 11);
push(&head, 21);
push(&head, 14);
search(head, 21)? printf("Yes") : printf("No");
return 0;
}
Output:
Yes
Time Complexity: O(n), where n represents the length of the given linked list.
Auxiliary Space: O(n), for recursive call stack where n represents the length of the given linked list.
Please refer complete article on Search an element in a Linked List (Iterative and Recursive) for more details!
Similar Reads
C Program to Implement Singly Linked List A linked list is a linear data structure used to store elements of the same data type but not in contiguous memory locations. It is a collection of nodes where each node contains a data field and a next pointer indicating the address of the next node. So only the current node in the list knows where
9 min read
C Program for Reverse a linked list Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input : Head of following linked list 1->2->3->4->NULL Output : Linked list should be changed to, 4->3->2->1->NULL I
4 min read
Search an Element in Doubly Circular Linked List Pre-requisite: Convert an Array to a Circular Doubly Linked List, Doubly Circular Linked ListGiven a doubly circular linked list. The task is to find the position of an element in the list. Image Representation: Algorithm: Declare a temp pointer, and initialize it to the head of the list.Iterate the
13 min read
Menu driven program for all operations on singly linked list in C A Linked List is a linear data structure that consists of two parts: one is the data part and the other is the address part. In this article, all the common operations of a singly linked list are discussed in one menu-driven program.Operations to be PerformedcreateList(): To create the list with the
8 min read
C Program to Implement Circular Linked List A linked list is a dynamic data structure where elements are not stored in contiguous memory locations but linked using pointers. In a circular linked list, the last node points back to the first node instead of pointing to NULL, forming a circle.In this article, we will learn how to implement the c
8 min read
C Program For Sentinel Linear Search Sentinel Linear Search is basically a variation of the traditional linear search algorithm. It is designed to reduce the number of comparisons required to find a specific target value within an array or list. In this article, we will learn how to implement Sentinal Linear Search in C, see how it wor
8 min read