0% found this document useful (0 votes)
26 views14 pages

DSA Practical 2 Sakshi New

The document contains a C++ program that implements a hash table with various functionalities including insertion, deletion, searching, and displaying key-value pairs. It defines a class 'hashfunction' which manages an array of hash structures and provides methods for handling collisions through linear probing. The main function allows users to interact with the hash table through a menu-driven interface.

Uploaded by

Sakshi Bhosale
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)
26 views14 pages

DSA Practical 2 Sakshi New

The document contains a C++ program that implements a hash table with various functionalities including insertion, deletion, searching, and displaying key-value pairs. It defines a class 'hashfunction' which manages an array of hash structures and provides methods for handling collisions through linear probing. The main function allows users to interact with the hash table through a menu-driven interface.

Uploaded by

Sakshi Bhosale
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/ 14

Assignment 2

Name: Sakshi Suraj Bhosale Roll no:309

INPUT-

#include<iostream>

#include<string.h>

using namespace std;

class hashfunction

typedef struct hash

int key;

int value;

}hash;

hash h[10];

public:

hashfunction();

void insert();

void display();

int find(int);

void delete1(int);

void insert1();

};

hashfunction::hashfunction()

int i;

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

h[i].key=-1;
h[i].value=-1;

void hashfunction::delete1(int k)

int index=find(k);

if(index==-1)

cout<<"key is empty";

}else

h[index].key=-1;

h[index].value=-1;

cout<<"key is deleted";

int hashfunction::find(int k)

int i;

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

if(h[i].key==k)

cout<<h[i].key<<"key is found at"<<i<<"location with value"<<h[i].value;

return i;

if(i==10)
{

return -1;

return 0;

void hashfunction::display()

int i;

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

cout<<"\n\th["<<i<<"]"<<h[i].key<<"\t\t"<<h[i].value;

void hashfunction::insert()

char ans;

int k,v,i,hi,cnt=0,flag=0;

do

if(cnt>=10)

cout<<"\ntable full";

cout<<"enter the key";

cin>>k;

cout<<"enter the value";

cin>>v;
hi=k%10;

if(h[hi].key==-1)

h[hi].key=k;

h[hi].value=v;

else

for(i=hi+1;i<10;i++)

if(h[i].key==-1)

h[i].key=k;

h[i].value=v;

flag=1;

break;

for(i=0;i<hi&& flag==0;i++)

if(h[i].key==-1)

h[i].key=k;

h[i].value=v;

break;

}
}

flag=0;

cnt++;

cout<<"do you want to insert more keys";

cin>>ans;

}while(ans=='y'||ans=='Y');

void hashfunction::insert1()

char ans;

int k,n,temp,ntemp,hi,cnt=0,flag=0,i;

do

if(cnt>=10)

cout<<"\n\tHash Table is FULL";

break;

cout<<"\n\tEnter a key: ";

cin>>k;

cout<<"\n\tEnter value: ";

cin>>n;

hi=k%10;// hash function

if(h[hi].key==-1) //key empty

{
h[hi].key=k;

h[hi].value=n;

else

if(h[hi].key%10!=hi) //key not empty,already filled,not own index then replace

temp=h[hi].key;

ntemp=h[hi].value;

h[hi].key=k; //assign users value to hashtable

h[hi].value=n;

for(i=hi+1;i<10;i++) //check for next index empty

if(h[i].key==-1) //next index empty

h[i].key=temp;

h[i].value=ntemp;

flag=1;

break;

for(i=0;i<hi && flag==0;i++) //cond true set flg 0

if(h[i].key==-1)

h[i].key=temp;

h[i].value=ntemp;

break;
}

else //own index then not replace

for(i=hi+1;i<10;i++)

if(h[i].key==-1)

h[i].key=k;

h[i].value=n;

flag=1;

break;

for(i=0;i<hi && flag==0;i++)

if(h[i].key==-1)

h[i].key=k;

h[i].value=n;

break;

flag=0;
cnt++;

cout<<"\n\t..... Do You Want to Insert More Key: ";

cin>>ans;

}while(ans=='y'||ans=='Y');

int main()

int ch,k,index;

char ans;

hashfunction obj;

do

cout<<"\nt\t************dictionary(ADT)***********";

cout<<"\n\t 1:insert\n\t2:display\n\t3:search\n\t4:delete\n\t5:insert with replacement\n\t6:exit";

cout<<"\nenter your choice";

cin>>ch;

switch(ch)

case 1:

obj.insert();

break;

case 2:

obj.display();

break;

case 3:

cout<<"\nenter key which you want to search";

cin>>k;

index=obj.find(k);
if(index==-1){

cout<<"\n key not found";

break;

case 4:

cout<<"\nenter key which you want to delete";

cin>>k;

obj.delete1(k);

break;

case 5:obj.insert1();

break;

case 6:

break;

cout<<"\n......do you want to continue main menu";

cin>>ans;

}while(ans=='y'||ans=='Y');

OUTPUT-

t ************dictionary(ADT)***********

1:insert

2:display

3:search
4:delete

5:insert with replacement

6:exit

enter your choice1

enter the key61

enter the value1

do you want to insert more keysy

enter the key71

enter the value2

do you want to insert more keysy

enter the key52

enter the value3

do you want to insert more keysy

enter the key55

enter the value5

do you want to insert more keysn

......do you want to continue main menuy

t ************dictionary(ADT)***********

1:insert

2:display

3:search

4:delete

5:insert with replacement

6:exit

enter your choice2


h[0]-1 -1

h[1]61 1

h[2]71 2

h[3]52 3

h[4]-1 -1

h[5]55 5

h[6]-1 -1

h[7]-1 -1

h[8]-1 -1

h[9]-1 -1

......do you want to continue main menuy

t ************dictionary(ADT)***********

1:insert

2:display

3:search

4:delete

5:insert with replacement

6:exit

enter your choice5

Enter a key: 42

Enter value: 3

..... Do You Want to Insert More Key: n

......do you want to continue main menuy


t ************dictionary(ADT)***********

1:insert

2:display

3:search

4:delete

5:insert with replacement

6:exit

enter your choice2

h[0]-1 -1

h[1]61 1

h[2]42 3

h[3]52 3

h[4]71 2

h[5]55 5

h[6]-1 -1

h[7]-1 -1

h[8]-1 -1

h[9]-1 -1

......do you want to continue main menuy

t ************dictionary(ADT)***********

1:insert

2:display

3:search

4:delete

5:insert with replacement


6:exit

enter your choice3

enter key which you want to search42

42key is found at2location with value3

......do you want to continue main menuy

t ************dictionary(ADT)***********

1:insert

2:display

3:search

4:delete

5:insert with replacement

6:exit

enter your choice4

enter key which you want to delete71

71key is found at4location with value2key is deleted

......do you want to continue main menuy

t ************dictionary(ADT)***********

1:insert

2:display

3:search

4:delete

5:insert with replacement

6:exit

enter your choice2


h[0]-1 -1

h[1]61 1

h[2]42 3

h[3]52 3

h[4]-1 -1

h[5]55 5

h[6]-1 -1

h[7]-1 -1

h[8]-1 -1

h[9]-1 -1

......do you want to continue main menu

You might also like