Hello friends!!!!!
Mid Square Hashing
#include<stdio.h>
#include<math.h>
// Code
#define SIZE 10
int hash(int key){
int address = pow(key , 2);
return (address / 10) % SIZE;
}
void insert(int a[],int value){
int index;
index = hash(value);
while(a[index] != -1){
index = (index + 1) % SIZE;
}
a[index] = value;
}
int deletion(int a[], int target){
int index = hash(target),j;
while(a[index] != -1){
if(a[index] == target){
a[index] = -1;
j = a[index];
}
else{
j = -1;
}
index = (index + 1) % SIZE;
}
return j;
}
int search(int a[], int target){
int val,index = hash(target);
while(a[index]!= -1){
if(a[index] == target){
val = index;
return val + 1;
}
else{
return -1;
}
index = (index + 1)%SIZE;
}
}
void display(int a[]){
int i;
for(i = 0 ; i < SIZE; i++){
if(a[i] != -1){
printf("%d ", a[i]);
}
}
}
int main(){
int Hash_table[100];
for(int i = 0 ; i < SIZE ; i++){
Hash_table[i] = -1;
}
insert(Hash_table,10);
insert(Hash_table,11);
insert(Hash_table,12);
insert(Hash_table,13);
insert(Hash_table,14);
insert(Hash_table,15);
insert(Hash_table,16);
display(Hash_table);
int res = search(Hash_table,12);
printf("\n%d is the position of 12\n", res);
printf("\n");
deletion(Hash_table,16);
printf("After deleting 16 : \n");
display(Hash_table);
}