用C++实现链表版本通讯录

这是一个使用C++编写的链表实现的简单通讯录系统,包括添加、删除、查看、搜索和更新联系人信息的功能。用户可以通过主菜单进行操作,系统采用链表数据结构存储联系人数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string>
using namespace std;
class Address;

class Contact{
private:
		string name;
		string sex;
		string tel;
		string QQ;
		string address;
		string addition;
		Contact *next;
public:
		Contact();
		friend class Address; 
		
};
Contact::Contact()
{
	next = NULL;
}
class Address{
public:
		Address();
		~Address();
		int show();
		void insert();
		void delete_per();
		void display();
		void search();
		void update(); 
private:
		Contact *head;
};
Address::Address()
{
	head = new Contact;
	if(head == NULL)
	{
		cout<<"fail create"<<endl;
	}
}
Address::~Address()
{
	delete head;
}
int Address::show()                                                              //主菜单函数
{
	int choice = 0 ;
	cout<<"\t\t\t\t**************************"<<endl;
	cout<<"\t\t\t\t*   通讯录c++简易版本    *"<<endl;
	cout<<"\t\t\t\t**************************"<<endl;
	cout<<"\t\t\t\t* 1、添加    2、删除     *"<<endl;
	cout<<"\t\t\t\t**************************"<<endl;
	cout<<"\t\t\t\t* 3、查看    4、搜索     *"<<endl;
	cout<<"\t\t\t\t**************************"<<endl;
	cout<<"\t\t\t\t* 5、更新    6、退出     *"<<endl;
	cout<<"\t\t\t\t**************************"<<endl;
	cout<<"\t\t\t\t请输入选择:";
	cin>>choice;
	while(!(choice >= 1&&choice <= 6))
	{
		while(getchar()!='\n');
		cout<<"输入有误,请重新输入!";
		cin>>choice;
	}
	return choice;
	
}
void Address::insert()                                                          //添加联系人
{
	Contact *p = head;
	char relay = 0;
	while(p->next != NULL)
	{
		p = p->next;
	}
	Contact *person = new Contact;
	cout<<"请输入姓名:";
	cin>>person->name;
	cout<<"请输入性别:";
	cin>>person->sex;
	cout<<"请输入电话:";
	cin>>person->tel;
	cout<<"请输入QQ:";
	cin>>person->QQ;
	cout<<"请输入住址:";
	cin>>person->address;
	cout<<"请输入备注:";
	cin>>person->addition;
	p->next = person;
	person->next = NULL;
	cout<<"\n添加成功,是否继续添加?(y/n)";
	cin>>relay;
	while(!(relay == 'y'||relay == 'Y'||relay == 'N'||relay == 'n'))
	{
		cout<<"输入错误,请重新输入(y/n):";
		cin>>relay;
	}
	if(relay == 'y'||relay == 'y')
	{
		system("clear");
		insert();
	}
}
void Address::delete_per()                                                      //删除联系人
{
	string m_name;
	Contact *p = head;
	Contact *pre = head;
	int flag = 0;
	cout<<"请输入你要删除的联系人姓名!";
	cin>>m_name;
	while(p->next != NULL)
	{
		pre = p;
		p = p->next;
		if(p->name == m_name)
		{
			pre->next = p->next;
			delete p;
			p = NULL;
			flag = 1;
			break;
		}
	}
	if(flag == 1)
	{
		cout<<"删除成功!"<<endl;
	}
	else
	{
		cout<<"您删除的联系人不存在,删除失败!"<<endl;
	}
}

void Address::display()                                                         //查看联系人 
{
	Contact *p = head;
	while(p->next != NULL)
	{
		p = p->next;
		cout<<endl<<"======================================="<<endl;
		cout<<"姓名:"<<p->name<<endl;
		cout<<"性别:"<<p->sex<<endl;
		cout<<"电话:"<<p->tel<<endl;
		cout<<"QQ:"<<p->QQ<<endl;
		cout<<"地址:"<<p->address<<endl;
		cout<<"备注:"<<p->addition<<endl;				
	}
	
}
void Address::search()                                                          //搜索联系人
{
	string m_name;
	Contact *p = head;
	int flag = 0;
	cout<<"请输入你要搜索的联系人姓名:";
	cin>>m_name;
	while(p->next != NULL)
	{
		p = p->next;
		if(p->name == m_name)
		{
			cout<<endl<<"======================================="<<endl;
			cout<<"姓名:"<<p->name<<endl;
			cout<<"性别:"<<p->sex<<endl;
			cout<<"电话:"<<p->tel<<endl;
			cout<<"QQ:"<<p->QQ<<endl;
			cout<<"地址:"<<p->address<<endl;
			cout<<"备注:"<<p->addition<<endl;				
			flag = 1;
		}
	}
	if(flag == 1)
	{
		cout<<"\n查询成功!"<<endl;
	}
	else
	{
		cout<<"您查询的联系人不存在,删除失败!"<<endl;
	}
}
void Address::update()                                                            //修改联系人
{
	Contact *p = head;
	string m_name;
	int flag = 0;
	
	cout<<"请输入你要更新的姓名:";
	cin>>m_name;
	while(p->next != NULL)
	{
		p = p->next;
		if(p->name == m_name)
		{
			cout<<"请更新性别:";
			cin>>p->sex;
			cout<<"请更新电话:";
			cin>>p->tel;
			cout<<"请更新QQ:";
			cin>>p->QQ;
			cout<<"请更新住址:";
			cin>>p->address;
			cout<<"请更新备注:";
			cin>>p->addition;
			flag = 1;
			break;
		}
	}
	if(flag == 1)
	{
		cout<<"\n更新成功"<<endl;
	}
	else
	{
		cout<<"查无此人,更新失败!"<<endl;
	}
}

int main()
{
	Address *person = new Address;
	int choice = 0;
	while(1)
	{
		system("clear");
		choice = person->show();
		switch(choice)
		{
			case 1:
			{
				system("clear");
				person->insert();
				break;
			}
			case 2:
			{
				system("clear");
				person->delete_per();
				break;
			}
			case 3:
			{
				system("clear");
				person->display();
				break;
			}
			case 4:
			{
				system("clear");
				person->search();
				break;
			}
			case 5:
			{
				system("clear");
				person->update();
				break;
			}
			case 6:
			{
				exit(0);
			}
		}
		cout<<"\n\n按任意键返回.....";
		getchar();
		getchar();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值