I am making a hash table based on reg no. The insertion function is working fine but the search and deletion doesn't work in case of collision. It's not doing anything at all. There aren't any compilation errors either. Any help would be appreciated.
int size=4;
struct students
{
char name[50];
int regno;
int age;
char city[50];
}stud[4];
void insertion()
{
int reg,k,i;
printf("\nenter the student details you want to insert:\n");
printf("\nenter regno: ");
scanf("%d",®);
k=reg%size;
if (stud[k].regno==0)
{
stud[k].regno=reg;
printf("\nenter name: ");
scanf("%s",&stud[k].name);
printf("\nenter age: ");
scanf("%d",&stud[k].age);
printf("\nenter city: ");
scanf("%s",&stud[k].city);
}
else
{
for(i=k+1;i<size;i++)
{
if (stud[i].regno==0)
{
stud[i].regno=reg;
printf("\nenter name: ");
scanf("%s",&stud[i].name);
printf("\nenter age: ");
scanf("%d",&stud[i].age);
printf("\nenter city: ");
scanf("%s",&stud[i].city);
break;
}
else
for(i=0;i<k;i++)
{
if (stud[i].regno==0)
{
stud[i].regno=reg;
printf("\nenter name: ");
scanf("%s",&stud[i].name);
printf("\nenter age: ");
scanf("%d",&stud[i].age);
printf("\nenter city: ");
scanf("%s",&stud[i].city);
break;
}
}
}
}
}
void deletion()
{
int reg, k;
int f=0;
printf("\nenter the student reg no you want to delete");
scanf("%d",®);
k=reg%size;
if(stud[k].regno==0)
{
printf("\nIt is empty");
}
else if(stud[k].regno==reg)
{
printf("The removed student is %d",stud[k].regno);
stud[k].regno=0;
stud[k].name[0]='\0';
stud[k].age=0;
stud[k].city[0]='\0';
printf("\n\n");
}
else
{
int i;
for (i=k+1;i<size;i++)
{
if(stud[k].regno==reg)
{
printf("The removed student is %d",stud[k].regno);
stud[k].regno=0;
stud[k].name[0]='\0';
stud[k].age=0;
stud[k].city[0]='\0';
printf("\n\n");
f=1;
break;
}
}
for(i=0;i<k;i++)
{
if (stud[i].regno==reg)
{
printf("The removed student is %d",stud[k].regno);
stud[k].regno=0;
stud[k].name[0]='\0';
stud[k].age=0;
stud[k].city[0]='\0';
printf("\n\n");
f=1;
break;
}
}
}
if (f==1)
{
printf("\nIt is not present");
}
}
void search()
{
int reg, k;
int f=0;
printf("\nenter the student reg no you want to search");
scanf("%d",®);
k=reg%size;
if(stud[k].regno==0)
{
printf("\nIt is empty");
}
else if(stud[k].regno==reg)
{
printf("The student found is: \n");
printf("\nreg no: %d",stud[k].regno);
printf("\nname: %s",stud[k].name);
printf("\nage: %d",stud[k].age);
printf("\ncity: %s",stud[k].city);
printf("\n\n");
}
else
{
int i;
for (i=k+1;i<size;i++)
{
if(stud[k].regno==reg)
{
printf("The student found is: \n");
printf("\nreg no: %d",stud[k].regno);
printf("\nname: %s",stud[k].name);
printf("\nage: %d",stud[k].age);
printf("\ncity: %s",stud[k].city);
printf("\n\n");
f=1;
break;
}
}
for(i=0;i<k;i++)
{
if (stud[i].regno==reg)
{
printf("The student found is: \n");
printf("\nreg no: %d",stud[k].regno);
printf("\nname: %s",stud[k].name);
printf("\nage: %d",stud[k].age);
printf("\ncity: %s",stud[k].city);
printf("\n\n");
f=1;
break;
}
}
if (f==1)
{
printf("\nIt is not present");
}
}
}
void display()
{
int i;
for(i=0;i<size;i++)
{
printf("\nreg no: %d",stud[i].regno);
printf("\nname: %s",stud[i].name);
printf("\nage: %d",stud[i].age);
printf("\ncity: %s",stud[i].city);
printf("\n\n");
}
}
void main()
{
int i,c;
for(i=0;i<size;i++)
{
stud[i].regno=0;
stud[i].name[0]='\0';
stud[i].age=0;
stud[i].city[0]='\0';
}
do {
printf (" Enter 1 for insert \n");
printf (" Enter 2 for deletion \n");
printf (" Enter 3 for search \n");
printf (" Enter 4 for display \n");
scanf ("%d", &c);
switch(c)
{
case 1: insertion();
break;
case 2: deletion();
break;
case 3: search();
break;
case 4: display();
}
} while(c>0 && c<=4);
}
The problem is in your inner loops.
else
for(i=0;i<k;i++)
{
i
is already in use, so you must use a new variable:
else
for(int j=i+1;j<k;j++)
{
This error is present in many places.
I believe your code can be "compressed". I believey our if..else { for..if..else { for..if
can be made into a single loop that, starting from reg%size
searches for the first empty entry. That would make an elegant solution.