Hashing: Using Open Addressing
Hashing: Using Open Addressing
Hashing: Using Open Addressing
Task 1:
Using Open Addressing:
#include<iostream>
#include<conio.h>
class HT
private:
int *arr;
int size;
public:
HT(int s);
void display();
};
HT::HT(int s)
size=s;
arr=new int[size];
for(int i=0;i<size;i++)
{
arr[i]=-1;
void HT::insertElement(int e)
insertElement(e,0);
int key=(e+f)%size;
if(arr[key]==-1)
arr[key]=e;
else
f++;
if(f==size)
else
insertElement(e,f);
int HT::deleteElement(int e)
return deleteElement(e,0);
{
int key=(e+f)%size;
if(arr[key]==e)
arr[key]=-1;
return key;
else
f++;
if(f==size)
else
return deleteElement(e,f);
int HT::searchElement(int e)
return searchElement(e,0);
int key=(e+f)%size;
if(arr[key]==e)
return key;
else
f++;
if(f==size)
else
return searchElement(e,f);
void HT::display()
for(int i=0;i<size;i++)
cout<<arr[i]<<endl;
Task 2:
Using Separate Channing:
#include<iostream>
#include<conio.h>
class Node
public:
Node* next;
int data;
};
class List
public:
Node* head;
};
class chainHash
private:
List* arr;
int size;
public:
chainHash(int);
void display();
};
chainHash::chainHash(int s)
size=s;
arr=new List;
for(int i=0;i<size;i++)
arr[i].head=NULL;
void chainHash::insert(int e)
int key=e%size;
Node*ptr=new Node;
ptr->data=e;
ptr->next=NULL;
Node* temp=arr[key].head;
if(temp==NULL)
arr[key].head=ptr;
}
else
while(temp->next!=NULL)
{temp=temp->next;}
temp->next=ptr;
void chainHash::display()
for(int i=0;i<size;i++)
Node* temp=arr[i].head;
cout<<i;
while(temp!=NULL)
cout<<"->"<<temp->data;
temp=temp->next;
cout<<endl;
void main()
chainHash t(5);
t.insert(3);
t.insert(1);
t.insert(13);
t.display();
_getch();