w3resource

C Exercises: Search an element in a Singly Linked List


10. Search Element Variants

Write a program in C to search for an existing element in a singly linked list.

Visual Presentation:

C Exercises: Search an element in a  Singly Linked List

Sample Solution:

C Code:

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

// Define the structure for a node in a linked list
struct node
{
    int num;                // Data of the node
    struct node *nextptr;   // Pointer to the next node
};

// Declare pointers to the starting node and a temporary node
struct node stnode, *ennode;

// Function to find an element in the linked list and return its position
int FindElement(int);

int main()
{
    int n, i, FindElem, FindPlc;

    // Initialize the starting node's pointer to NULL
    stnode.nextptr = NULL;

    // Point ennode to the starting node
    ennode = &stnode;

    printf("\n\n Linked List : Search an element in a Singly Linked List :\n");
    printf("---------------------------------------------------------------\n");

    printf(" Input the number of nodes : ");
    scanf("%d", &n);

    printf("\n");
    // Loop to create the linked list with user input data
    for (i = 0; i < n; i++)
    {
        // Allocate memory for the next node
        ennode->nextptr = (struct node *)malloc(sizeof(struct node));

        printf(" Input data for node %d : ", i + 1);
        scanf("%d", &ennode->num);

        ennode = ennode->nextptr;
    }
    ennode->nextptr = NULL;

    printf("\n Data entered in the list are :\n");

    // Reset ennode to the starting node
    ennode = &stnode;

    // Loop to display the data entered in the linked list
    while (ennode->nextptr != NULL)
    {
        printf(" Data = %d\n", ennode->num);
        ennode = ennode->nextptr;
    }

    printf("\n");

    printf(" Input the element to be searched : ");
    scanf("%d", &FindElem);

    // Call FindElement function to search for the element and get its position
    FindPlc = FindElement(FindElem);

    // Display the position of the found element or a message if it doesn't exist
    if (FindPlc <= n)
        printf(" Element found at node %d \n\n", FindPlc);
    else
        printf(" This element does not exist in the linked list.\n\n");
}

// Function to find an element in the linked list and return its position
int FindElement(int FindElem)
{
    int ctr = 1;

    // Reset ennode to the starting node
    ennode = &stnode;

    // Loop to traverse the linked list and find the element
    while (ennode->nextptr != NULL)
    {
        // Check if the element is found at the current node
        if (ennode->num == FindElem)
            break;
        else
        {
            ctr++;
            ennode = ennode->nextptr;
        }
    }
    return ctr; // Return the position of the found element
}

Sample Output:

 Linked List : Search an element in a  Singly Linked List :                                                   
---------------------------------------------------------------                                               
 Input the number of nodes : 3                                                                                
                                                                                                              
 Input data for node 1 : 2                                                                                    
 Input data for node 2 : 5                                                                                    
 Input data for node 3 : 8                                                                                    
                                                                                                              
 Data entered in the list are :                                                                               
 Data = 2                                                                                                     
 Data = 5                                                                                                     
 Data = 8                                                                                                     
                                                                                                              
 Input the element to be searched : 5                                                                         
 Element found at node 2

Flowchart:

Flowchart: Search an element in a  Singly Linked List

FindElement() :

Flowchart: Search an element in a  Singly Linked List

For more Practice: Solve these Related Problems:

  • Write a C program to search for a given element in a singly linked list using recursion.
  • Write a C program to search for an element and count how many times it appears in the linked list.
  • Write a C program to search for an element that meets specific criteria (e.g., even and greater than 10) in a linked list.
  • Write a C program to return the position of a searched element in a singly linked list or indicate if it is not found.

Go to:


PREV : Delete Tail Variants.
NEXT : Linked List to String Challenges.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.