P1
P1
: 12
// Assginment No.: 01
#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;
public:
Hashing(int size) {
this->size = size;
this->table = vector<list<Node>>(size);
}
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;
}
}
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> ./1
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...
*/