Hashing: Using Open Addressing

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

HASHING

Task 1:
Using Open Addressing:

#include<iostream>

#include<conio.h>

using namespace std;

class HT

private:

int *arr;

int size;

void insertElement(int e, int f);

int deleteElement(int e, int f);

int searchElement(int e, int f);

public:

HT(int s);

void insertElement(int e);

int deleteElement(int e);

int searchElement(int e);

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

void HT::insertElement(int e,int f)

int key=(e+f)%size;

if(arr[key]==-1)

arr[key]=e;

else

f++;

if(f==size)

cout<<"Hash table is full!";

else

insertElement(e,f);

int HT::deleteElement(int e)

return deleteElement(e,0);

int HT::deleteElement(int e,int f)

{
int key=(e+f)%size;

if(arr[key]==e)

arr[key]=-1;

return key;

else

f++;

if(f==size)

cout<<"Number not found!";

else

return deleteElement(e,f);

int HT::searchElement(int e)

return searchElement(e,0);

int HT::searchElement(int e,int f)

int key=(e+f)%size;

if(arr[key]==e)

cout<<"Number found at "<<key<<" index"<<endl;

return key;

else

f++;
if(f==size)

cout<<"Number Not Found!";

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>

using namespace std;

class Node

public:

Node* next;

int data;

};

class List

public:

Node* head;
};

class chainHash

private:

List* arr;

int size;

public:

chainHash(int);

void insert(int e);

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

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