0% found this document useful (0 votes)
10 views5 pages

P2

The document presents a C++ implementation of a hash table using chaining for collision resolution. It includes methods for inserting, searching, and deleting key-value pairs, as well as displaying the contents of the hash table. The program allows user interaction through a menu-driven interface to perform these operations.

Uploaded by

yebop47991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

P2

The document presents a C++ implementation of a hash table using chaining for collision resolution. It includes methods for inserting, searching, and deleting key-value pairs, as well as displaying the contents of the hash table. The program allows user interaction through a menu-driven interface to perform these operations.

Uploaded by

yebop47991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Roll No.

: 12

//Assignment No. : 02

/* Problem Statement: Implement all the functions of a dictionary (ADT)


using hashing and handle collisions using chaining with/ without replacement.
Data: set of (key, value) pairs, keys are mapped to values, keys must be
comparable,
keys must be unique standard operation: insert (key, value), find (key),
Delete(key) (python). */

#include <iostream>
#include <vector>

using namespace std;

class Hashing {
struct Node {
long key;
string value;

Node() : key(-1), value("") {}


};

vector<Node> table;
int size;
int count;

long hashFunction(long key) {


return key % size;
}

public:
Hashing(int size) : size(size), count(0) {
table.resize(size);
}

void insert(long key, const string& value) {


if (count >= size) {
cout << "Hash table is full!" << endl;
return;
}

int index = hashFunction(key);


int startIndex = index;

while (table[index].key != -1 && table[index].key != -2 && table[index].key


!= key) {
cout << "Collision at index " << index << ", probing..." << endl;
index = (index + 1) % size;
if (index == startIndex) {
cout << "No empty slots found, cannot insert!" << endl;
return;
}
}

if (table[index].key == -1 || table[index].key == -2) {


count++;
}
table[index].key = key;
table[index].value = value;
}

void remove(long key) {


int index = hashFunction(key);
int startIndex = index;

while (table[index].key != -1) {


if (table[index].key == key) {
table[index].key = -2;
table[index].value = "";
count--;
cout << "Key " << key << " removed from index " << index << endl;
return;
}
index = (index + 1) % size;
if (index == startIndex) {
break;
}
}
cout << "Key " << key << " not found!" << endl;
}

void search(long key) {


int index = hashFunction(key);
int startIndex = index;

while (table[index].key != -1) {


if (table[index].key == key) {
cout << "Key " << key << " found with value: " <<
table[index].value << " at index: " << index << endl;
return;
}
index = (index + 1) % size;
if (index == startIndex) {
break;
}
}
cout << "Key " << key << " not found." << endl;
}

void display() {
for (int i = 0; i < size; i++) {
if (table[i].key == -1) {
cout << "Index " << i << ": EMPTY" << endl;
} else if (table[i].key == -2) {
cout << "Index " << i << ": DELETED" << endl;
} else {
cout << "Index " << i << ": (" << table[i].key << ", " <<
table[i].value << ")" << endl;
}
}
}
};

int main() {
int n;
cout << "Enter the size of the hash table: ";
cin >> n;

Hashing hashTable(n);
int choice, num, key;
string value;
do{
cout << "***********************************" << endl;
cout << "\n Menu" << endl;
cout<<endl<<"1. Insert \n2. Search \n3. Delete \n4. Display \n5.
Exit"<<endl;
cout << "***********************************" << endl;
cout<<"Enter your Choice :";
cin>>choice;

switch(choice)
{
case 1:
cout<<"How many keys have to insert? :";
cin>>num;
for(int i=0;i<num;i++)
{
cout<<"Enter the key:";
cin>>key;
cout<<"Enter the value:";
cin>>value;
hashTable.insert(key,value);
}
break;
case 2:
cout<<"How many keys have to search? :";
cin>>num;
for(int i=0;i<num;i++)
{
cout<<"Enter the key:";
cin>>key;
hashTable.search(key);
}
break;
case 3:
cout<<"How many keys have to delete? :";
cin>>num;
for(int i=0;i<num;i++)
{
cout<<"Enter the key: ";
cin>>key;
hashTable.remove(key);
}
break;
case 4:
hashTable.display();
break;
case 5:
cout<<"Exiting..."<<endl;
break;
default:
cout<<"Wrong Input, Try Again"<<endl;
}
}while(choice!=5);
return 0;
}

/* Output

PS C:\Users\Admin\OneDrive\Desktop\c++\DSA> g++ P2.cpp -o 1


PS C:\Users\Admin\OneDrive\Desktop\c++\DSA> ./1
Enter the size of the hash table: 10
***********************************

Menu

1. Insert
2. Search
3. Delete
4. Display
5. Exit
***********************************
Enter your Choice :1
How many keys have to insert? :6
Enter the key:5
Enter the value:John
Enter the key:1
Enter the value:Tom
Enter the key:10
Enter the value:James
Enter the key:26
Enter the value:Tina
Enter the key:99
Enter the value:Sana
Enter the key:105
Enter the value:Mary
Collision at index 5, probing...
Collision at index 6, probing...

1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :2
How many keys have to search? :1
Enter the key:5
Key 5 found with value: John at index: 5

1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :4
Index 0: (10, James)
Index 1: (1, Tom)
Index 2: EMPTY
Index 3: EMPTY
Index 4: EMPTY
Index 5: (5, John)
Index 6: (26, Tina)
Index 7: (105, Mary)
Index 8: EMPTY
Index 9: (99, Sana)

1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :3
How many keys have to delete? :1
Enter the key: 5
Key 5 removed from index 5

1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :4
Index 0: (10, James)
Index 1: (1, Tom)
Index 2: EMPTY
Index 3: EMPTY
Index 4: EMPTY
Index 5: DELETED
Index 6: (26, Tina)
Index 7: (105, Mary)
Index 8: EMPTY
Index 9: (99, Sana)

1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your Choice :5
Exiting...

*/

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