c++用链表写一个通讯录,加文件保存,实现增删查改
时间: 2024-01-22 20:18:59 浏览: 94
好的,以下是一个简单的通讯录的链表实现,包括增删查改和文件保存功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 通讯录联系人节点结构体
struct ContactPerson {
string name; // 姓名
string phone; // 电话
string email; // 邮箱
ContactPerson* next; // 指向下一个节点的指针
};
// 通讯录链表类
class ContactList {
public:
ContactList(); // 构造函数
~ContactList(); // 析构函数
void addContactPerson(); // 添加联系人
void deleteContactPerson(); // 删除联系人
void searchContactPerson(); // 查找联系人
void modifyContactPerson(); // 修改联系人
void displayContactList(); // 显示所有联系人
void saveToFile(); // 将当前通讯录保存到文件
void loadFromFile(); // 从文件中读取通讯录
private:
ContactPerson* head; // 头指针
};
ContactList::ContactList() {
head = NULL;
}
ContactList::~ContactList() {
ContactPerson* p = head;
while (p) {
ContactPerson* q = p;
p = p->next;
delete q;
}
}
void ContactList::addContactPerson() {
ContactPerson* newPerson = new ContactPerson;
cout << "请输入联系人姓名:";
cin >> newPerson->name;
cout << "请输入联系人电话:";
cin >> newPerson->phone;
cout << "请输入联系人邮箱:";
cin >> newPerson->email;
newPerson->next = NULL;
if (head == NULL) {
head = newPerson;
} else {
ContactPerson* p = head;
while (p->next) {
p = p->next;
}
p->next = newPerson;
}
cout << "添加成功!" << endl;
}
void ContactList::deleteContactPerson() {
if (head == NULL) {
cout << "通讯录为空!" << endl;
return;
}
string name;
cout << "请输入要删除的联系人姓名:";
cin >> name;
ContactPerson* p = head;
ContactPerson* q = NULL;
while (p) {
if (p->name == name) {
if (q == NULL) {
head = p->next;
} else {
q->next = p->next;
}
delete p;
cout << "删除成功!" << endl;
return;
}
q = p;
p = p->next;
}
cout << "未找到该联系人!" << endl;
}
void ContactList::searchContactPerson() {
if (head == NULL) {
cout << "通讯录为空!" << endl;
return;
}
string name;
cout << "请输入要查找的联系人姓名:";
cin >> name;
ContactPerson* p = head;
while (p) {
if (p->name == name) {
cout << "姓名:" << p->name << endl;
cout << "电话:" << p->phone << endl;
cout << "邮箱:" << p->email << endl;
return;
}
p = p->next;
}
cout << "未找到该联系人!" << endl;
}
void ContactList::modifyContactPerson() {
if (head == NULL) {
cout << "通讯录为空!" << endl;
return;
}
string name;
cout << "请输入要修改的联系人姓名:";
cin >> name;
ContactPerson* p = head;
while (p) {
if (p->name == name) {
cout << "请输入新的联系人姓名:";
cin >> p->name;
cout << "请输入新的联系人电话:";
cin >> p->phone;
cout << "请输入新的联系人邮箱:";
cin >> p->email;
cout << "修改成功!" << endl;
return;
}
p = p->next;
}
cout << "未找到该联系人!" << endl;
}
void ContactList::displayContactList() {
if (head == NULL) {
cout << "通讯录为空!" << endl;
return;
}
ContactPerson* p = head;
while (p) {
cout << "姓名:" << p->name << endl;
cout << "电话:" << p->phone << endl;
cout << "邮箱:" << p->email << endl;
p = p->next;
}
}
void ContactList::saveToFile() {
ofstream file("contacts.txt");
if (!file) {
cout << "无法打开文件!" << endl;
return;
}
ContactPerson* p = head;
while (p) {
file << p->name << " " << p->phone << " " << p->email << endl;
p = p->next;
}
file.close();
cout << "保存成功!" << endl;
}
void ContactList::loadFromFile() {
ifstream file("contacts.txt");
if (!file) {
cout << "无法打开文件!" << endl;
return;
}
ContactPerson* p = head;
while (p) {
ContactPerson* q = p;
p = p->next;
delete q;
}
head = NULL;
string line;
while (getline(file, line)) {
ContactPerson* newPerson = new ContactPerson;
sscanf(line.c_str(), "%s %s %s", &newPerson->name, &newPerson->phone, &newPerson->email);
newPerson->next = NULL;
if (head == NULL) {
head = newPerson;
} else {
ContactPerson* p = head;
while (p->next) {
p = p->next;
}
p->next = newPerson;
}
}
file.close();
cout << "读取成功!" << endl;
}
int main() {
ContactList contactList;
int choice = -1;
while (choice != 0) {
cout << "请选择操作:" << endl;
cout << "1. 添加联系人" << endl;
cout << "2. 删除联系人" << endl;
cout << "3. 查找联系人" << endl;
cout << "4. 修改联系人" << endl;
cout << "5. 显示所有联系人" << endl;
cout << "6. 保存到文件" << endl;
cout << "7. 从文件中读取" << endl;
cout << "0. 退出程序" << endl;
cout << "请选择:";
cin >> choice;
switch (choice) {
case 1:
contactList.addContactPerson();
break;
case 2:
contactList.deleteContactPerson();
break;
case 3:
contactList.searchContactPerson();
break;
case 4:
contactList.modifyContactPerson();
break;
case 5:
contactList.displayContactList();
break;
case 6:
contactList.saveToFile();
break;
case 7:
contactList.loadFromFile();
break;
case 0:
break;
default:
cout << "无效的选择!" << endl;
break;
}
}
return 0;
}
```
以上代码中,通讯录联系人节点结构体包含姓名、电话、邮箱和指向下一个节点的指针。通讯录链表类包含添加、删除、查找、修改、显示、保存到文件和从文件中读取等功能。其中保存和读取都是通过文件流实现的,文件格式为每行一个联系人,每个联系人用空格分隔姓名、电话和邮箱。
阅读全文