In this program, we are going to share a c program to implement hashing algorithm.
A hashing function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes.
Hashing algorithms can be pretty useful. However, IT is a really fast industry and this also extends to the hashing algorithms. The values are used to index a fixed-size table called a hash table.
Copy below C program and execute it with c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include<stdio.h> #include<conio.h> void main() { int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int n, value; int temp, hash; clrscr(); printf("nEnter the value of n(table size):"); scanf("%d", &n); do { printf("nEnter the hash value"); scanf("%d", &value); hash = value % n; if (a[hash] == 0) { a[hash] = value; printf("na[%d]the value %d is stored", hash, value); } else { for (hash++; hash < n; hash++) { if (a[hash] == 0) { printf("Space is allocated give other value"); a[hash] = value; printf("n a[%d]the value %d is stored", hash, value); goto menu; } } for (hash = 0; hash < n; hash++) { if (a[hash] == 0) { printf("Space is allocated give other value"); a[hash] = value; printf("n a[%d]the value %d is stored", hash, value); goto menu; } } printf("nnERRORn"); printf("nEnter '0' and press 'Enter key' twice to exit"); } menu: printf("n Do u want enter more"); scanf("%d", &temp); } while (temp == 1); getch(); } |
Learn more about hashes – Wikipedia.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.