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

P1

The document presents a C++ implementation of a telephone book database using a hash table to store client telephone numbers. It includes methods for inserting, searching, deleting, and displaying entries, as well as handling collisions. The program allows user interaction through a menu-driven interface for managing the telephone book data.

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)
7 views5 pages

P1

The document presents a C++ implementation of a telephone book database using a hash table to store client telephone numbers. It includes methods for inserting, searching, deleting, and displaying entries, as well as handling collisions. The program allows user interaction through a menu-driven interface for managing the telephone book data.

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

// Assginment No.: 01

/*Problem Statement: Consider a telephone book database of N clients.


Make use of a hash table implementation to quickly look up client's telephone
number.
Make use of two collision handling techniques and compare them using number of
comparisons
required to find a set of telephone numbers.*/

#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;

struct Node {
int key;
string value;
Node(int k, string val) : key(k), value(val) {}
};

class Hashing {
vector<list<Node>> table;
int size;

int hashFunction(int key) {


return key % size;
}

public:
Hashing(int size) {
this->size = size;
this->table = vector<list<Node>>(size);
}

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


int index = hashFunction(key);
for (auto& node : table[index]) {
if (node.key == key) {
node.value = value;
return;
}
}
table[index].emplace_back(key, value);
}

void removeByName(const string& name) {


for (int i = 0; i < size; i++) {
table[i].remove_if([&name](const Node& node) { return node.value ==
name; });
}
}

void display() {
for (int i = 0; i < size; i++) {
cout << "Index " << i << ": ";
for (const auto& node : table[i]) {
cout << "(" << node.key << ", " << node.value << ") --> ";
}
cout << "nullptr" << endl;
}
}

void searchByName(const string& name) {


for (int i = 0; i < size; i++) {
for (const auto& node : table[i]) {
if (node.value == name) {
cout << "Client " << name << " found with Telephone No.: " <<
node.key << " at index: " << i << endl;
return;
}
}
}
cout << "Client " << name << " not found." << 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 << "\t\tMenu" << endl;
cout << "\n1. Insert \n2. Search by Name \n3. Delete by Name \n4. Display \
n5. Exit" << endl;
cout << "***********************************" << endl;
cout << "Enter your Choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter the number of Telephone Numbers to insert: ";
cin >> num;
for (int i = 0; i < num; i++) {
cout << "Enter Telephone No.: ";
cin >> key;
cin.ignore(); // Ignore newline character
cout << "Enter the Name: ";
getline(cin, value);
hashTable.insert(key, value);
}
break;
case 2:
cout << "Enter the Name to search: ";
cin.ignore(); // Ignore newline character
getline(cin, value);
hashTable.searchByName(value);
break;
case 3:
cout << "Enter the Name to delete: ";
cin.ignore(); // Ignore newline character
getline(cin, value);
hashTable.removeByName(value);
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++ P1.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 by Name
3. Delete by Name
4. Display
5. Exit
***********************************
Enter your Choice: 1
Enter the number of Telephone Numbers to insert: 5
Enter Telephone No.: 987456321
Enter the Name: tom
Enter Telephone No.: 87456329
Enter the Name: john
Enter Telephone No.: 745688139
Enter the Name: mary
Enter Telephone No.: 84563217
Enter the Name: sana
Enter Telephone No.: 974586321
Enter the Name: james
***********************************
Menu

1. Insert
2. Search by Name
3. Delete by Name
4. Display
5. Exit
***********************************
Enter your Choice: 4
Index 0: nullptr
Index 1: (987456321, tom) --> (974586321, james) --> nullptr
Index 2: nullptr
Index 3: nullptr
Index 4: nullptr
Index 5: nullptr
Index 6: nullptr
Index 7: (84563217, sana) --> nullptr
Index 8: nullptr
Index 9: (87456329, john) --> (745688139, mary) --> nullptr
***********************************
Menu

1. Insert
2. Search by Name
3. Delete by Name
4. Display
5. Exit
***********************************
Enter your Choice: 2
Enter the Name to search: sana
Client sana found with Telephone No.: 84563217 at index: 7
***********************************
Menu

1. Insert
2. Search by Name
3. Delete by Name
4. Display
5. Exit
***********************************
Enter your Choice: 3
Enter the Name to delete: tom
***********************************
Menu

1. Insert
2. Search by Name
3. Delete by Name
4. Display
5. Exit
***********************************
Enter your Choice: 4
Index 0: nullptr
Index 1: (974586321, james) --> nullptr
Index 2: nullptr
Index 3: nullptr
Index 4: nullptr
Index 5: nullptr
Index 6: nullptr
Index 7: (84563217, sana) --> nullptr
Index 8: nullptr
Index 9: (87456329, john) --> (745688139, mary) --> nullptr
***********************************
Menu

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

You might also like