All about programming in GNU/LINUX

General function for Linear search of Values of any Data TYpe……

#include<stdio.h>
#include<stdlib.h>
void *lsearch(void *key,void *base,int n,int elemsize);

int main()
{
int n, i,key;
int *k;
scanf(“%d”,&n);
int arr[n];

for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
scanf(“%d”,&key);
lsearch(&key,arr,n,sizeof(int));

return 0;
}
void *lsearch(void *key,void *base,int n,int elemsize)
{
int i;
for( i=0;i<n;i++)
{
void *elemntadd=(char *)base+(i*elemsize);
if(memcmp(key,elemntadd,elemsize)==0)
{
printf(“\nMatch FOUND “);
return elemntadd;
}
}
printf(“\nNo MAtch”);
return NULL;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s