0% found this document useful (0 votes)
4 views13 pages

Hashing

The document contains several C programming implementations of different hashing techniques including Division Method, Mid Square Method, Separate Chaining, Linear Probing, Quadratic Probing, and Double Hashing. Each method demonstrates how to insert and display elements in a hash table, handling collisions in various ways. The code snippets include necessary functions and structures to manage the hash table operations.

Uploaded by

Sailaja Sravani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Hashing

The document contains several C programming implementations of different hashing techniques including Division Method, Mid Square Method, Separate Chaining, Linear Probing, Quadratic Probing, and Double Hashing. Each method demonstrates how to insert and display elements in a hash table, handling collisions in various ways. The code snippets include necessary functions and structures to manage the hash table operations.

Uploaded by

Sailaja Sravani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Division Method

#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;

//check if chain[key] is empty


if(chain[key] == NULL)
chain[key] = newNode;
//collision
else
{
//add the node at the end of chain[key].
struct node *current = chain[key];
while(current->next!=NULL)
{
current = current->next;
}
current->next = newNode;
}
}
void print()
{
int i;
for(i = 0; i < size; i++)
{
struct node *current = chain[i];
printf("chain[%d]-->",i);
while(current!=NULL)
{
printf("%d -->",current->data);
current = current->next;
}
printf("NULL\n");
}
}

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)

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);
}
}
}

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;

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);
}
}
}

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);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy