Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Search an element in a Singly Linked List

C Linked List : Exercise-10 with Solution

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

Pictorial Presentation:

C Exercises: Search an element in a  Singly Linked List

Sample Solution:

C Code:

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

struct node
{
  int num;
  struct node *nextptr;
}
 
stnode, *ennode;

int FindElement(int);
void main()
{
	int n,i,FindElem,FindPlc;
	stnode.nextptr=NULL;
	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");
	for(i=0;i< n;i++)
	{
		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");

    ennode=&stnode;
	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);
	FindPlc=FindElement(FindElem);
	if(FindPlc<=n)
		printf(" Element found at node %d \n\n",FindPlc);
	else
		printf(" This element does not exists in linked list.\n\n");
}
int FindElement(int FindElem)
{
	int ctr=1;
	ennode=&stnode;
	while(ennode->nextptr!=NULL)
	{
		if(ennode->num==FindElem)
			break;
		else
			ctr++;
			ennode=ennode->nextptr;
	}
	return ctr;
}

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

C Programming Code Editor:

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

Previous: Write a program in C to delete the last node of Singly Linked List.
Next: Write a program in C to create and display a doubly linked list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP