Table of contents | |
Introduction | |
Basic Overview of Phone Directory | |
Implementing the Phone Directory | |
Sample Code and Explanation | |
Sample Problems and Solutions: | |
Conclusion |
A phone directory is a common data structure used to store and manage contact information such as names and phone numbers. In this article, we will learn how to implement a phone directory in C++ using data structures and algorithms. We'll cover the basic concepts, provide example code, explain its functionality, and conclude with some sample problems to solidify your understanding.
A phone directory is a collection of contacts, typically organized by names or any other identifying key, along with their associated phone numbers. The primary operations performed on a phone directory include adding new contacts, searching for contacts, updating existing contacts, and deleting contacts.
There are several ways to implement a phone directory in C++. Let's explore three common approaches:
In this approach, we use an array of structures to store contact information. Each structure represents a contact, containing fields for name and phone number. The array provides an index-based lookup for efficient search operations.
In a linked list implementation, each node of the list represents a contact. The nodes contain fields for name and phone number, along with a pointer to the next node. This approach allows for dynamic memory allocation and flexible insertion and deletion of contacts.
A hash table implementation utilizes a hash function to convert the contact's key (e.g., name) into an index in an array. Each array index holds a linked list of contacts with the same hash value. This approach provides fast average-case search, insert, and delete operations.
#include <iostream>
#include <string>
const int MAX_CONTACTS = 100;
struct Contact {
std::string name;
std::string phoneNumber;
};
Contact phoneDirectory[MAX_CONTACTS];
int numOfContacts = 0;
void addContact(const std::string& name, const std::string& phoneNumber) {
if (numOfContacts < MAX_CONTACTS) {
phoneDirectory[numOfContacts].name = name;
phoneDirectory[numOfContacts].phoneNumber = phoneNumber;
numOfContacts++;
std::cout << "Contact added successfully.\n";
} else {
std::cout << "Phone directory is full.\n";
}
}
void searchContact(const std::string& name) {
for (int i = 0; i < numOfContacts; i++) {
if (phoneDirectory[i].name == name) {
std::cout << "Contact found: " << phoneDirectory[i].name << " - "
<< phoneDirectory[i].phoneNumber << "\n";
return;
}
}
std::cout << "Contact not found.\n";
}
// Additional functions like updateContact and deleteContact can be added
int main() {
addContact("John Doe", "1234567890");
addContact("Jane Smith", "9876543210");
searchContact("John Doe");
searchContact("Alice Johnson");
return 0;
}
Output:
Contact added successfully.
Contact added successfully.
Contact found: John Doe - 1234567890
Contact not found.
Explanation:
#include <iostream>
#include <string>
struct Contact {
std::string name;
std::string phoneNumber;
Contact* next;
};
Contact* phoneDirectory = nullptr;
void addContact(const std::string& name, const std::string& phoneNumber) {
Contact* newContact = new Contact;
newContact->name = name;
newContact->phoneNumber = phoneNumber;
newContact->next = phoneDirectory;
phoneDirectory = newContact;
std::cout << "Contact added successfully.\n";
}
void searchContact(const std::string& name) {
Contact* current = phoneDirectory;
while (current != nullptr) {
if (current->name == name) {
std::cout << "Contact found: " << current->name << " - "
<< current->phoneNumber << "\n";
return;
}
current = current->next;
}
std::cout << "Contact not found.\n";
}
// Additional functions like updateContact and deleteContact can be added
int main() {
addContact("John Doe", "1234567890");
addContact("Jane Smith", "9876543210");
searchContact("John Doe");
searchContact("Alice Johnson");
return 0;
}
Output:
Contact added successfully.
Contact added successfully.
Contact found: John Doe - 1234567890
Contact not found.
Explanation:
#include <iostream>
#include <string>
#include <unordered_map>
std::unordered_map<std::string, std::string> phoneDirectory;
void addContact(const std::string& name, const std::string& phoneNumber) {
phoneDirectory[name] = phoneNumber;
std::cout << "Contact added successfully.\n";
}
void searchContact(const std::string& name) {
if (phoneDirectory.count(name) > 0) {
std::cout << "Contact found: " << name << " - " << phoneDirectory[name] << "\n";
} else {
std::cout << "Contact not found.\n";
}
}
// Additional functions like updateContact and deleteContact can be added
int main() {
addContact("John Doe", "1234567890");
addContact("Jane Smith", "9876543210");
searchContact("John Doe");
searchContact("Alice Johnson");
return 0;
}
Output:
Contact added successfully.
Contact added successfully.
Contact found: John Doe - 1234567890
Contact not found.
Explanation:
Problem 1: Implement a function to update the phone number of an existing contact in the phone directory.
void updateContact(const std::string& name, const std::string& newPhoneNumber) {
if (phoneDirectory.count(name) > 0) {
phoneDirectory[name] = newPhoneNumber;
std::cout << "Contact updated successfully.\n";
} else {
std::cout << "Contact not found.\n";
}
}
Problem 2: Implement a function to delete a contact from the phone directory.
void deleteContact(const std::string& name) {
if (phoneDirectory.erase(name) > 0) {
std::cout << "Contact deleted successfully.\n";
} else {
std::cout << "Contact not found.\n";
}
}
In this article, we explored different approaches to implementing a phone directory in C++. We covered array of structures, linked list, and hash table implementations. Each approach has its own advantages and trade-offs. By understanding these implementations and their associated functions, you can efficiently store and manage contact information in your own phone directory applications.
153 videos|115 docs|24 tests
|
|
Explore Courses for Software Development exam
|