#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define t_size 5

int collision(int);
void constructor(void);
int hash_func(int);
int set_data(int);
void insert(int,int);
int search(int);

enum{failure=0,success=1};
int operation=failure;

enum{empty=0,fill=1};
struct hash_table
{
int data;
int data_status;
}
table[t_size];


void main(void)
{
int data,option,check;
constructor();
clrscr();
while(1)
{
printf("[1] Insert\n\n");
printf("[2] Search\n\n");
printf("[3] Quit\n\n");
printf("=====>>  ");
scanf("%d",&option);
printf("\nEnter Data :");
scanf("%d",&data);

switch(option)
{
case 1:
check=set_data(data);
if(check==failure)
{
printf("\n\n\"Data Buffer Full Can Not Enter Data\"\n\n");
}
break;

case 2:
check=search(data);
if(check==-1)
{
printf("\n\n\"No Match Found\"\n\n");
break;
}

case 3:
{
exit(0);
}

else
{
printf("\n\n\"Match Found at Location %d\"\n\n",check);}
break;
}
}


}
void constructor()
{
for(int x=0;x<t_size;x++)
{
table[x].data_status=empty;
}
}

int hash_func(int data)
{
int h_key;

h_key=data%t_size;
return h_key;
}

int set_data(int data)
{
operation=failure;
int h_key;

h_key=hash_func(data);

if( table[h_key].data_status==empty)
{
insert(data,h_key);
}

else if(  (h_key=collision(h_key))==-1 )
operation=failure;

else
insert(data,h_key);//success

return operation;
}

void insert(int data,int h_key)
{
table[h_key].data=data;
table[h_key].data_status=fill;
operation=success;
}

int collision(int h_key)
{
int new_key=-1;

for(int x=h_key+1;x!=h_key;x++)
{

if(x==t_size)
x=0;

if(table[x].data_status==empty)
{
new_key=x;
break;
}

if(x==h_key)
break;
}
return new_key;
}


int search(int data)
{
int h_key,index=-1;

h_key=hash_func(data);
if(table[h_key].data==data)
index=h_key;

else
for(int x=h_key+1;x!=h_key;x++)
{
if(x==t_size)
x=0;

if(table[x].data==data)
{
index=x;
break;
}
}
return index;
}
