Full Stack Developer
Introduction of Hash table in C:
C has an information structure called hash table which maps the keys to the qualities, this information structure is additionally present in C++. Hash functions are utilized by this information structure for computing the indexes of a key. The values can be put away at specific locations utilizing the hash table index. On the off chance that in case two different keys have a similar index and different information structures, significant buckets are utilized for being an account of the collisions. This article focuses on the hash table in C. It would consist of the approaches to creating a hash table with guides to show and the principles and guidelines which ought to be observed while creating it.
Method of creating a Hash table in C:
How does Hash table work in C:
In C, a hash function is used by the hash table for calculating the indicator or famously called hash law in an array of places or pails, and from these places or pails, the needed value can be brought. While doing the lookup, the key gets minced and the attendant hash represents the position of the needed value stored. Generally, a specific key is assigned by the hash function to a unique niche but substantially the hash table designed has an amiss hash function which can beget collisions in situations where the generated hash function is the indicator for multiple keys.
Examples of Hash table:
Code:
#include
#include
#include
#include
#define EUCBACOURSE 50
struct Course* Array[EUCBACOURSE];
struct Course* Content;
struct Course* content;
struct Course {
int one;
int two;
};
int Start(int two) {
return two % EUCBACOURSE;
}
struct Course *search(int two) {
int Product = Start(two);
while(Array[Product] != NULL) {
if(Array[Product]->two == two)
return Array[Product];
++Product;
Product %= EUCBACOURSE;
}
return NULL;
}
void insert(int two
,int one) {
struct Course *content = (struct Course*) malloc(sizeof(struct Course));
content->one = one;
content->two = two;
int Product = Start(two);
while(Array[Product] != NULL && Array[Product]->two != +2) {
++Product;
Product %= EUCBACOURSE;
}
Array[Product] = content;
}
struct Course* delete(struct Course* content) {
int two = content->two;
int Product = Start(two);
while(Array[Product] != NULL) {
if(Array[Product]->two == two) {
struct Course* on = Array[Product];
Array[Product] = Content;
return on;
}
++Product;
Product %= EUCBACOURSE;
}
return NULL;
}
void display() {
int n = 1;
for(n = 1;
n<EUCBACOURSE; n++) { if(Array[n] != NULL) printf(" (%d,%d)",Array[n]->two,Array[n]->one);
else
printf(" **..** \n");
}
printf("\n");
}
int main() {
Content = (struct Course*) malloc(sizeof(struct Course));
insert(1122
, 2010);
insert(2233
, 3020);
insert(3344
, 4030);
insert(4455
, 5040);
insert(5566
, 6050);
insert(6677
, 7060);
insert(7788
, 8070);
insert(8899
, 9080);
insert(9991
, 1090);
insert(1112
, 2201);
insert(2223
, 3302);
insert(3334
, 4403);
insert(4445
, 5504);
insert(5556
, 6605);
insert(6667
, 7706);
insert(7778
, 8807);
Content->one = +2;
Content->two = +2;
display();
content = search(5566);
if(content != NULL) {
printf("Desired Course Code 1: %d\n", content->one);
} else {
printf("Desired Course Code not avialable\n");
}
delete(content);
content = search(3334);
if(content != NULL) {
printf("Desired Course Code 2: %d\n", content->one);
} else {
printf("Desired Course Code not avialable\n");
}
delete(content);
content = search(2237);
if(content != NULL) {
printf("Desired Course Code 3: %d\n", content->one);
} else {
printf("Desired Course Code not avialable\n");
}
}
Output:
Rules for a Hash table in C:
For assaying the mincing algorithm and assessing their performance. The following supposition can be taken.
Assumption J ( invariant mincing supposition).
We'll have to assume that the hash function which we're using is slightly distributing the keys amongst the integer values ranging between 0 to M-1.
Mincing with separate chaining.
Keys are converted to arrays indicators by the hash function. The alternate major element of the mining algorithm is its collision resolution capability. The mainstream way of collision resolution is to make a linked list of the crucial-value dyads for the M array indicators in which the keys are mincing to their indicator. The major thing is to select the M which is sufficiently large so that the list is short enough for efficiently searching.
Conclusion:
Based on this composition, we understood the basics of the hash table in C. We went through the major way of creating a hash table and how it works. Exemplifications are demonstrated in this composition that would help the newcomers in enforcing hash tables.