//linear probing
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printArray(int *A, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d->%d \n", i, A[i]);
}
printf("\n");
}
int hash(int key, int size)
{
return key % size;
}
int place(int H[], int key, int size)
{
int index = hash(key, size);
int i = 0;
while (H[(index + i) % size] != 0)
{
i++;
}
return (index + i) % size;
}
void insert(int H[], int key, int size)
{
int index = hash(key, size);
if (index != 0)
{
index = place(H, key, size);
}
H[index] = key;
}
int main()
{
int n = 1, e, c = 0, i;
printf("enter the no of element to hash : ");
scanf("%d", &i);
int HT[i];
for (int l = 0; l < i; l++)
{
HT[l] = 0;
}
while (n != 0)
{
printf("enter the element to hash : ");
scanf("%d", &e);
insert(HT, e, i);
c++;
printf("want to enter more[1/0]->[yes/no] : ");
scanf("%d", &n);
if (c == i)
{
printf("cant hash exceded the limt\n");
n = 0;
}
}
printArray(HT, i);
return 0;
}