Hashing
Hashing
#include<stdio.h>
int main()
{
int n, I;
printf(“Enter size of the hash table:”);
scanf(“%d”,&n);
int key[n], index[n];
printf("Enter keys: ");
for(i=0; i<n;i++)
scanf("%d", &key[i]);
for(i = 0; i < n; i ++)
{
indexes[i] = (keys[i] % n);
}
printf("\nThe indexes of the values in the Hash Table: ");
for(i = 0; i < n; i++)
{
printf("%d ", indexes[i]);
}
return 0;
}
Mid Square Method
Division Method
#include<stdio.h>
int main()
{
int n, I,h;
printf(“Enter size of the hash table:”);
scanf(“%d”,&n);
int key[n], index[n];
printf("Enter keys: ");
for(i=0; i<n;i++)
scanf("%d", &key[i]);
for(i = 0; i < n; i ++)
{
h= (keys[i]*key[i]/10) % 10;
index[h]=key[I];
}
printf("\nThe indexes of the values in the Hash Table: ");
for(i = 0; i < n; i++)
{
printf("hash[%d]=%d", h,index[i]);
}
return 0;
}
Separate chaining
#include<stdio.h>
#include<stdlib.h>
#define size 7
struct node
{
int data;
struct node *next;
};
struct node *chain[size];
void init()
{
int i;
for(i = 0; i < size; i++)
chain[i] = NULL;
}
void insert(int value)
{
//create a newnode with value
struct node *newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;
//calculate hash key
int key = value % size;
int main()
{
//init array of list to NULL
init();
insert(7);
insert(0);
insert(3);
insert(10);
insert(4);
insert(5);
print();
}
Linear Probing
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
{
int key,index,ihkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
{
h[index]=key;
break;
}
if(i == TABLE_SIZE)
Quadratic Probing
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
{
int key,index,i,hkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{
index=(hkey+i*i)%TABLE_SIZE;
if(h[index] == NULL)
{
h[index]=key;
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}
void display()
{
int i;
}
main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
exit(0);
}
}
}
Double Hashing
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
{
int key,index,i,hkey,hash2;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
hash2 = 7-(key %7);
for(i=0;i<TABLE_SIZE;i++)
{
index=(hkey+i*hash2)%TABLE_SIZE;
if(h[index] == NULL)
{
h[index]=key;
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}
void display()
{
int i;
printf("\nelements in the hash table are \n");
for(i=0;i< TABLE_SIZE; i++)
printf("\nat index %d \t value = %d",i,h[i]);
}
main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
exit(0);
}
}
}