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

Experiment No 02 DSA

The document outlines an experiment to implement a hash table using chaining for collision resolution, detailing both methods: chaining with and without replacement. It includes a problem statement, aims, outcomes, hardware and software requirements, theoretical explanations, and a sample program code. Additionally, it poses assignment questions related to hash tables and collision resolution techniques.

Uploaded by

eshwaribhandkar
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 views8 pages

Experiment No 02 DSA

The document outlines an experiment to implement a hash table using chaining for collision resolution, detailing both methods: chaining with and without replacement. It includes a problem statement, aims, outcomes, hardware and software requirements, theoretical explanations, and a sample program code. Additionally, it poses assignment questions related to hash tables and collision resolution techniques.

Uploaded by

eshwaribhandkar
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/ 8

Experiment No 02

⮚ Title : Implement Hash Table using Chaining with/without replacement.

⮚ 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 :

In collision handling method chaining is a concept which introduces an additional field


with data i.e. chain. A separe chain table is maintained for collising data. When collision
occurs we store the second collising data by Linear Probing method. The address of this
colliding data can be stored with the first colliding element in the chain table, without
replacement.

Index Data Chain

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

For example consider elements,

131,3,4,21,61,6,71,8,9

1) Chaining without replacement

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.

2)Chaining with replacement :

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

Index Data Chain

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.

Index Data Chain

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

void Hashfunction::remove(long key)


{
int index = find(key);
if(index == -1)
{
cout<<"Key not found!!"<<endl;
}
else
{
h[index].key = -1;
strcpy(h[index].name, NULL);
cout<<"Key Removed!!"<<endl;
}
}

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.

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