0% found this document useful (0 votes)
45 views15 pages

Hash Ex8

The document discusses different hashing techniques: 1. Linear probing is implemented with a hash table of size 10 using a key % table size to calculate the index and probing sequentially if the index is occupied. 2. Quadratic probing also uses a hash table size of 10 but calculates the index as (key % table size + i) % table size to probe quadratically if the index is occupied. 3. Double hashing calculates the index as (key % table size + i * hash2) % table size, where hash2 is a secondary hash function, to resolve collisions. 4. Separate chaining stores elements in linked lists at each index, with the index calculated as

Uploaded by

Prabavathi
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)
45 views15 pages

Hash Ex8

The document discusses different hashing techniques: 1. Linear probing is implemented with a hash table of size 10 using a key % table size to calculate the index and probing sequentially if the index is occupied. 2. Quadratic probing also uses a hash table size of 10 but calculates the index as (key % table size + i) % table size to probe quadratically if the index is occupied. 3. Double hashing calculates the index as (key % table size + i * hash2) % table size, where hash2 is a secondary hash function, to resolve collisions. 4. Separate chaining stores elements in linked lists at each index, with the index calculated as

Uploaded by

Prabavathi
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/ 15

EX.

NO:8 HASHING TECHNIQUE

10.02.21

AIM:

To understand the concept of hashing technique

1.To implement linear probing

PROGRAM:

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey;

printf("\nenter a value to insert into hash table :");

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;

T.PRABAVATHI(MTECH)
20MA32
break;

if(i == TABLE_SIZE)

printf("\nelement cannot be inserted\n");

void search()

int key,index,i,flag=0,hkey;

printf("\nenter search element\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

for(i=0;i<TABLE_SIZE; i++)

index=(hkey+i)%TABLE_SIZE;

if(h[index]==key)

printf("value is found at index %d",index);

break;

if(i == TABLE_SIZE)

printf("\n value is not found\n");

T.PRABAVATHI(MTECH)
20MA32
}

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. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;

case 2:

display();

T.PRABAVATHI(MTECH)
20MA32
break;

case 3:

search();

break;

case 4:exit(0);

OUTPUT:

2. To implement quadratic probing

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

T.PRABAVATHI(MTECH)
20MA32
int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey;

printf("\nenter a value to insert into hash table :");

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 search()

T.PRABAVATHI(MTECH)
20MA32
{

int key,index,i,flag=0,hkey;

printf("\nenter search element\n");

scanf("%d",&key);

hkey=key%TABLE_SIZE;

for(i=0;i<TABLE_SIZE; i++)

index=(hkey+i)%TABLE_SIZE;

if(h[index]==key)

printf("value is found at index %d",index);

break;

if(i == TABLE_SIZE)

printf("\n value is not found\n");

void display()

int i;

printf("\nelements in the hash table are \n");

for(i=0;i< TABLE_SIZE; i++)

T.PRABAVATHI(MTECH)
20MA32
printf("\nat index %d \t value = %d",i,h[i]);

main()

int opt,i;

while(1)

printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");

scanf("%d",&opt);

switch(opt)

case 1:

insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

T.PRABAVATHI(MTECH)
20MA32
OUTPUT:

3. To implement double Hashing

PROGRAM:

#include <stdio.h>

#include<stdlib.h>

#define TABLE_SIZE 10

int h[TABLE_SIZE]={NULL};

void insert()

int key,index,i,flag=0,hkey,hash2;

printf("\nenter a value to insert into hash table :");

T.PRABAVATHI(MTECH)
20MA32
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 :");

void search()

int key,index,i,flag=0,hash2,hkey;

printf("\nenter search element\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]==key)

T.PRABAVATHI(MTECH)
20MA32
{

printf("value is found at index %d",index);

break;

if(i == TABLE_SIZE)

printf("\n value is not found\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. Search \t4.Exit :");

scanf("%d",&opt);

switch(opt)

case 1:

T.PRABAVATHI(MTECH)
20MA32
insert();

break;

case 2:

display();

break;

case 3:

search();

break;

case 4:exit(0);

OUTPUT:

4. To implement the separate chaining

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

T.PRABAVATHI(MTECH)
20MA32
#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;

T.PRABAVATHI(MTECH)
20MA32
//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 *temp = chain[key];

while(temp->next)

temp = temp->next;

temp->next = newNode;

void print()

int i;

for(i = 0; i < size; i++)

struct node *temp = chain[i];

printf("chain[%d]-->",i);

while(temp)

T.PRABAVATHI(MTECH)
20MA32
printf("%d -->",temp->data);

temp = temp->next;

printf("NULL\n");

int main()

//init array of list to NULL

init();

insert(7);

insert(8);

insert(3);

insert(10);

insert(4);

insert(5);

print();

return 0;

T.PRABAVATHI(MTECH)
20MA32
OUTPUT:

T.PRABAVATHI(MTECH)
20MA32

You might also like