c code for double hashing

  • c
  • write a c code for double hashing

    WAP to double hashing

    Code

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> void printArray(int *A, int n) { for (int i = 0; i < n; i++) { printf("%d->%d \n", i, A[i]); } printf("\n"); } int isPrime(int n) { if (n <= 1) return 0; for (int i = 2; i <= sqrt(n); i++) if (n % i == 0) return 0; return 1; } int hash1(int key, int size) { return key % size; } int hash2(int key, int size) { int r = size; while (isPrime(r) != 1) { r = r - 1; } return r - (key % r); } int place(int H[], int key, int size) { int i = 0; while (H[(hash1(key, size) + (i * hash2(key, size)))] != 0) { i++; } return (hash1(key, size) + (i * hash2(key, size))); } void insert(int H[], int key, int size) { int index = hash1(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); i=i+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; }