c++链表实现通讯录管理系统

c++实现通讯录管理系统
用于实现通讯录的管理,通过链表实现。联系人修改函数省略未写。

addressbook.cpp
#include <iostream>
#include <string.h>
#include "subfunction.h"
using namespace std;
//显示菜单
//***************
//***  1.添加  ***
//***  2.显示  ***
//***  3.删除  ***
//***  4.查找  ***
//***  5.修改  ***
//***  6.清空  ***
//***  7.退出  ***
//***************
void menudisplay()
{
	cout << "****************" << endl;
	cout << "***  1.添加  ***" << endl;
	cout << "***  2.显示  ***" << endl;
	cout << "***  3.删除  ***" << endl;
	cout << "***  4.查找  ***" << endl;
	cout << "***  5.修改  ***" << endl;
	cout << "***  6.清空  ***" << endl;
	cout << "***  7.退出  ***" << endl;
	cout << "****************" << endl;
}

//选择框架
subfunction.cpp
int select()
{
	struct people *head = NULL;//创建通讯录
	int select;
	while (1)
	{
		menudisplay();
		cout << "请输入您要选择的操作编号: " << endl;
		cin >> select;
		cout << "您选择的操作编号为: " << select << endl<<endl;
		switch (select)
		{
		case 1://***  1.添加  ***
			head=addpeople(head);
			break;
		case 2://***  2.显示  ***
			printpeople(head);
			break;
		case 3://***  3.删除  ***
			head=deletpeople(head);
			break;
		case 4://***  4.查找  ***
			findpeople( head);
			break;
		case 5://***  5.修改  ***
			break;
		case 6://***  6.清空  ***
			head= deletlist(head);
			break;
		case 7://***  7.退出  ***
			cout << "再见!" << endl<<endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
}


int main()
{
	select();
	return 0;
}

subfunction.h
#include "subfunction.h"
#include <iostream>
#include <string>
using namespace std;
//***  1.添加  ***
struct people * addpeople(struct people * head)
{
	struct people *newpeople = new(struct people);
		cout << "请输入联系人姓名(拼音):" << endl;
		cin >> newpeople->name;
		cout << "请输入联系人年龄:" << endl;
		cin >> newpeople->age;
		cout << "请输入联系人电话:" << endl;
		cin >> newpeople->phonenumber;
		cout << "请输入联系人性别(M or W):" << endl;
		cin >> newpeople->sex;
		newpeople->p=NULL;
		struct people * pt=head;
		struct people * q = head;
		if (head == NULL)
		{
			head = newpeople;
		}
		else
		{
		while (pt != NULL)
		{
			q = pt;
			pt = pt->p;
		}
		q->p = newpeople;
		}
		return head;
}

//***  2.显示  ***
void printpeople(struct people *head)
{
	struct people *ptb = head;
	if (ptb == NULL)
	{
		cout << "通讯录无联系人" << endl;
	}
	else
	{
		while (ptb != NULL)
		{
			cout << "联系人姓名(拼音):" << ptb->name << endl;
			cout << "联系人年龄:" << ptb->age << endl;
			cout << "联系人电话:" << ptb->phonenumber << endl;
			cout << "联系人性别(M or W):" << ptb->sex << endl << endl;
			ptb = ptb->p;
		}
	}
}

//***  3.删除  ***
struct people * deletpeople(struct people * head)
{
	struct people delpeople; //待删除对象
	cout << "请输入联系人姓名(拼音):" << endl;
	cin >> delpeople.name;
	cout << "请输入联系人年龄:" << endl;
	cin >> delpeople.age;
	cout << "请输入联系人电话:" << endl;
	cin >> delpeople.phonenumber;
	cout << "请输入联系人性别(M or W):" << endl;
	cin >> delpeople.sex;
	delpeople.p = NULL;

	struct people *ptb = head;
	struct people *qtb = head;
	if (ptb == NULL)
	{
		cout << "通讯录为空" << endl;
	}
	else
	{
		if (ptb->phonenumber == delpeople.phonenumber)
		{
			head = head->p;
			cout << "已删除该联系人" << endl;
			delete ptb;
		}
		else
		{
			qtb = ptb;
			ptb = ptb->p;
			while (ptb != NULL)
			{
				if (ptb->phonenumber == delpeople.phonenumber)
				{
					qtb->p = ptb->p;
					cout << "已删除该联系人" << endl;
					delete ptb;
					break;
				}
				ptb = ptb->p;
			}
			if (ptb == NULL)
				cout << "无此联系人" << endl;
		}
	}
	return head;
}

//***  4.查找  ***
void findpeople(struct people * head)
{
	string name;
	cout << "请输入联系人姓名:" << endl;
	cin >> name;
	struct people *ptb = head;
	if (head != NULL)
	{
		while (ptb != NULL)
		{
			if (ptb->name == name)
			{
				cout << "联系人姓名(拼音):" << ptb->name << endl;
				cout << "联系人年龄:" << ptb->age << endl;
				cout << "联系人电话:" << ptb->phonenumber << endl;
				cout << "联系人性别(M or W):" << ptb->sex << endl << endl;
				break;
			}
		 ptb = ptb->p;
		}
		if (ptb == NULL)
		{
			cout<<"无此联系人"<<endl;
		}
	}
	else cout << "无此联系人" << endl;
}
//***  5.修改  ***
void changepeople(struct people * head);

//***  6.清空  ***
struct people *deletlist(struct people * head)
{
	delete head;
	head = NULL;
	return head;
}
#pragma once
#ifndef subfunction_h
#define subfunction_h
#include <iostream>
#include <string>
#define max 1000//通讯录最大含量
using namespace std;
//定义结构体类型--链表,用于存放联系人
struct people {
	string name;
	int age;
	string phonenumber;
	char sex;
	struct people *p = NULL;
};
//***  1.添加  ***
struct people * addpeople(struct people *head);

//***  2.显示  ***
void printpeople(struct people *head);

//***  3.删除  ***
struct people * deletpeople(struct people * head);

//***  4.查找  ***
void findpeople(struct people * head);

//***  5.修改  ***
void changepeople(struct people * head);

//***  6.清空  ***
struct people * deletlist(struct people * head);

#endif // !subfunction_h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiZhi_BUAA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值