简易通讯录:单链表实现
结构描述:
| #include <iostream> |
| #include <string> |
| #include <cstdlib> |
| |
| using namespace std; |
| |
| class PeoNode { |
| private: |
| string name; |
| string sex; |
| int age; |
| string tele; |
| string addr; |
| |
| PeoNode * next; |
| |
| |
| friend class Contact; |
| }; |
| |
| class Contact { |
| public: |
| |
| void initContact(); |
| |
| bool isEmpty(); |
| |
| void addPeople(); |
| |
| void printContact(); |
| |
| void deleteByName(string & name); |
| |
| PeoNode * searchByName(string & name); |
| |
| void modifyByName(string & name); |
| |
| PeoNode * buyNode(); |
| |
| void makeEmpty(); |
| private: |
| PeoNode * head; |
| }; |
初始化通讯录
把链表的头指针置空:
| void Contact::init() { |
| head = nullptr; |
| } |
添加联系人
使用头插法添加联系人至链表中:
| void Contact::addPeople() { |
| PeoNode * newNode = buyNode(); |
| newNode->next = head; |
| head = newNode; |
| } |
分配节点
根据用户输入创建一个联系人节点,并返回节点。
| PeoNode * Contact::buyNode() { |
| |
| PeoNode * newNode = new PeoNode(); |
| if (newNode == nullptr) { |
| cout << "New Failed!\n"; |
| exit(-1); |
| } |
| |
| |
| cout << "Please enter name:> "; getline(cin, newNode->name); |
| cout << "Please enter sex:> "; getline(cin, newNode->sex); |
| |
| cout << "Please enter age:> "; cin >> newNode->age; cin.ignore(); |
| cout << "Please enter telephone:> "; getline(cin, newNode->tele); |
| cout << "Please enter address:> "; getline(cin, newNode->addr); |
| |
| newNode->next = nullptr; |
| |
| return newNode; |
| } |
打印通讯录
| void Contact::printContact() { |
| if (isEmpty()) { |
| cout << "List Is Empty!\n"; |
| exit(-1); |
| } |
| |
| |
| cout << "name" << "\t" |
| << "sex" << "\t" |
| << "age" << "\t" |
| << "tele" << "\t" |
| << "addr" << "\n"; |
| PeoNode * cur = head; |
| while (cur != nullptr) { |
| cout << cur->name << "\t" |
| << cur->sex << "\t" |
| << cur->age << "\t" |
| << cur->tele << "\t" |
| << cur->addr << "\n"; |
| cur = cur->next; |
| } |
| } |
查找联系人
根据联系人的名字查找。
- 表空:报错
- 非空:遍历查找,找到第一个名字匹配的,并返回节点指针,若到表尾还没有找到,则联系人不存在
| PeoNode * Contact::searchByName(string & name) { |
| if (isEmpty()) { |
| cout << "List Is Empty!\n"; |
| exit(-1); |
| } |
| |
| PeoNode * cur = head; |
| while (cur != nullptr) { |
| if (cur->name == name) { |
| return cur; |
| } |
| |
| cur = cur->next; |
| } |
| |
| |
| return nullptr; |
| } |
删除联系人
- 根据名字查找
- 找到:删除
- 找不到:报告不存在
| void Contact::deleteByName(string & name) { |
| PeoNode * ret = searchByName(name); |
| if (ret == nullptr) { |
| cout << "People is Not Exist!\n"; |
| exit(-1); |
| } |
| |
| |
| PeoNode * cur = head; |
| while (cur->next != ret) { |
| cur = cur->next; |
| } |
| cur->next = ret->next; |
| |
| |
| delete ret; |
| ret = nullptr; |
| } |
修改联系人信息
- 根据名字查找
- 查找成功:修改
- 查找失败:返回错误
| void Contact::modifyByName(string & name) { |
| PeoNode * ret = searchByName(name); |
| if (ret == nullptr) { |
| cout << "Search Failed!\n"; |
| exit(-1); |
| } |
| |
| |
| cout << "Please enter name:> "; getline(cin, ret->name); |
| cout << "Please enter sex:> "; getline(cin, ret->sex); |
| |
| cout << "Please enter age:> "; cin >> ret->age; cin.ignore(); |
| cout << "Please enter telephone:> "; getline(cin, ret->tele); |
| cout << "Please enter address:> "; getline(cin, ret->addr); |
| } |
销毁
| void Contact::makeEmpty() { |
| if (isEmpty()) { |
| cout << "List Is Empty!\n"; |
| exit(-1); |
| } |
| |
| while (!isEmpty()) { |
| cout << head->name; |
| |
| PeoNode * tmp = head; |
| head = head->next; |
| free(tmp); |
| tmp = nullptr; |
| |
| cout << "has been poped!\n"; |
| } |
| |
| cout << "Make Empty Successfully!\n"; |
| } |
综上
Contact.hpp
| #include <iostream> |
| #include <string> |
| #include <cstdlib> |
| |
| using namespace std; |
| |
| class PeoNode { |
| private: |
| string name; |
| string sex; |
| int age; |
| string tele; |
| string addr; |
| |
| PeoNode * next; |
| |
| |
| friend class Contact; |
| }; |
| |
| class Contact { |
| public: |
| |
| void initContact(); |
| |
| bool isEmpty(); |
| |
| void addPeople(); |
| |
| void printContact(); |
| |
| void deleteByName(string & name); |
| |
| PeoNode * searchByName(string & name); |
| |
| void modifyByName(string & name); |
| |
| PeoNode * buyNode(); |
| |
| void makeEmpty(); |
| private: |
| PeoNode * head; |
| }; |
Contact.cpp
| #include "Contact.hpp" |
| |
| void Contact::initContact() { |
| head = nullptr; |
| } |
| |
| bool Contact::isEmpty() { |
| return head == nullptr; |
| } |
| |
| void Contact::addPeople() { |
| PeoNode * newNode = buyNode(); |
| newNode->next = head; |
| head = newNode; |
| } |
| |
| PeoNode * Contact::buyNode() { |
| |
| PeoNode * newNode = new PeoNode(); |
| if (newNode == nullptr) { |
| cout << "New Failed!\n"; |
| exit(-1); |
| } |
| |
| |
| cout << "Please enter name:> "; getline(cin, newNode->name); |
| cout << "Please enter sex:> "; getline(cin, newNode->sex); |
| |
| cout << "Please enter age:> "; cin >> newNode->age; cin.ignore(); |
| cout << "Please enter telephone:> "; getline(cin, newNode->tele); |
| cout << "Please enter address:> "; getline(cin, newNode->addr); |
| |
| newNode->next = nullptr; |
| |
| return newNode; |
| } |
| |
| void Contact::printContact() { |
| if (isEmpty()) { |
| cout << "List Is Empty!\n"; |
| exit(-1); |
| } |
| |
| |
| cout << "name" << "\t" |
| << "sex" << "\t" |
| << "age" << "\t" |
| << "tele" << "\t" |
| << "addr" << "\n"; |
| PeoNode * cur = head; |
| while (cur != nullptr) { |
| cout << cur->name << "\t" |
| << cur->sex << "\t" |
| << cur->age << "\t" |
| << cur->tele << "\t" |
| << cur->addr << "\n"; |
| cur = cur->next; |
| } |
| } |
| |
| PeoNode * Contact::searchByName(string & name) { |
| if (isEmpty()) { |
| cout << "List Is Empty!\n"; |
| exit(-1); |
| } |
| |
| PeoNode * cur = head; |
| while (cur != nullptr) { |
| if (cur->name == name) { |
| return cur; |
| } |
| |
| cur = cur->next; |
| } |
| |
| |
| return nullptr; |
| } |
| |
| void Contact::deleteByName(string & name) { |
| PeoNode * ret = searchByName(name); |
| if (ret == nullptr) { |
| cout << "People is Not Exist!\n"; |
| exit(-1); |
| } |
| |
| |
| PeoNode * cur = head; |
| while (cur->next != ret) { |
| cur = cur->next; |
| } |
| cur->next = ret->next; |
| |
| |
| delete ret; |
| ret = nullptr; |
| } |
| |
| void Contact::modifyByName(string & name) { |
| PeoNode * ret = searchByName(name); |
| if (ret == nullptr) { |
| cout << "Search Failed!\n"; |
| exit(-1); |
| } |
| |
| |
| cout << "Please enter name:> "; getline(cin, ret->name); |
| cout << "Please enter sex:> "; getline(cin, ret->sex); |
| |
| cout << "Please enter age:> "; cin >> ret->age; cin.ignore(); |
| cout << "Please enter telephone:> "; getline(cin, ret->tele); |
| cout << "Please enter address:> "; getline(cin, ret->addr); |
| } |
| |
| void Contact::makeEmpty() { |
| if (isEmpty()) { |
| cout << "List Is Empty!\n"; |
| exit(-1); |
| } |
| |
| while (!isEmpty()) { |
| cout << head->name; |
| |
| PeoNode * tmp = head; |
| head = head->next; |
| free(tmp); |
| tmp = nullptr; |
| |
| cout << "has been poped!\n"; |
| } |
| |
| cout << "Make Empty Successfully!\n"; |
| } |
踩坑
C++处理字符串输入用 std::getline()
方法
C++中 new
关键字为对象分配空间,并返回该对象的指针。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架