用单链表设计通讯录的程序目的
时间: 2024-03-18 07:46:01 浏览: 18
用单链表设计通讯录的程序的目的是为了实现通讯录的存储、查找、添加、删除等基本功能。通过单链表的结构,可以方便地实现对通讯录中联系人信息的管理。具体来说,利用单链表可以将联系人信息按照一定的顺序进行存储和排序,同时还可以快速地查找指定联系人的信息,方便用户随时随地地查询通讯录中的信息。此外,通过单链表,还可以方便地实现新增联系人、修改联系人信息、删除联系人等操作,从而实现对通讯录的高效管理。
相关问题
c++用单链表实现通讯录
好的,我可以为您提供一个基本的单链表通讯录的实现。首先,我们需要定义一个通讯录的节点结构体,包含姓名、电话号码、邮箱等信息:
```c++
struct Contact {
std::string name;
std::string phone;
std::string email;
Contact* next;
};
```
其中,`next`指向下一个节点。接下来,我们需要定义一个用于管理通讯录的类 `ContactList`,其中包括添加、删除、查找联系人等方法:
```c++
class ContactList {
public:
ContactList() {
head = nullptr;
}
~ContactList() {
Contact* current = head;
while (current != nullptr) {
Contact* next = current->next;
delete current;
current = next;
}
}
void addContact(const std::string& name, const std::string& phone, const std::string& email) {
Contact* newContact = new Contact();
newContact->name = name;
newContact->phone = phone;
newContact->email = email;
newContact->next = head;
head = newContact;
}
void removeContact(const std::string& name) {
Contact* current = head;
Contact* previous = nullptr;
while (current != nullptr) {
if (current->name == name) {
if (previous == nullptr) {
head = current->next;
} else {
previous->next = current->next;
}
delete current;
return;
}
previous = current;
current = current->next;
}
}
Contact* findContact(const std::string& name) {
Contact* current = head;
while (current != nullptr) {
if (current->name == name) {
return current;
}
current = current->next;
}
return nullptr;
}
private:
Contact* head;
};
```
上述代码中,`addContact`方法在链表头部插入一个新的节点,`removeContact`方法删除指定姓名的节点,`findContact`方法查找指定姓名的节点。
使用示例:
```c++
ContactList myContacts;
myContacts.addContact("Alice", "123456", "alice@example.com");
myContacts.addContact("Bob", "789012", "bob@example.com");
myContacts.addContact("Charlie", "345678", "charlie@example.com");
Contact* contact = myContacts.findContact("Bob");
if (contact != nullptr) {
std::cout << "Phone: " << contact->phone << std::endl;
}
myContacts.removeContact("Charlie");
```
以上是一个基本的单链表通讯录实现,您可以根据需要进行扩展和修改。
单链表创建通讯录
好的,让我来帮助你回答这个问题。
首先,我们需要定义一个通讯录中每个联系人的信息。一个联系人的信息可能包括姓名、电话号码、电子邮件地址等等。我们可以使用一个结构体来表示这些信息:
```
struct Contact {
char name[50];
char phone_number[20];
char email[50];
};
```
接下来,我们需要定义一个单链表来存储所有联系人的信息。单链表中的每个节点包含一个联系人信息和指向下一个节点的指针。我们可以使用如下的结构体来表示一个单链表节点:
```
struct ListNode {
Contact contact;
ListNode *next;
};
```
现在我们可以开始编写创建通讯录的代码。首先,我们需要让用户输入通讯录中联系人的个数。然后,我们可以循环读入每个联系人的信息,并将其插入到单链表中。
下面是一个示例代码:
```
#include <iostream>
#include <cstring>
using namespace std;
struct Contact {
char name[50];
char phone_number[20];
char email[50];
};
struct ListNode {
Contact contact;
ListNode *next;
};
ListNode* createList(int n) {
ListNode *head = nullptr, *tail = nullptr;
for (int i = 0; i < n; i++) {
Contact c;
cout << "Enter name: ";
cin.getline(c.name, 50);
cout << "Enter phone number: ";
cin.getline(c.phone_number, 20);
cout << "Enter email: ";
cin.getline(c.email, 50);
ListNode *node = new ListNode;
node->contact = c;
node->next = nullptr;
if (head == nullptr) {
head = tail = node;
} else {
tail->next = node;
tail = node;
}
}
return head;
}
int main() {
int n;
cout << "Enter number of contacts: ";
cin >> n;
cin.ignore(); // Ignore the newline character left in the input stream
ListNode *head = createList(n);
// Do something with the list
return 0;
}
```
这个代码会先让用户输入通讯录中联系人的个数,然后循环读入每个联系人的信息,并将其插入到单链表中。最后,它返回单链表的头节点。
希望这个代码能对你有所帮助!