【C++简单项目】用单链表制作通讯录

一、通讯录的基本功能

1.添加联系人

2.显示联系人

3.查找联系人

4.删除联系人

5.修改联系人

6.清除联系人

7.退出系统

二、分代码

1.主体框架

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

struct Contacts
{
	string name;
	string phone;
	string address;
};
struct Node
{
	Contacts person;
	Node* next;
};

void showMenu();
void add(Node*& head);
void show(Node* head);
void search(Node* head);
void del(Node* head);
void revise(Node* head);
void empty(Node* &head);

int main()
{
	Node* head = NULL;
	while (1)
	{
		showMenu();//显示菜单
		int choice;
		cin >> choice;
		switch (choice)
		{
		case 1://添加联系人
			add(head);
			break;
		case 2://显示联系人
			show(head);
			break;
		case 3://查找联系人
			search(head);
			break;
		case 4://删除联系人
			del(head);
			break;
		case 5://修改联系人
			revise(head);
			break;
		case 6://清空联系人
			empty(head);
			break;
		case 7://退出系统
			system("cls");
			cout << "欢迎下次使用!\n";
			return 0;
		}
	}
	return 0;
}

2.显示菜单

void showMenu()
{
	string str1 = "*****";
	string str2 = "**********************";
	cout << str2 << endl;
	cout << str1 << "1.添加联系人" << str1 << endl;
	cout << str1 << "2.显示联系人" << str1 << endl;
	cout << str1 << "3.查找联系人" << str1 << endl;
	cout << str1 << "4.删除联系人" << str1 << endl;
	cout << str1 << "5.修改联系人" << str1 << endl;
	cout << str1 << "6.清除联系人" << str1 << endl;
	cout << str1 << "7.退出系统" << str1 << endl;
	cout << str2 << endl;
}

3.添加联系人(按名字的拼音排序)

用插入法生成有序链表。

void add(Node*& head)
{
	Node* s, * p, * q;

	s = new Node;
	cin >> s->person.name >> s->person.phone >> s->person.address;
	s->next = NULL;

	if (head == NULL)//若链表为空,则先建立头结点
	{
		head = s;
		cout << "已成功添加联系人的信息!\n";
		return;
	}
	if (s->person.name < head->person.name)//被插数据最小,插在链表头
	{
		s->next = head;
		head = s;
		cout << "已成功添加联系人的信息!\n";
		return;
	}
	for (q = head, p = head->next; p ;q = p, p = p->next)
	{//搜索插入,插在链表中间
		if (s->person.name < p->person.name)
		{
			s->next = p;
			q->next = s;
			cout << "已成功添加联系人的信息!\n";
			return;
		}
	}
	q->next = s;//被插数据最大,插在链表尾
	cout << "已成功添加联系人的信息!\n";
	return;
}

4.显示联系人(并记录总人数)

void show(Node* head)
{
	//若列表为空
	if (head == NULL)
	{
		cout << "联系人列表为空!" << endl;
		return;
	}

	//若列表不为空
	Node* p = head;
	int count = 0;
	cout << "姓名" << '\t' << "电话" << '\t' << "住址" << endl;
	while (p)
	{
		cout << p->person.name << '\t' << p->person.phone << '\t' << p->person.address << endl;
		count++;
		p = p->next;
	}
	cout << "联系人总人数为:"<< count << endl;
	return;
}

5.查找联系人

void search(Node* head)
{
	Node* p = head;
	string name;
	cout << "请输入要查找的联系人的姓名:" << endl;
	cin >> name;
	while (p)
	{
		if (p->person.name == name)
		{
			cout << p->person.name << '\t' << p->person.phone << '\t' << p->person.address << endl;  
			return;
		}
		p = p->next;
	}
	cout << "查无此人" << endl;
	return;
}

6.删除联系人

void del(Node* head)
{
	Node* p, * q;
	string name;
	cout << "请输入要删除的联系人的姓名:" << endl;
	cin >> name;

	if (head == NULL)//若链表为空
	{
		cout << "联系人列表为空!error" << endl;
		return;
	}
	if (head->person.name == name)//若被删节点是头结点
	{
		p = head;
		head = head->next;
		delete p;
		p = NULL;
		cout << "联系人" << name << "已被删除" << endl;
		return;
	}
	for (q = head, p = head->next; p ;q = p, p = p->next)
	{//若被删节点不是头结点,则遍历查找并删除节点
		if (p->person.name == name)
		{
			q->next = p->next;
			delete p;
			p = NULL;
			cout << "联系人" << name << "已被删除" << endl;
			return;
		}
	}
	cout << "查无此人!" << endl;//若链表中不存在要删的节点
	return;
}

7.修改联系人

void revise(Node* head)
{
	Node* p = head;

	string name;
	cout << "请输入要修改的联系人的姓名:" << endl;
	cin >> name;

	while (p)
	{
		if (p->person.name == name)
		{
			cin >> p->person.name >> p->person.phone >> p->person.address;
			return;
		}
		p = p->next;
	}//若联系人列表中存在此人
	cout << "查无此人!" << endl;//若联系人列表中不存在此人
	return;
}

8.清除联系人

void empty(Node* &head)
{
	if (head == NULL)//若链表已为空
	{
		cout << "链表已为空!\n";
		return;
	}
	Node* p;
	while (head)//若链表不为空,则不断删除头结点使其被清空
	{
		p = head;
		head = head->next;
		delete p;
		p = NULL;
	}
	cout << "已成功清空链表!\n";
	return;
}

三、优化

1.程序暂停和清空控制台

system("pause");//暂停程序的运行,按下任意键后,又可以继续执行

system("cls");//清空控制台界面

 目的是让运行结果界面更简洁美观。

2.待补充

四、完整代码

已优化且补充了提示语句。

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

struct Contacts
{
	string name;
	string phone;
	string address;
};
struct Node
{
	Contacts person;
	Node* next;
};

void showMenu();
void add(Node*& head);
void show(Node* head);
void search(Node* head);
void del(Node* head);
void revise(Node* head);
void empty(Node* &head);

int main()
{
	Node* head = NULL;
	while (1)
	{
		showMenu();//显示菜单
		int choice;
		cout << "请选择功能序号:\n";
		cin >> choice;
		switch (choice)
		{
		case 1://添加联系人
			add(head);
			break;
		case 2://显示联系人
			show(head);
			break;
		case 3://查找联系人
			search(head);
			break;
		case 4://删除联系人
			del(head);
			break;
		case 5://修改联系人
			revise(head);
			break;
		case 6://清空联系人
			empty(head);
			break;
		case 7://退出系统
			system("cls");
			cout << "欢迎下次使用!\n";
			return 0;
		}
	}
	return 0;
}
void showMenu()
{
	string str1 = "*****";
	string str2 = "**********************";
	cout << str2 << endl;
	cout << str1 << "1.添加联系人" << str1 << endl;
	cout << str1 << "2.显示联系人" << str1 << endl;
	cout << str1 << "3.查找联系人" << str1 << endl;
	cout << str1 << "4.删除联系人" << str1 << endl;
	cout << str1 << "5.修改联系人" << str1 << endl;
	cout << str1 << "6.清除联系人" << str1 << endl;
	cout << str1 << "7.退出系统**" << str1 << endl;
	cout << str2 << endl;
}

void add(Node*& head)
{
	Node* s, * p, * q;

	s = new Node;
	cout << "请输入要添加的联系人的信息:姓名、电话、地址\n";
	cin >> s->person.name >> s->person.phone >> s->person.address;
	s->next = NULL;

	if (head == NULL)
	{
		head = s;
		cout << "已成功添加联系人的信息!\n";
		system("pause");
		system("cls");
		return;
	}
	if (s->person.name < head->person.name)
	{
		s->next = head;
		head = s;
		cout << "已成功添加联系人的信息!\n";
		system("pause");
		system("cls");
		return;
	}
	for (q = head, p = head->next; p ;q = p, p = p->next)
	{
		if (s->person.name < p->person.name)
		{
			s->next = p;
			q->next = s;
			cout << "已成功添加联系人的信息!\n";
			system("pause");
			system("cls");
			return;
		}
	}
	q->next = s;
	cout << "已成功添加联系人的信息!\n";
	system("pause");
	system("cls");
	return;
}

void show(Node* head)
{
	//若列表为空
	if (head == NULL)
	{
		cout << "联系人列表为空!" << endl;
		system("pause");
		system("cls");
		return;
	}

	//若列表不为空
	Node* p = head;
	int count = 0;
	cout << "姓名" << '\t' << "电话" << '\t' << "住址" << endl;
	while (p)
	{
		cout << p->person.name << '\t' << p->person.phone << '\t' << p->person.address << endl;
		count++;
		p = p->next;
	}
	cout << "联系人总人数为:"<< count << endl;
	system("pause");
	system("cls");
	return;
}

void search(Node* head)
{
	Node* p = head;
	string name;
	cout << "请输入要查找的联系人的姓名:" << endl;
	cin >> name;
	while (p)
	{
		if (p->person.name == name)
		{
			cout << p->person.name << '\t' << p->person.phone << '\t' << p->person.address << endl;  
			system("pause");
			system("cls");
			return;
		}
		p = p->next;
	}
	cout << "查无此人" << endl;
	system("pause");
	system("cls");
	return;
}

void del(Node* head)
{
	Node* p, * q;
	string name;
	cout << "请输入要删除的联系人的姓名:" << endl;
	cin >> name;

	if (head == NULL)
	{
		cout << "联系人列表为空!error" << endl;
		system("pause");
		system("cls");
		return;
	}
	if (head->person.name == name)
	{
		p = head;
		head = head->next;
		delete p;
		p = NULL;
		cout << "联系人" << name << "已被删除" << endl;
		system("pause");
		system("cls");
		return;
	}
	for (q = head, p = head->next; p ;q = p, p = p->next)
	{
		if (p->person.name == name)
		{
			q->next = p->next;
			delete p;
			p = NULL;
			cout << "联系人" << name << "已被删除" << endl;
			system("pause");
			system("cls");
			return;
		}
	}
	cout << "查无此人!" << endl;
	system("pause");
	system("cls");
	return;
}

void revise(Node* head)
{
	Node* p = head;

	string name;
	cout << "请输入要修改的联系人的姓名:" << endl;
	cin >> name;

	while (p)
	{
		if (p->person.name == name)
		{
			cout << "请输入要修改的联系人的信息:姓名、电话、地址\n";
			cin >> p->person.name >> p->person.phone >> p->person.address;
			cout << "已成功修改!\n";
			system("pause");
			system("cls");
			return;
		}
		p = p->next;
	}
	cout << "查无此人!" << endl;
	system("pause");
	system("cls");
	return;
}

void empty(Node* &head)
{
	if (head == NULL)
	{
		cout << "链表已为空!\n";
		system("pause");
		system("cls");
		return;
	}
	Node* p;
	while (head)//不断删除头结点
	{
		p = head;
		head = head->next;
		delete p;
		p = NULL;
	}
	cout << "已成功清空链表!\n";
	system("pause");
	system("cls");
	return;
}

  • 18
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,我可以为您提供一个基本的单链表通讯录的实现。首先,我们需要定义一个通讯录的节点结构体,包含姓名、电话号码、邮箱等信息: ```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"); ``` 以上是一个基本的单链表通讯录实现,您可以根据需要进行扩展和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值