鱼C论坛

 找回密码
 立即注册
查看: 4667|回复: 8

[知识点备忘] S1E48:内存池

[复制链接]
发表于 2017-4-19 02:50:22 | 显示全部楼层 |阅读模式
购买主题 已有 2 人购买  本主题需向作者支付 5 鱼币 才能浏览
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-4 11:55:23 | 显示全部楼层
通讯录程序(未加入内存池),看视频纯手打
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct Person
  5. {
  6.         char name[40];
  7.         char phone[20];
  8.         struct Person *next;
  9. };

  10. void getInput(struct Person *person);
  11. void printPerson(struct Person *person);
  12. void addPerson(struct Person **contacts);
  13. void changePerson(struct Person *contacts);
  14. void delPerson(struct Person **contacts);
  15. struct Person *findPerson(struct Person *contacts);
  16. void displayContacts(struct Person *contacts);
  17. void releaseContacts(struct Person **contacts);

  18. void getInput(struct Person *person)
  19. {
  20.         printf("请输入姓名:");
  21.         scanf("%s", person->name);
  22.         printf("请输入电话号码:");
  23.         scanf("%s", person->phone);
  24. }

  25. void addPerson(struct Person **contacts)
  26. {
  27.         struct Person *person;
  28.         struct Person *temp;
  29.        
  30.         person = (struct Person *)malloc(sizeof(struct Person));
  31.         if (person == NULL)
  32.         {
  33.                 printf("内存分配失败!\n");
  34.                 exit(1);
  35.         }
  36.        
  37.         getInput(person);
  38.        
  39.         // 将person用头插法添加到通讯录中
  40.         if (*contacts != NULL)
  41.         {
  42.                 temp = *contacts;
  43.                 *contacts = person;
  44.                 person->next = temp;
  45.         }
  46.         else
  47.         {
  48.                 *contacts = person;
  49.                 person->next = NULL;
  50.         }
  51. }

  52. void printPerson(struct Person *person)
  53. {
  54.         printf("联系人:%s\n", person->name);
  55.         printf("电话:%s\n", person->phone);
  56. }

  57. struct Person *findPerson(struct Person *contacts)
  58. {
  59.         struct Person *current;
  60.         char input[40];
  61.        
  62.         printf("请输入联系人:");
  63.         scanf("%s", input);
  64.        
  65.         current = contacts;
  66.         while (current != NULL && strcmp(current->name, input))
  67.         {
  68.                 current = current->next;
  69.         }
  70.        
  71.         return current;
  72. }

  73. void changePerson(struct Person *contacts)
  74. {
  75.         struct Person *person;
  76.        
  77.         person = findPerson(contacts);
  78.         if (person == NULL)
  79.         {
  80.                 printf("找不到该联系人!\n");
  81.         }
  82.         else
  83.         {
  84.                 printf("请输入新的联系电话:");
  85.                 scanf("%s", person->phone);
  86.         }
  87. }

  88. void delPerson(struct Person **contacts)
  89. {
  90. //        struct Person *temp;
  91.         struct Person *person;
  92.         struct Person *current;
  93.         struct Person *previous;
  94.        
  95.         // 先找到待删除的节点指针
  96.         person = findPerson(*contacts);
  97.         if (person == NULL)
  98.         {
  99.                 printf("找不到该联系人!\n");
  100.         }
  101.         else
  102.         {
  103.                 current = *contacts;
  104.                 previous = NULL;
  105.                
  106.                 // current定位到待删除的节点
  107.                 while (current != NULL && current != person)
  108.                 {
  109.                         previous = current;
  110.                         current = current->next;
  111.                 }
  112.                
  113.                 if (previous == NULL)
  114.                 {
  115.                         // 待删除的节点是第一个节点
  116.                         *contacts = current->next;
  117.                 }
  118.                 else
  119.                 {
  120.                         // 待删除的节点不是第一个节点
  121.                         previous->next = current->next;
  122.                 }
  123.                
  124.                 free(person);
  125.         }
  126. }

  127. void displayContacts(struct Person *contacts)
  128. {
  129.         struct Person *current;
  130.        
  131.         current = contacts;
  132.         while (current != NULL)
  133.         {
  134.                 printPerson(current);
  135.                 current = current->next;
  136.         }
  137. }

  138. void releaseContacts(struct Person **contacts)
  139. {
  140.         struct Person *temp;
  141.        
  142.         while (*contacts != NULL)
  143.         {
  144.                 temp = *contacts;
  145.                 *contacts = (*contacts)->next;
  146.                 free(temp);
  147.         }
  148. }

  149. int main(void)
  150. {
  151.         int code;
  152.         struct Person *contacts = NULL;
  153.         struct Person *person;
  154.        
  155.         printf("| 欢迎使用通讯录管理程序 |\n");
  156.         printf("|--- 1:插入新的联系人 ---|\n");
  157.         printf("|--- 2:查找已有联系人 ---|\n");
  158.         printf("|--- 3:更改已有联系人 ---|\n");
  159.         printf("|--- 4:删除已有联系人 ---|\n");
  160.         printf("|--- 5:显示当前通讯录 ---|\n");
  161.         printf("|--- 6:退出通讯录程序 ---|\n");
  162.         printf("|- Powered by FishC.com -|\n");
  163.        
  164.         while (1)
  165.         {
  166.                 printf("\n请输入指令代码:");
  167.                 scanf("%d", &code);
  168.                 switch (code)
  169.                 {
  170.                         case 1: addPerson(&contacts); break;
  171.                        
  172.                         case 2: person = findPerson(contacts);
  173.                                         if (person == NULL)
  174.                                         {
  175.                                                 printf("找不到联系人!\n");
  176.                                         }
  177.                                         else
  178.                                         {
  179.                                                 printPerson(person);
  180.                                         }
  181.                                         break;
  182.                        
  183.                         case 3: changePerson(contacts); break;
  184.                        
  185.                         case 4: delPerson(&contacts); break;
  186.                        
  187.                         case 5: displayContacts(contacts); break;
  188.                        
  189.                         case 6: goto END;
  190.                 }
  191.         }
  192.        
  193. END:
  194.         releaseContacts(&contacts);
  195.        
  196.         return 0;
  197. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 6 反对 1

使用道具 举报

发表于 2020-3-4 11:56:20 | 显示全部楼层
通讯录程序(加入内存池),看视频纯手打
  1. // 加入内存池的版本
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. #define MAX 1024        //定义内存池最大空间

  6. struct Person
  7. {
  8.         char name[40];
  9.         char phone[20];
  10.         struct Person *next;
  11. };

  12. struct Person *pool = NULL;
  13. int count;

  14. void getInput(struct Person *person);
  15. void printPerson(struct Person *person);
  16. void addPerson(struct Person **contacts);
  17. void changePerson(struct Person *contacts);
  18. void delPerson(struct Person **contacts);
  19. struct Person *findPerson(struct Person *contacts);
  20. void displayContacts(struct Person *contacts);
  21. void releaseContacts(struct Person **contacts);
  22. void releasePool(void);

  23. void getInput(struct Person *person)
  24. {
  25.         printf("请输入姓名:");
  26.         scanf("%s", person->name);
  27.         printf("请输入电话号码:");
  28.         scanf("%s", person->phone);
  29. }

  30. void addPerson(struct Person **contacts)
  31. {
  32.         struct Person *person;
  33.         struct Person *temp;
  34.        
  35.         // 如果内存池非空,则直接从里面获取空间
  36.         if (pool != NULL)
  37.         {
  38.                 person = pool;
  39.                 pool = pool->next;
  40.                 count--;
  41.         }
  42.         // 如果内存池为空,则调用malloc函数申请新的内存空间
  43.         else
  44.         {
  45.                 person = (struct Person *)malloc(sizeof(struct Person));
  46.                 if (person == NULL)
  47.                 {
  48.                         printf("内存分配失败!\n");
  49.                         exit(1);
  50.                 }
  51.         }

  52.         getInput(person);
  53.        
  54.         // 将person用头插法添加到通讯录中
  55.         if (*contacts != NULL)
  56.         {
  57.                 temp = *contacts;
  58.                 *contacts = person;
  59.                 person->next = temp;
  60.         }
  61.         else
  62.         {
  63.                 *contacts = person;
  64.                 person->next = NULL;
  65.         }
  66. }

  67. void printPerson(struct Person *person)
  68. {
  69.         printf("联系人:%s\n", person->name);
  70.         printf("电话:%s\n", person->phone);
  71. }

  72. struct Person *findPerson(struct Person *contacts)
  73. {
  74.         struct Person *current;
  75.         char input[40];
  76.        
  77.         printf("请输入联系人:");
  78.         scanf("%s", input);
  79.        
  80.         current = contacts;
  81.         while (current != NULL && strcmp(current->name, input))
  82.         {
  83.                 current = current->next;
  84.         }
  85.        
  86.         return current;
  87. }

  88. void changePerson(struct Person *contacts)
  89. {
  90.         struct Person *person;
  91.        
  92.         person = findPerson(contacts);
  93.         if (person == NULL)
  94.         {
  95.                 printf("找不到该联系人!\n");
  96.         }
  97.         else
  98.         {
  99.                 printf("请输入新的联系电话:");
  100.                 scanf("%s", person->phone);
  101.         }
  102. }

  103. void delPerson(struct Person **contacts)
  104. {
  105.         struct Person *temp;
  106.         struct Person *person;
  107.         struct Person *current;
  108.         struct Person *previous;
  109.        
  110.         // 先找到待删除的节点指针
  111.         person = findPerson(*contacts);
  112.         if (person == NULL)
  113.         {
  114.                 printf("找不到该联系人!\n");
  115.         }
  116.         else
  117.         {
  118.                 current = *contacts;
  119.                 previous = NULL;
  120.                
  121.                 // current定位到待删除的节点
  122.                 while (current != NULL && current != person)
  123.                 {
  124.                         previous = current;
  125.                         current = current->next;
  126.                 }
  127.                
  128.                 if (previous == NULL)
  129.                 {
  130.                         // 待删除的节点是第一个节点
  131.                         *contacts = current->next;
  132.                 }
  133.                 else
  134.                 {
  135.                         // 待删除的节点不是第一个节点
  136.                         previous->next = current->next;
  137.                 }
  138.                
  139.                 // 判断内存池是不是有空位
  140.                 if (count < MAX)
  141.                 {
  142.                         if (pool != NULL)
  143.                         {
  144.                                 temp = pool;
  145.                                 pool = person;
  146.                                 person->next = temp;
  147.                         }
  148.                         else
  149.                         {
  150.                                 pool = person;
  151.                                 person->next = NULL;
  152.                         }
  153.                         count++;
  154.                 }
  155.                 else
  156.                 {
  157.                         free(person);
  158.                 }
  159.                
  160.         }
  161. }

  162. void displayContacts(struct Person *contacts)
  163. {
  164.         struct Person *current;
  165.        
  166.         current = contacts;
  167.         while (current != NULL)
  168.         {
  169.                 printPerson(current);
  170.                 current = current->next;
  171.         }
  172. }

  173. void releaseContacts(struct Person **contacts)
  174. {
  175.         struct Person *temp;
  176.        
  177.         while (*contacts != NULL)
  178.         {
  179.                 temp = *contacts;
  180.                 *contacts = (*contacts)->next;
  181.                 free(temp);
  182.         }
  183. }

  184. void releasePool(void)
  185. {
  186.         struct Person *temp;
  187.        
  188.         while (pool != NULL)
  189.         {
  190.                 temp = pool;
  191.                 pool = pool->next;
  192.                 free(temp);
  193.         }
  194. }

  195. int main(void)
  196. {
  197.         int code;
  198.         struct Person *contacts = NULL;
  199.         struct Person *person;
  200.        
  201.         printf("| 欢迎使用通讯录管理程序 |\n");
  202.         printf("|--- 1:插入新的联系人 ---|\n");
  203.         printf("|--- 2:查找已有联系人 ---|\n");
  204.         printf("|--- 3:更改已有联系人 ---|\n");
  205.         printf("|--- 4:删除已有联系人 ---|\n");
  206.         printf("|--- 5:显示当前通讯录 ---|\n");
  207.         printf("|--- 6:退出通讯录程序 ---|\n");
  208.         printf("|- Powered by FishC.com -|\n");
  209.        
  210.         while (1)
  211.         {
  212.                 printf("\n请输入指令代码:");
  213.                 scanf("%d", &code);
  214.                 switch (code)
  215.                 {
  216.                         case 1: addPerson(&contacts); break;
  217.                        
  218.                         case 2: person = findPerson(contacts);
  219.                                         if (person == NULL)
  220.                                         {
  221.                                                 printf("找不到联系人!\n");
  222.                                         }
  223.                                         else
  224.                                         {
  225.                                                 printPerson(person);
  226.                                         }
  227.                                         break;
  228.                        
  229.                         case 3: changePerson(contacts); break;
  230.                        
  231.                         case 4: delPerson(&contacts); break;
  232.                        
  233.                         case 5: displayContacts(contacts); break;
  234.                        
  235.                         case 6: goto END;
  236.                 }
  237.         }
  238.        
  239. END:
  240.         releaseContacts(&contacts);
  241.         releasePool();
  242.         return 0;
  243. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 4 反对 0

使用道具 举报

发表于 2020-3-7 21:58:21 | 显示全部楼层
千万不要再while()后面打上;
不要问我为什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 13:04:41 | 显示全部楼层
漂亮
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-7 16:47:32 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct Person   //单链表节点
  5. {
  6.         char name[128];
  7.         char phonenum[128];
  8.         struct Person *next;
  9. };

  10. int sum = 0;     //联系人总数
  11. void star(); //程序开场白
  12. void addPerson(struct Person **head);  //1.头插法插入联系人
  13. void add1Person(struct Person **head);  //1.尾插法插入联系人,头、尾插法在main函数中更换即可
  14. void getPerson(struct Person *person);  //1.1录入联系人
  15. int displayContacts(struct Person *head,int count);   //5.显示当前通讯录
  16. void release(struct Person *head);   //6.退出通讯录程序
  17. void findPerson(struct Person *head);  //2.查找已有联系人
  18. void changePerson(struct Person *head);  //3.更改已有联系人
  19. void delPerson(struct Person **head);   //4.删除已有联系人

  20. int main(void)
  21. {
  22.         struct Person *head = NULL;  // 单3链表 头指针
  23.         int num;

  24.         star();  
  25.         
  26.         do
  27.         {
  28.                 do
  29.                 {
  30.                         num = 0;
  31.                         printf("\n*请输入功能选项:");
  32.                         scanf("%d",&num);
  33.                         fflush(stdin);
  34.                         if(num >= 1 && num <= 6)
  35.                         {
  36.                                 break;
  37.                         }
  38.                         printf("无效指令,请重新输入!\n");
  39.                 }while(1);
  40.                 switch(num)
  41.                 {
  42.                         case 1: add1Person(&head);break;
  43.                         case 2: findPerson(head);break;
  44.                         case 3: changePerson(head);break;
  45.                         case 4: delPerson(&head);break;
  46.                         case 5: displayContacts(head,0);break;
  47.                         case 6: release(head);break;
  48.                 }
  49.         }while(num != 6);

  50.         return 0;
  51. }

  52. void delPerson(struct Person **head)  //4.删除已有联系人
  53. {
  54.         struct Person *now;
  55.         struct Person *before = NULL;
  56.         int num,count = 1,num1;

  57.         now = *head;

  58.         num1 = displayContacts(*head,4);

  59.         if(num1 != 1)
  60.         {
  61.                 do
  62.                 {
  63.                         printf("请输入要删除的联系人序号(1~%d):",sum);
  64.                         scanf("%d",&num);
  65.                         fflush(stdin);
  66.                         if(num <= sum && num >= 1)
  67.                         {
  68.                                 break;
  69.                         }
  70.                         printf("序号输出错误,请重新输入!\n");
  71.                 }while(1);

  72.                 while(count < num && now != NULL)
  73.                 {
  74.                         before = now;
  75.                         now = now->next;
  76.             count++;
  77.                 }

  78.                 if(before == NULL)
  79.                 {
  80.                         *head  = now->next;
  81.                 }
  82.                 else
  83.                 {
  84.                         before->next = now->next;
  85.                 }
  86.                 free(now);
  87.                 sum--;

  88.                 printf("\n*联系人信息删除完成!\n");
  89.         }

  90. }

  91. void changePerson(struct Person *head)  //3.更改已有联系人
  92. {
  93.         struct Person *person;
  94.         int num1 = 0,num,count = 1;
  95.     char ch;   //判读是否更改姓名、电话

  96.         person = head;
  97.         num1 = displayContacts(head,3);

  98.         if(num1 != 1)
  99.         {
  100.                 do
  101.                 {
  102.                         printf("请输入要更改的联系人序号(1~%d):",sum);
  103.                         scanf("%d",&num);
  104.                         fflush(stdin);
  105.                         if(num <= sum && num >= 1)
  106.                         {
  107.                                 break;
  108.                         }
  109.                         printf("序号输出错误,请重新输入!\n");
  110.                 }while(1);

  111.                 while(person != NULL && count < num)
  112.                 {
  113.                         person = person->next;
  114.                         count++;
  115.                 }

  116.                 printf("\n*更改第%d位联系人信息:\n",num);
  117.                 printf("是否更改姓名“%s ”(Y/N)?",person->name);
  118.         scanf("%c",&ch);
  119.         fflush(stdin);
  120.         if(ch == 'Y')
  121.         {
  122.             printf("更改为:");
  123.                         scanf("%s",person->name);
  124.                         fflush(stdin);
  125.                 }
  126.                 printf("是否更改电话“%s ”(Y/N)?",person->phonenum);
  127.         scanf("%c",&ch);
  128.         fflush(stdin);
  129.         if(ch == 'Y')
  130.         {
  131.             printf("更改为:");
  132.                         scanf("%s",person->phonenum);
  133.                         fflush(stdin);
  134.                 }
  135.                 printf("\n*联系人信息更改完成!\n");
  136.         }
  137.         else
  138.         {
  139.                 return;
  140.         }
  141. }

  142. void findPerson(struct Person *head)   //2.查找已有联系人
  143. {
  144.         char find[128];
  145.         int num = 0;
  146.         struct Person *person;

  147.         printf("请输入查找信息(姓名/电话):");
  148.         scanf("%s",find);
  149.         getchar();

  150.         person = head;
  151.         while(person != NULL)
  152.         {
  153.                 if(!strcmp(person->name,find) || !strcmp(person->phonenum,find))
  154.                 {
  155.                     if(num == 0)
  156.                         {
  157.                                 printf("\n-------已查找到以下联系信息-------\n");
  158.                         }
  159.                         printf("\n第%d位联系人:\n",num + 1);
  160.                         printf("姓名:%s\n",person->name);
  161.                         printf("电话:%s\n",person->phonenum);
  162.                           num++;
  163.                 }
  164.                 person = person->next;
  165.         }
  166.         if(num == 0)
  167.         {
  168.                 printf("\n--------未查到相关联系信息--------\n");
  169.         }
  170.         printf("\n----------------------------------\n");

  171. }
  172. void release(struct Person *head)  //6.退出通讯录程序
  173. {
  174.         printf("\n*此通讯录小程序已退出!\n");
  175.         free(head);
  176.         exit(1);
  177. }

  178. int displayContacts(struct Person *head,int count)  //5.显示当前通讯录
  179. {
  180.     int num = 1;
  181.         struct Person *person;

  182.         person = head;

  183.     printf("------------通讯录如下------------\n");
  184.         while(person != NULL)
  185.         {
  186.                 printf("\n第%d位联系人信息如下:\n",num);
  187.                 printf("姓名:%s\n",person->name);
  188.                 printf("电话:%s\n",person->phonenum);
  189.                 num++;
  190.                 person = person->next;
  191.         }
  192.         if(num == 1)
  193.         {
  194.                 if(count == 0)
  195.                 {
  196.                         printf("\n当前通讯录暂无联系人,请添加后再显示!\n");
  197.                 }
  198.                 else if(count == 3)
  199.                 {
  200.                         printf("\n当前通讯录暂无联系人,请添加后再进行更改!\n");
  201.                 }
  202.                 else if(count == 4)
  203.                 {
  204.                         printf("\n当前通讯录暂无联系人,请添加后再进行删除!\n");
  205.                 }
  206.             printf("\n----------------------------------\n");
  207.                 return 1;
  208.         }
  209.         printf("\n----------------------------------\n");
  210.         num = 1;
  211. }

  212. void getPerson(struct Person *person)  //1.1录入联系人
  213. {
  214.         printf("\n请输入联系人姓名:");
  215.         scanf("%s",person->name);
  216.         getchar();
  217.         printf("请输入联系人电话:");
  218.         scanf("%s",person->phonenum);
  219.         getchar();
  220. }

  221. void addPerson(struct Person **head)  //1.头插法插入联系人
  222. {
  223.         struct Person *person;
  224.         struct Person *temp;

  225.         person = (struct Person *)malloc(sizeof(struct Person));
  226.         if(person == NULL)
  227.         {
  228.                 printf("内存分配失败!\n");
  229.                 exit(1);
  230.         }

  231.         getPerson(person);  //录入联系人

  232.         if(*head == NULL)
  233.         {
  234.                 *head = person;
  235.                 person->next = NULL;
  236.         }
  237.         else
  238.         {
  239.                 temp = *head;
  240.                 *head = person;
  241.                 person->next = temp;
  242.         }
  243.         printf("第%d位联系人已录入完成!\n",++sum);
  244. }

  245. void add1Person(struct Person **head)  //1.尾插法插入联系人
  246. {
  247.         struct Person *person;
  248.         static struct Person *tail;

  249.         person = (struct Person *)malloc(sizeof(struct Person));
  250.         if(person == NULL)
  251.         {
  252.                 printf("内存分配失败!\n");
  253.                 exit(1);
  254.         }

  255.         getPerson(person);  //录入联系人

  256.         if(*head == NULL)
  257.         {
  258.                 *head = person;
  259.                 person->next = NULL;
  260.         }
  261.         else
  262.         {
  263.                 tail->next = person;
  264.                 person->next = NULL;
  265.         }
  266.     tail = person;
  267.         printf("第%d位联系人已录入完成!\n",++sum);
  268. }

  269. void star()
  270. {
  271.         printf("********************************\n");
  272.         printf("**     夏日de通讯录小程序     **\n");
  273.         printf("**  功能选项:                **\n");
  274.         printf("**  1.添加新的联系人          **\n");
  275.         printf("**  2.查找已有联系人          **\n");
  276.         printf("**  3.更改已有联系人          **\n");
  277.         printf("**  4.删除已有联系人          **\n");
  278.         printf("**  5.显示当前通讯录          **\n");
  279.         printf("**  6.退出通讯录程序          **\n");
  280.         printf("**                            **\n");
  281.         printf("********************************\n");
  282. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-7 21:26:28 | 显示全部楼层
有内存池版本,并对上一版本releasePerson函数中做了修正~
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>  //有内存池版本

  4. #define MAX 1024

  5. struct Person   //单链表节点
  6. {
  7.         char name[128];
  8.         char phonenum[128];
  9.         struct Person *next;
  10. };

  11. struct Person *pool = NULL;  //定义内存池

  12. int sum = 0;     //联系人总数
  13. void star(); //程序开场白
  14. void addPerson(struct Person **head);  //1.头插法插入联系人
  15. void add1Person(struct Person **head);  //1.尾插法插入联系人,头、尾插法在main函数中更换即可
  16. void getPerson(struct Person *person);  //1.1录入联系人
  17. int displayContacts(struct Person *head,int count);   //5.显示当前通讯录
  18. void release(struct Person **head);   //6.退出通讯录程序
  19. void findPerson(struct Person *head);  //2.查找已有联系人
  20. void changePerson(struct Person *head);  //3.更改已有联系人
  21. void delPerson(struct Person **head);   //4.删除已有联系人
  22. void releasepool(void);  //释放内存池

  23. int main(void)
  24. {
  25.         struct Person *head = NULL;  // 单链表 头指针
  26.         int num;

  27.         star();

  28.         do
  29.         {
  30.                 do
  31.                 {
  32.                         num = 0;
  33.                         printf("\n*请输入功能选项:");
  34.                         scanf("%d",&num);
  35.                         fflush(stdin);
  36.                         if(num >= 1 && num <= 6)
  37.                         {
  38.                                 break;
  39.                         }
  40.                         printf("无效指令,请重新输入!\n");
  41.                 }while(1);
  42.                 switch(num)
  43.                 {
  44.                         case 1: add1Person(&head);break;
  45.                         case 2: findPerson(head);break;
  46.                         case 3: changePerson(head);break;
  47.                         case 4: delPerson(&head);break;
  48.                         case 5: displayContacts(head,0);break;
  49.                         case 6: release(&head);break;
  50.                 }
  51.         }while(num != 6);

  52.         return 0;
  53. }

  54. void releasepool(void)   //释放内存池
  55. {
  56.         struct Person *temp;

  57.         while(pool != NULL)
  58.         {
  59.                 temp = pool;
  60.                 pool = pool->next;
  61.                 free(pool);
  62.         }
  63. }


  64. void delPerson(struct Person **head)  //4.删除已有联系人
  65. {
  66.         struct Person *now;
  67.         struct Person *before = NULL;
  68.         struct Person *temp;
  69.         int num,count = 1,num1;

  70.         now = *head;

  71.         num1 = displayContacts(*head,4);

  72.         if(num1 != 1)
  73.         {
  74.                 do
  75.                 {
  76.                         printf("请输入要删除的联系人序号(1~%d):",sum);
  77.                         scanf("%d",&num);
  78.                         fflush(stdin);
  79.                         if(num <= sum && num >= 1)
  80.                         {
  81.                                 break;
  82.                         }
  83.                         printf("序号输出错误,请重新输入!\n");
  84.                 }while(1);

  85.                 while(count < num && now != NULL)
  86.                 {
  87.                         before = now;
  88.                         now = now->next;
  89.             count++;
  90.                 }

  91.                 if(before == NULL)
  92.                 {
  93.                         *head  = now->next;
  94.                 }
  95.                 else
  96.                 {
  97.                         before->next = now->next;
  98.                 }
  99.                 if(sum < MAX) //判断内存是否有空位
  100.                 {
  101.                         if(pool != NULL)
  102.                         {
  103.                                 temp = pool;
  104.                                 pool = now;
  105.                                 now->next = temp;
  106.                         }
  107.                         else
  108.                         {
  109.                                 pool = now;
  110.                                 now->next = NULL;
  111.                         }
  112.                 }
  113.                 else
  114.                 {
  115.                         free(now);
  116.                 }
  117.                 sum--;

  118.                 printf("\n*联系人信息删除完成!\n");
  119.         }

  120. }

  121. void changePerson(struct Person *head)  //3.更改已有联系人
  122. {
  123.         struct Person *person;
  124.         int num1 = 0,num,count = 1;
  125.     char ch;   //判读是否更改姓名、电话

  126.         person = head;
  127.         num1 = displayContacts(head,3);

  128.         if(num1 != 1)
  129.         {
  130.                 do
  131.                 {
  132.                         printf("请输入要更改的联系人序号(1~%d):",sum);
  133.                         scanf("%d",&num);
  134.                         fflush(stdin);
  135.                         if(num <= sum && num >= 1)
  136.                         {
  137.                                 break;
  138.                         }
  139.                         printf("序号输出错误,请重新输入!\n");
  140.                 }while(1);

  141.                 while(person != NULL && count < num)
  142.                 {
  143.                         person = person->next;
  144.                         count++;
  145.                 }

  146.                 printf("\n*更改第%d位联系人信息:\n",num);
  147.                 printf("是否更改姓名“%s ”(Y/N)?",person->name);
  148.         scanf("%c",&ch);
  149.         fflush(stdin);
  150.         if(ch == 'Y')
  151.         {
  152.             printf("更改为:");
  153.                         scanf("%s",person->name);
  154.                         fflush(stdin);
  155.                 }
  156.                 printf("是否更改电话“%s ”(Y/N)?",person->phonenum);
  157.         scanf("%c",&ch);
  158.         fflush(stdin);
  159.         if(ch == 'Y')
  160.         {
  161.             printf("更改为:");
  162.                         scanf("%s",person->phonenum);
  163.                         fflush(stdin);
  164.                 }
  165.                 printf("\n*联系人信息更改完成!\n");
  166.         }
  167.         else
  168.         {
  169.                 return;
  170.         }
  171. }

  172. void findPerson(struct Person *head)   //2.查找已有联系人
  173. {
  174.         char find[128];
  175.         int num = 0;
  176.         struct Person *person;

  177.         printf("请输入查找信息(姓名/电话):");
  178.         scanf("%s",find);
  179.         getchar();

  180.         person = head;
  181.         while(person != NULL)
  182.         {
  183.                 if(!strcmp(person->name,find) || !strcmp(person->phonenum,find))
  184.                 {
  185.                     if(num == 0)
  186.                         {
  187.                                 printf("\n-------已查找到以下联系信息-------\n");
  188.                         }
  189.                         printf("\n第%d位联系人:\n",num + 1);
  190.                         printf("姓名:%s\n",person->name);
  191.                         printf("电话:%s\n",person->phonenum);
  192.                           num++;
  193.                 }
  194.                 person = person->next;
  195.         }
  196.         if(num == 0)
  197.         {
  198.                 printf("\n--------未查到相关联系信息--------\n");
  199.         }
  200.         printf("\n----------------------------------\n");

  201. }
  202. void release(struct Person **head)  //6.退出通讯录程序
  203. {
  204.     struct Person *temp;
  205.    
  206.     while(*head != NULL)  //释放已存储的
  207.     {
  208.             temp = *head;
  209.             *head = (*head)->next;
  210.                 free(*head);
  211.         }

  212.         releasepool();  //释放内存池的

  213.         printf("\n*此通讯录小程序已退出!\n");
  214.         exit(1);
  215. }

  216. int displayContacts(struct Person *head,int count)  //5.显示当前通讯录
  217. {
  218.     int num = 1;
  219.         struct Person *person;

  220.         person = head;

  221.     printf("------------通讯录如下------------\n");
  222.         while(person != NULL)
  223.         {
  224.                 printf("\n第%d位联系人信息如下:\n",num);
  225.                 printf("姓名:%s\n",person->name);
  226.                 printf("电话:%s\n",person->phonenum);
  227.                 num++;
  228.                 person = person->next;
  229.         }
  230.         if(num == 1)
  231.         {
  232.                 if(count == 0)
  233.                 {
  234.                         printf("\n当前通讯录暂无联系人,请添加后再显示!\n");
  235.                 }
  236.                 else if(count == 3)
  237.                 {
  238.                         printf("\n当前通讯录暂无联系人,请添加后再进行更改!\n");
  239.                 }
  240.                 else if(count == 4)
  241.                 {
  242.                         printf("\n当前通讯录暂无联系人,请添加后再进行删除!\n");
  243.                 }
  244.             printf("\n----------------------------------\n");
  245.                 return 1;
  246.         }
  247.         printf("\n----------------------------------\n");
  248.         num = 1;
  249. }

  250. void getPerson(struct Person *person)  //1.1录入联系人
  251. {
  252.         printf("\n请输入联系人姓名:");
  253.         scanf("%s",person->name);
  254.         getchar();
  255.         printf("请输入联系人电话:");
  256.         scanf("%s",person->phonenum);
  257.         getchar();
  258. }

  259. void addPerson(struct Person **head)  //1.头插法插入联系人
  260. {
  261.         struct Person *person;
  262.         struct Person *temp;

  263.         if(pool != NULL)  //检测内存池是否有位置
  264.         {
  265.                 person = pool;
  266.                 pool = pool->next;
  267.         }
  268.         else
  269.         {
  270.                 person = (struct Person *)malloc(sizeof(struct Person));
  271.                 if(person == NULL)
  272.                 {
  273.                         printf("内存分配失败!\n");
  274.                         exit(1);
  275.                 }
  276.         }

  277.         getPerson(person);  //录入联系人

  278.         if(*head == NULL)
  279.         {
  280.                 *head = person;
  281.                 person->next = NULL;
  282.         }
  283.         else
  284.         {
  285.                 temp = *head;
  286.                 *head = person;
  287.                 person->next = temp;
  288.         }
  289.         printf("第%d位联系人已录入完成!\n",++sum);
  290. }

  291. void add1Person(struct Person **head)  //1.尾插法插入联系人
  292. {
  293.         struct Person *person;
  294.         static struct Person *tail;

  295.         if(pool != NULL)  //检测内存池是否有位置
  296.         {
  297.                 person = pool;
  298.                 pool = pool->next;
  299.         }
  300.         else
  301.         {
  302.                 person = (struct Person *)malloc(sizeof(struct Person));
  303.                 if(person == NULL)
  304.                 {
  305.                         printf("内存分配失败!\n");
  306.                         exit(1);
  307.                 }
  308.         }

  309.         getPerson(person);  //录入联系人

  310.         if(*head == NULL)
  311.         {
  312.                 *head = person;
  313.                 person->next = NULL;
  314.         }
  315.         else
  316.         {
  317.                 tail->next = person;
  318.                 person->next = NULL;
  319.         }
  320.     tail = person;
  321.         printf("第%d位联系人已录入完成!\n",++sum);
  322. }

  323. void star()
  324. {
  325.         printf("********************************\n");
  326.         printf("**     夏日de通讯录小程序     **\n");
  327.         printf("**  功能选项:                **\n");
  328.         printf("**  1.添加新的联系人          **\n");
  329.         printf("**  2.查找已有联系人          **\n");
  330.         printf("**  3.更改已有联系人          **\n");
  331.         printf("**  4.删除已有联系人          **\n");
  332.         printf("**  5.显示当前通讯录          **\n");
  333.         printf("**  6.退出通讯录程序          **\n");
  334.         printf("**                            **\n");
  335.         printf("********************************\n");
  336. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-22 15:08:42 | 显示全部楼层
看完了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-6 18:48:05 | 显示全部楼层
好好学习才能天天向上!求更新
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 08:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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