Dsa1.Code Output
Dsa1.Code Output
Code:
#include <iostream>
#include <unordered_map>
class TelephoneBook {
private:
unordered_map<string, string> phoneBook; // Hash table (key: name, value: phone number)
public:
// Insert a contact
phoneBook[name] = number;
cout << "Contact added: " << name << " -> " << number << endl;
if (phoneBook.find(name) != phoneBook.end()) {
cout << "Phone Number of " << name << ": " << phoneBook[name] << endl;
} else {
// Delete a contact
if (phoneBook.erase(name)) {
} else {
}
// Display all contacts
void displayContacts() {
if (phoneBook.empty()) {
return;
cout << entry.first << " -> " << entry.second << endl;
};
// Driver Code
int main() {
TelephoneBook tb;
int choice;
do {
cout << "\n1. Insert Contact\n2. Search Contact\n3. Delete Contact\n4. Display Contacts\n5.
Exit\nEnter choice: ";
switch (choice) {
case 1:
tb.insertContact(name, number);
break;
case 2:
tb.searchContact(name);
break;
case 3:
tb.deleteContact(name);
break;
case 4:
tb.displayContacts();
break;
case 5:
break;
default:
return 0;
}
Output:
1. Insert Contact
2. Search Contact
3. Delete Contact
4. Display Contacts
5. Exit
Enter choice: 1
1. Insert Contact
2. Search Contact
3. Delete Contact
4. Display Contacts
5. Exit
Enter choice: 1
1. Insert Contact
2. Search Contact
3. Delete Contact
4. Display Contacts
5. Exit
Enter choice: 2
2. Search Contact
3. Delete Contact
4. Display Contacts
5. Exit
Enter choice: 3
1. Insert Contact
2. Search Contact
3. Delete Contact
4. Display Contacts
5. Exit
Enter choice: 4
Telephone Book:
1. Insert Contact
2. Search Contact
3. Delete Contact
4. Display Contacts
5. Exit
Enter choice: