通讯录功能:增加联系人、查找联系人、删除联系人、查看所有联系人
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//定义通讯录中名单结构体 :链表结点
struct Infor {
string name;
string phone;
string address;
Infor *next;
};
//创建通讯录
Infor *create() {
Infor *head, *r;
head = new Infor;
head -> next = NULL;
r = head;
return head;
}
//添加联系人
Infor *add(Infor *head) {
Infor *person = new Infor;
cout << "请输入姓名:";
cin >> person -> name;
cout << endl << "请输入电话号码:";
cin >> person -> phone;
cout << endl << "请输入地址:";
cin >> person -> address;
Infor *current = head; //current指针用于遍历链表找对应位置之后插入
while(current -> next != NULL && person -> name < current -> name) { //找到名字对应位置就插入(尾插法)
//还没找到位置就不断后移寻找
current = current -> next;
}
//找到对应位置或者current已经到达最后一个结点时,插入当前联系人
//先链后断(插在current指针之后)
person -> next = current -> next; //先链
current -> next = person; //后断
//最后返回插入后的链表
return head;
}
//查找联系人:根据姓名查找
Infor *find(Infor *head) {
Infor *person = head -> next; //从第一个结点开始查找
cout << "请输入要查找联系人的姓名:";
string name;
cin >> name;
//注:== 也可以实现全中文字符串的比较
while(person != NULL && person -> name != name) { //还未到达最后一个结点且未找到时不断往后查找
person = person -> next;
}
if(person == NULL) {
return NULL;
}
return person;
}
//删除联系人
bool del(Infor *head) {
string name;
cout << "请输入要删除的联系人名字:";
cin >> name;
Infor *person = head -> next;
Infor *prior = head; //用来记录前一个结点
//先查找联系人,找到后删除该联系人
while(person != NULL && person -> name != name) { //还未到达最后一个结点且未找到时不断往后查找
person = person -> next;
prior = prior -> next;
}
if(person == NULL) {
return false; //没有找到返回false
} else {
//找到,删除联系人
prior -> next = person -> next;
delete(person);
return true; //删除成功返回true
}
}
//显示通讯录所有联系人
void show(Infor *head) {
Infor *person = head -> next;
while(person != NULL) {
cout << "姓名:" << person -> name << endl;
cout << "电话:" << person -> phone << endl;
cout << "家庭地址:" << person -> address << endl;
cout << "============================================" << endl;
person = person -> next;
}
}
int main(int argc, char** argv) {
cout << "--------------------欢迎来到通讯录--------------------" << endl;
cout << " 按a添加" << endl;
cout << " 按d删除" << endl;
cout << " 按f查找" << endl;
cout << " 按s显示全部联系人" << endl;
cout << " 按e退出" << endl;
char chose;
cin >> chose;
//1、先创建通讯录
Infor *mailList = create();
while(chose != 'e') {
if(chose == 'a') { //添加
mailList = add(mailList);
} else if(chose == 'd') { //删除
bool result = del(mailList);
if(result == false) {
cout << "通讯录中没有找到该联系人" << endl;
} else {
cout << "删除成功!" << endl;
}
} else if(chose == 'f') {
Infor *person = find(mailList);
if(person != NULL) {
cout << "姓名:" << person -> name << endl;
cout << "电话:" << person -> phone << endl;
cout << "家庭地址:" << person -> address << endl;
} else {
cout << "该通讯录中没有该联系人,请重新输入!" << endl;
}
} else if(chose == 's') {
show(mailList);
} else {
cout << "输入选项有误,请重新输入" << endl;
}
//界面
cout << "--------------------欢迎来到通讯录--------------------" << endl;
cout << " 按a添加" << endl;
cout << " 按d删除" << endl;
cout << " 按f查找" << endl;
cout << " 按s显示全部联系人" << endl;
cout << " 按e退出" << endl;
cin >> chose;
}
return 0;
}