Indexed Sequential Searching Example: Create a telephone directory application and maintain the names and telephone numbers.

Leave a Comment

/*Create a telephone directory application and maintain the names and telephone numbers. Get the name of the person whose number to be searched and search through a portion of the array using indexed sequential search.*/

#include<stdio.h>                                  //for standard input/output functions
#include<stdlib.h>                               
//for standard library functions
#include<conio.h>                               
//for console input/output functions

struct tele{                                            
//Defining the the telephone directory structure                        char name[20];                                      //To store customer's Name                       
long tel;                                                  //to store the telephone number                  
}a[100];                                                  //Initializing the telephone directory structure with an array
 

void enter(struct tele a[], int n)          //Function to Enter the data in the telephone directory 
{
  int i;
  printf("\nNow enter the data\n");   
  for(i=0;i<n;i++)
   {
     printf("\nEnter the name:");
     fflush(stdin);                                 
//to clear the input stream of all pending input     
     gets(a[i].name);
     printf("\nEnter the telephone number:");
     scanf("%ld",&a[i].tel);
   }
}


void sort(struct tele a[],int n)         
//Function to sort the telephone directory as per the name{
  int i,j;
  struct tele t;
  for(i=0;i <n-1;i++)
 {
   for(j=i+1;j<n;j++) 
  {
    if(strcmpi(a[i].name,a[j].name)>0)
   {
     t=a[i];
     a[i]=a[j];
     a[j]=t;
   }
  }
  }
}

void c_index(struct tele index[], int n, struct tele a[],int asize)  
//For creating the index 
{
int i,j;
for(i=0,j=0;i<n;i++,j++)
{
index[j]=a[i];
}
index[j]=a[asize-1];
}

//For indexed search
 int i_search(char search[], struct tele index[], int isize, struct tele a[], int asize)
{                        
int i=0,j=0,pos=0;
int high=0,low=0;
if(strcmpi(search,index[isize-1].name)>0||strcmpi(search,index[0].name)<0)
 return -1;
while(i<asize)
{
if(strcmpi(search,index[i].name)==0)
{
pos=8*i;
return pos;
}
if(strcmpi(search,index[i].name)

{
low=8*(i-1);
high=8*i;
break;
}
else
{
low=8*i;
high=8*(i+1);
}
i++;
}
while(low<asize)

 {
if((strcmpi(search,a[low].name))==0)
return low+1;
else
low++;
}
return -1;
}


int main()

{
char search[20];
int pos,i;
int n;
printf("\nEnter the max limit:");
scanf("%d",&n);
enter(a,n);                                             
//Calling the "enter" function
struct tele index[(n/8)+1];
c_index(index,(n/8)+1,a,n);
printf("\nEnter the name u wanna search:");
fflush(stdin);
gets(search);
pos=i_search(search,index,(n/8)+1,a,n); 
//Calling the "i_search" function
if(pos==-1)
printf("\nElement not found!!");
else
printf("%s found at %d position",search,pos+1);
printf("Telephone number is %ld",a[pos].tel);
getche();
return 0;
}


0 comments: