Experiment No 02 DSA
Experiment No 02 DSA
⮚ Problem Statement : Implement all function of dictionary (ADT) using hashing and
handle collision using chaining with/without replacement. Data : Set of (key,value)
pairs, key are map to value, keys must be comparable, key must be unique. Standard
operation : Insert (key,value), Find (key), Delete (key).
⮚ Aim : Make use of hashing and handle collisions using chaining with/without
replacement.
⮚ Outcome : At the end of this experiment, student will able to illustrate chaining
with/without replacement.
⮚ Hardware Requirement :
PC/Laptop
⮚ Software Requirement :
G++/GCC
⮚ Theory :
1)Chaining without replacement :
0 -1 -1
1 131 2
2 21 5
3 3 -1
4 4 -1
5 61 7
6 6 -1
7 71 -1
8 8 -1
9 9 -1
131,3,4,21,61,6,71,8,9
From the example, you can see that the chain is maintained the number who
demands for location 1. First number 131 comes we will place at index 1. Next comes 21
but collision occurs so by linear probing we will place 21 at index 2, and chain is
maintained by writing 2 in chain at index 1 similarly next comes 61 by linear probing we
can place 61 at index 5 and chain will be maintained at index 2. Thus any element which
gives hash key as 1 will be stored by linear probing at empty location but a chain is
maintained so that traversing the hash table will be efficient.
The drawback of this method is in finding the next empty location. We are least
bothered about the fact when the element which actually belonging to that empty location
cannot obtain its location. This means logic of hash function gets disturbed.
As previous method has drawback of losing the meaning of the hash function, to
overcome this drawback the method known as changing with replacement is introduced.
Let us discuss the example to understand the method. Suppose we have to store following
elements :
131,21,31,4,5
0 -1 -1
1 131 2
2 21 3
3 31 -1
4 4 -1
5 5 -1
Now next element is 2. As hash function will indicate hash key as 2 but already at index 2.
We have stored element 21. But we also know that 21 is not of that position at which
currently it is placed. Hence we will replace 21 by 2 and accordingly chain table will be
updated. See the table.
0 -1 -1
1 131 6
2 2 -1
3 31 -1
4 4 -1
5 5 -1
6 21 3
7 -1 -1
8 -1 -1
9 -1 -1
Program code :
#include<iostream>
#include<cstring>
using namespace std;
class Hashfunction
{
typedef struct hash
{
long key;
char name[10];
}hash;
hash h[10];
public:
Hashfunction();
void insert();
void remove(long);
int find(long);
void display();
};
Hashfunction::Hashfunction()
{
int i;
for(i = 0; i < 10; i++)
{
h[i].key = -1;
strcpy(h[i].name, NULL);
}
}
int Hashfunction::find(long k)
{
int i;
for(i = 0; i < 10; i++)
{
if(h[i].key == k)
{
cout<<h[i].key<<" is found at location "<< i <<" with name
"<<h[i].name<<endl;
}
}
if(i == 10)
return -1;
}
void Hashfunction::display()
{
int i;
cout<<"\n\t\tKey\t\tName";
for(i = 0; i < 10; i++)
{
cout<<"\n\th["<<i<<"]:\t"<<h[i].key<<"\t\t"<<h[i].name<<endl;
}
}
void Hashfunction::insert()
{
char ans, n[10], ntmep[10];
long k, temp;
int v, hashindex, count = 0, flag = 0;
do
{
if(count >= 10)
{
cout<<"\n\tHash table is FULL";
break;
}
cout<<"\n\tEnter a Telephone Number: ";
cin>>k;
cout<<"\n\tEnter the Name: ";
cin>>n;
hashindex = k % 10;
if(h[hashindex].key == -1)
{
h[hashindex].key = k;
strcpy(h[hashindex].name, n);
}
else
{
if(h[hashindex].key % 10 != hashindex)
{
temp = h[hashindex].key;
strcpy(ntemp, h[hashindex].name);
for(i = hashindex + 1; i < 10; i++)
{
if(h[i].key == -1)
{
h[i].key == temp;
strcpy(h[i].name, temp);
flag = 1;
break;
}
}
for(i = 0; i < hashindex && flag == 0; i++)
{
if(h[i].key == -1)
{
h[i].key == temp;
strcpy(h[i].name, temp);
break;
}
}
}
}
}
}
int main()
{
return 0;
}
Assignment Questions :
1. what is the use of hash Table
2. Explain the characteristics of a good hash function.
3. what is collision? What are the different collision resolution Technique.