program For EISCH //name - Jatin Chand Deopa //ID - 51944
program For EISCH //name - Jatin Chand Deopa //ID - 51944
program For EISCH //name - Jatin Chand Deopa //ID - 51944
int searching(int search, record *ptr, int n) //Function for searching an element in table
{
int pos=search%n;
record *temp2;
if(ptr[pos].data==search)
return pos+1;
else if(ptr[pos].data!=search&&ptr[pos].link==NULL)
return -1;
else if(ptr[pos].data!=search&&ptr[pos].link!=NULL)
{
temp2=ptr[pos].link;
while(temp2->link!=NULL)
{
if(temp2->data==search)
{
return (temp2-ptr)+1;
}
temp2=temp2->link;
}
return (temp2-ptr)+1;
}
}
int display(record *ptr, int n) //Function to display all the table elements
{
printf("Entered elements are:-\n");
for(int i=0; i<n; i++)
{
printf("%d ",ptr->data);
printf("%d\n", ptr->link);
ptr++;
}
}
int main ()
{ int n, num,key,search,i;
int val,pos;
printf("Please enter the value of n for hash function\n");
scanf("%d", &n);
record *ptr=table(n);
r=ptr+n;
printf("Please enter the number of elements to be inserted\n");
scanf("%d",&num);
printf("Now enter the table elements\n");
for(int i=0; i<num; i++)
{
scanf("%d",&key);
hashing(key, ptr, n);
}
printf("\nThe elements are: ");
display(ptr,n);
do
{
printf("\n1.Search the element");
printf("\n2.Exit");
printf("\nSelect the option\n");
scanf("%d",&val);
switch(val)
{
int hashing(int key, record *ptr, int n, int cellar) //Function to insert elements
{
int pos=key%(n-cellar);
record *temp1;
if(ptr[pos].data==-1)
ptr[pos].data=key;
else if(ptr[pos].data!=-1&&ptr[pos].link==NULL)
{
r--;
while(r->data!=-1)
r--;
r->data=key;
ptr[pos].link=r;
}
else if(ptr[pos].data!=-1&&ptr[pos].link!=NULL)
{
temp1=ptr[pos].link;
while(temp1->link!=NULL)
temp1=temp1->link;
r--;
while(r->data!=-1)
r--;
r->data=key;
temp1->link=r;
}
}
int searching(int search, record *ptr, int n, int cellar) //Function for searching an element in table
{
int pos=search%(n-cellar);
record *temp2;
if(ptr[pos].data==search)
return pos+1;
else if(ptr[pos].data!=search&&ptr[pos].link==NULL)
return -1;
else if(ptr[pos].data!=search&&ptr[pos].link!=NULL)
{
temp2=ptr[pos].link;
while(temp2->link!=NULL)
{
if(temp2->data==search)
{
return (temp2-ptr)+1;
}
temp2=temp2->link;
}
return (temp2-ptr)+1;
}
}
int display(record *ptr, int n) //Function to display all the table elements
{
printf("Entered elements are:-\n");
for(int i=0; i<n; i++)
{
printf("%d ",ptr->data);
ptr++;
}
}
int main ()
{ int n, num,key,search,i;
int val,pos;
printf("Please enter the value of n for hash function\n");
scanf("%d",&n);
int cellar;
scanf("%d",&cellar);
record *ptr=table(n);
r=ptr+n;
printf("Please enter the number of elements to be inserted\n");
scanf("%d",&num);
printf("Now enter the table elements\n");
for(int i=0; i<num; i++)
{
scanf("%d",&key);
hashing(key, ptr, n,cellar);
}
printf("\nThe elements are: ");
display(ptr,n);
do
{
printf("\n1.Search the element");
printf("\n2.Exit");
printf("\nSelect the option\n");
scanf("%d",&val);
switch(val)
{