VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1032|回复: 0

[交流] 萌新参考别人的通讯录自己写的单链表存储结构的通讯录,大佬给给意见

[复制链接]
45_avatar_middle
最佳答案
0 
在线会员 发表于 2022-5-20 19:19:28 | 显示全部楼层 |阅读模式
  1. #include<iostream>
  2. #include<string>
  3. using namespace std ;


  4. struct people
  5. {
  6.         string t_name;
  7.         int t_age;
  8.         int t_sex;
  9.         string t_phone;
  10.         string t_addr;
  11.         people*p_next;
  12. };

  13. people* initNode()//初始化节点
  14. {
  15.         people*head = new people();
  16.         head->p_next = NULL;
  17.         return head;
  18. }

  19. void Free_Node(people*head)//释放内存
  20. {
  21.         people* p;
  22.         people*temp;
  23.         p = head;
  24.         while (p != NULL)
  25.         {
  26.                 temp = p->p_next;
  27.                 delete p;
  28.                 p = temp;
  29.         }
  30. }
  31. //添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)
  32. void Add_people(people*head)
  33. {
  34.        
  35.         people*temp=new people();
  36.         //姓名
  37.         string name;
  38.         cout << "请输入姓名" << endl;
  39.         cin >>name;
  40.         temp->t_name = name;
  41.         //性别
  42.         int sex;
  43.         cout << "请输入性别" << endl;
  44.         cout << "1--男" << endl;
  45.         cout << "2--女" << endl;
  46.         while (true)
  47.         {
  48.                 cin >> sex;
  49.                 if (sex == 1 || sex == 2)
  50.                 {
  51.                         temp->t_sex = sex;
  52.                         break;
  53.                 }
  54.                 else
  55.                         cout << "请重新输入:" << endl;
  56.         }
  57.         //年龄
  58.         int age;
  59.         cout << "请输入年龄" << endl;
  60.         cin >> age;
  61.         temp->t_age = age;
  62.         //联系电话
  63.         string phone;
  64.         cout << "请输入联系电话" << endl;
  65.         cin >> phone;
  66.         temp->t_phone = phone;
  67.         //家庭地址
  68.         string addr;
  69.         cout << "请输入家庭地址" << endl;
  70.         cin >> addr;
  71.         temp->t_addr = addr;
  72.         //尾插法插入节点
  73.         people*p;
  74.         p = head;
  75.         while (p->p_next != NULL)
  76.         {
  77.                 p = p->p_next;
  78.         }
  79.         p->p_next = temp;
  80.         temp->p_next = NULL;
  81. }
  82. //显示联系人:显示通讯录中所有联系人信息
  83. void Show_All_people(people*head)
  84. {
  85.         people*p = head->p_next;
  86.         if (p == NULL)
  87.         {
  88.                 cout << "通讯录为空!" << endl;
  89.         }
  90.         else
  91.         {
  92.                 while (p!= NULL)
  93.                 {
  94.                         cout << "姓名:" << p->t_name << '\t';
  95.                         cout << "性别:" << (p->t_sex == 1 ? "男" : "女") << '\t';
  96.                         cout << "年龄:" << p->t_age << '\t';
  97.                         cout << "电话:" << p->t_phone << '\t';
  98.                         cout << "地址:" << p->t_addr << endl;
  99.                         p = p->p_next;
  100.                 }
  101.         }
  102. }
  103. //查找联系人是否存在
  104. people* isExit(people*head,string name)
  105. {
  106.         people*p = head->p_next;
  107.         while (p!= NULL)
  108.         {
  109.                 if (p->t_name == name)
  110.                         return p;
  111.                 p = p->p_next;
  112.         }

  113.         return NULL;
  114. }
  115. //删除联系人:按照姓名进行删除指定联系人
  116. void Delete_people(people*head)
  117. {
  118.         string name;
  119.         cout << "输入要删除的指定联系人" << endl;
  120.         cin >> name;
  121.         people*p = isExit(head, name);
  122.         if (p == NULL)
  123.                 cout << "查无此人" << endl;
  124.         else
  125.         {
  126.                 people*temp;
  127.                 temp = head->p_next;
  128.                 while (temp!=NULL)
  129.                 {
  130.                         temp = temp->p_next;
  131.                         if (temp->p_next == p)
  132.                                 break;
  133.                 }
  134.                 temp->p_next = p->p_next;
  135.         }

  136. }
  137. //查找联系人:按照姓名查看指定联系人信息
  138. void Find_People(people*head)
  139. {
  140.         string name;
  141.         cout << "输入要删除的指定联系人" << endl;
  142.         cin >> name;
  143.         people*p = isExit(head, name);
  144.         if (p == NULL)
  145.         {
  146.                 cout << "没有此人" << endl;
  147.         }
  148.         else
  149.         {
  150.                 cout << "姓名:" << p->t_name << '\t';
  151.                 cout << "性别:" << (p->t_sex == 1 ? "男" : "女") << '\t';
  152.                 cout << "年龄:" << p->t_age << '\t';
  153.                 cout << "电话:" << p->t_phone << '\t';
  154.                 cout << "地址:" << p->t_addr << endl;
  155.         }
  156. }
  157. //修改联系人:按照姓名重新修改指定联系人
  158. void Modi_People(people*head)
  159. {
  160.         string name;
  161.         cout << "输入要修改的指定联系人" << endl;
  162.         cin >> name;
  163.         people*p = isExit(head, name);
  164.         if (p == NULL)
  165.         {
  166.                 cout << "没有此人" << endl;
  167.         }
  168.         else
  169.         {

  170.                 //姓名
  171.                 string name;
  172.                 cout << "请输入姓名" << endl;
  173.                 cin >> name;
  174.                 p->t_name = name;
  175.                 //性别
  176.                 int sex;
  177.                 cout << "请输入性别" << endl;
  178.                 cout << "1--男" << endl;
  179.                 cout << "2--女" << endl;
  180.                 while (true)
  181.                 {
  182.                         cin >> sex;
  183.                         if (sex == 1 || sex == 2)
  184.                         {
  185.                                 p->t_sex = sex;
  186.                                 break;
  187.                         }
  188.                         else
  189.                                 cout << "请重新输入:" << endl;
  190.                 }
  191.                 //年龄
  192.                 int age;
  193.                 cout << "请输入年龄" << endl;
  194.                 cin >> age;
  195.                 p->t_age = age;
  196.                 //联系电话
  197.                 string phone;
  198.                 cout << "请输入联系电话" << endl;
  199.                 cin >> phone;
  200.                 p->t_phone = phone;
  201.                 //家庭地址
  202.                 string addr;
  203.                 cout << "请输入家庭地址" << endl;
  204.                 cin >> addr;
  205.                 p->t_addr = addr;
  206.         }
  207. }
  208. //清空联系人:清空通讯录中所有信息
  209. void clear_people(people*head)
  210. {
  211.         head->p_next = NULL;
  212. }

  213. void Show_Menu()
  214. {
  215.         cout << "**************************" << endl;
  216.         cout << "*****  1.添加联系人  *****" << endl;
  217.         cout << "*****  2.显示联系人  *****" << endl;
  218.         cout << "*****  3.删除联系人  *****" << endl;
  219.         cout << "*****  4.查找联系人  *****" << endl;
  220.         cout << "*****  5.修改联系人  *****" << endl;
  221.         cout << "*****  6.清空联系人  *****" << endl;
  222.         cout << "*****  0.退出通讯录  *****" << endl;
  223.         cout << "**************************" << endl;
  224. }


  225. int main()
  226. {
  227.         people*head;
  228.         head = initNode();
  229.         int select;
  230.         while (true)
  231.         {
  232.                 Show_Menu();
  233.                 cin >> select;
  234.                 switch (select)
  235.                 {
  236.                 case 1:
  237.                         Add_people(head);
  238.                         break;
  239.                 case 2:
  240.                         Show_All_people(head);
  241.                         break;
  242.                 case 3:
  243.                         Delete_people(head);
  244.                         break;
  245.                 case 4:
  246.                         Find_People(head);
  247.                         break;
  248.                 case 5:
  249.                         Modi_People(head);
  250.                         break;
  251.                 case 6:
  252.                         clear_people(head);
  253.                         break;
  254.                 case 0:
  255.                         Free_Node(head);
  256.                         cout << "谢谢使用" << endl;
  257.                         return 0;
  258.                 default:
  259.                         break;
  260.                 }
  261.         }
  262. }
复制代码




上一篇:使用IOCP框架制作端口转发映射方案征集
下一篇:关于C++ Primer第五版P289练习8.10
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-5-29 23:19

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表