顺序表应用——通讯录实现

简介: 顺序表应用——通讯录实现

前言:

       通讯录实现的基础是基于顺序表的实现,对于顺序表如有疑惑之处可翻看之前文章。

一、开始菜单

       菜单的打印类似于之前游戏开始菜单,这里直接上代码,不再过多介绍:

1. void menu()
2. {
3.  printf("********************************\n");
4.  printf("*****1、添加⽤⼾ 2、删除⽤⼾*****\n");
5.  printf("*****3、查找⽤⼾ 4、修改⽤⼾*****\n");
6.  printf("*****5、展⽰⽤⼾ 0、退出 *****\n");
7.  printf("********************************\n");
8. 
9. }
10. int main()
11. {
12.   int input = 0;
13.   do
14.   {
15.     menu();
16.     printf("请选择:");
17.     scanf("%d", &input);
18.     switch (input)
19.     {
20.     case 1:
21. 
22.       break;
23.     case 2:
24. 
25.       break;
26.     case 3:
27. 
28.       break;
29.     case 4:
30. 
31.       break;
32.     case 5:
33. 
34.       break;
35.     default:
36.       printf("输⼊有误,请重新输⼊\n");
37.       break;
38. 
39.     }
40.   } while (input);
41.   return 0;
42. }

       这便是一个简易的操作界面,我们逐渐往里面扩充。

二、通讯录代码实现

       通讯录大家都见过,无外乎有这几大功能:增加联系人,删除联系人,修改联系人,查找联系人,我们将围绕这几大功能进行实现。

       2.1 通讯录初始化

       通讯录的初始化与顺序表相同,所以我们可直接调用,代码如下:

1. void InitContact(contact* con)
2. {
3.   SeqListInit( con);
4. }

       2.2 增加联系人

       增加联系人与顺序表的插入原理相同,只需简单修改即可,大家可在这里自行选择头插或尾插,实现代码如下:

1. void AddContact(contact* con) {
2.  PeoInfo info;
3.  printf("请输⼊姓名:\n");
4.  scanf("%s", &info.name);
5.  printf("请输⼊性别:\n");
6.  scanf("%s", &info.sex);
7.  printf("请输⼊年龄:\n");
8.  scanf("%d", &info.age);
9.  printf("请输⼊联系电话:\n");
10.   scanf("%s", &info.tel);
11.   printf("请输⼊地址:\n");
12.   scanf("%s", &info.addr);
13.   SeqListPushBack(con, info);
14.   printf("插⼊成功!\n");
15. }

       2.3 删除联系人

       在我们进行删除联系人之前,我们应该进行查找是否存在,存在了我们在对其进行删除,在查找时可对其姓名、性别等进行查找,这里以姓名进行查找,代码实现如下:

1. int FindByName(contact* con, char name[]) {
2.  for (int i = 0; i < con->size; i++)
3.  {
4.    if (0 == strcmp(con->a[i].name, name)) {
5.      return i;
6.    }
7.  }
8.  return -1;
9. }
10. void DelContact(contact* con)
11. {
12.   char name[NAME_MAX];
13.   printf("请输⼊要删除的⽤⼾姓名:\n");
14.   scanf("%s", name);
15.   int pos = FindByName(con, name);
16.   if (pos < 0)
17.   {
18.     printf("要删除的⽤⼾不存在,删除失败!\n");
19.     return;
20.   }
21.   SeqListErase(con, pos);
22.   printf("删除成功!\n");
23. }

       2.4  修改联系人

       修改原理与删除类似都是先查找,在进行操作,代码实现如下:

1. void ModifyContact(contact* con)
2. {
3.  char name[NAME_MAX];
4.  printf("请输入要修改的名字: ");
5.  scanf("%s", name);
6.  int pos = FindByName(con, name);
7.  if (pos < 0)
8.  {
9.    printf("要删除的⽤⼾不存在,删除失败!\n");
10.     return;
11.   }
12.   PeoInfo info;
13.   printf("请输⼊要修改的姓名:\n");
14.   scanf("%s", &con->a[pos].name);
15.   printf("请输⼊要修改的性别:\n");
16.   scanf("%s", &con->a[pos].sex);
17.   printf("请输⼊要修改的年龄:\n");
18.   scanf("%d", &con->a[pos].age);
19.   printf("请输⼊要修改的联系电话:\n");
20.   scanf("%s", &con->a[pos].tel);
21.   printf("请输⼊要修改的地址:\n");
22.   scanf("%s", &con->a[pos].addr);
23.   printf("修改成功!\n");
24. }

       2.5 通讯录的销毁

       销毁与初始化类似,这里不过多讲解,代码如下:

1. void DestroyContact(contact* con)
2. {
3.  SeqListDesTroy(con);
4. }

三、总代码

       test.c

1. #define _CRT_SECURE_NO_WARNINGS 1
2. #include"contact.h"
3. #include"seqlist.h"
4. void menu()
5. {
6.  printf("********************************\n");
7.  printf("*****1、添加⽤⼾ 2、删除⽤⼾*****\n");
8.  printf("*****3、查找⽤⼾ 4、修改⽤⼾*****\n");
9.  printf("*****5、展⽰⽤⼾ 0、退出 *****\n");
10.   printf("********************************\n");
11. 
12. }
13. int main()
14. {
15.   contact con;
16.   InitContact(&con);
17.   int input = 0;
18.   do
19.   {
20.     menu();
21.     printf("请选择:");
22.     scanf("%d", &input);
23.     switch (input)
24.     {
25.     case 1:
26.       AddContact(&con);
27.       break;
28.     case 2:
29.       DelContact(&con);
30.       break;
31.     case 3:
32.       FindContact(&con);
33.       break;
34.     case 4:
35.       ModifyContact(&con);
36.       break;
37.     case 5:
38.       ShowContact(&con);
39.       break;
40.     default:
41.       printf("输⼊有误,请重新输⼊\n");
42.       break;
43.     }
44.   } while (input);
45.   return 0;
46. }

       seqlist.h

1. #pragma once
2. #include<stdio.h>
3. #include<assert.h>
4. #include<stdlib.h> 
5. #include"contact.h"
6. //数据类型为PersonInfo
7. typedef struct PersonInfo SQDataType;
8. //typedef int SQDataType;
9. //动态顺序表
10. typedef struct SeqList {
11.   SQDataType* a;
12.   int size;//保存有效数据个数
13.   int capacity;//空间的⼤⼩
14. }SLT;
15. //初始化与销毁
16. void SeqListInit(SLT* psl);
17. void SeqListDesTroy(SLT* psl);
18. void SeqListPrint(SLT sl);
19. void CheckCapacity(SLT* psl);
20. // 头部插⼊删除 / 尾部插⼊删除
21. void SeqListPushBack(SLT* psl, SQDataType x);
22. void SeqListPushFront(SLT* psl, SQDataType x);
23. void SeqListPopBack(SLT* psl);
24. void SeqListPopFront(SLT* psl);
25. //查找
26. int SeqListFind(SLT* psl, SQDataType x);
27. // 在指定位置之前插⼊/删除
28. //void SeqListInsert(SLT* psl, int pos, SQDataType x);
29. void SeqListInsert(SLT* psl, size_t pos, SQDataType x);
30. void SeqListErase(SLT* psl, size_t pos);
31. size_t SeqListSize(SLT* psl);
32. //修改指定位置的值
33. void SeqListAt(SLT* psl, size_t pos, SQDataType x);

       seqlist.c

1. #include "SeqList.h"
2. 
3. void SLInit(SLT* ps) {
4. assert(ps);
5.     ps->a == NULL;
6.     ps->capacity = ps->size = 0;
7. }
8. void SLDestroy(SLT* ps) {
9. assert(ps);
10. if (ps->a) {
11. free(ps->a);
12.     }
13.     ps->a = NULL;
14.     ps->size = ps->capacity = 0;
15. }
16. 
17. //void SLPrint(SL* ps) {
18. //    assert(ps);
19. //    for (int i = 0; i < ps->size; ++i) {
20. //        printf("%d ", ps->a[i]);
21. //    }
22. //    printf("\n");
23. //}
24. void SLCheckCapacity(SLT* ps) {
25. assert(ps);
26. if (ps->size == ps->capacity) {
27.         SQDataType* tmp = (SQDataType*)realloc(ps->a, sizeof(SQDataType) * ps->capacity * 2);
28. if (tmp == NULL) {
29. perror("realloc fail!\n");
30. exit(1);
31.         }
32.         ps->a = tmp;
33.         ps->capacity *= 2;
34.     }
35. }
36. void SLPushBack(SLT* ps, SQDataType x) {
37. assert(ps);
38. //    SLCheckCapacity(ps);
39. //    ps->a[ps->size++] = x;
40. SLInsert(ps, ps->size, x);
41. }
42. void SLPopBack(SLT* ps) {
43. assert(ps);
44. assert(ps->size > 0);
45. //    --ps->size;
46. SLErase(ps, ps->size - 1);
47. }
48. void SLPushFront(SLT* ps, SQDataType x) {
49. assert(ps);
50. //    SLCheckCapacity(ps);
51. //    for (int i = ps->size; i > 0; --i) {
52. //        ps->a[i] = ps->a[i-1];
53. //    }
54. //    ps->a[0] = x;
55. //    ++ps->size;
56. SLInsert(ps, 0, x);
57. }
58. void SLPopFront(SLT* ps) {
59. assert(ps);
60. assert(ps->size > 0);
61. //    for (int i = 0; i < ps->size-1; ++i) {
62. //        ps->a[i] = ps->a[i+1];
63. //    }
64. //    --ps->size;
65. SLErase(ps, 0);
66. }
67. //指定位置之前插入数据
68. void SLInsert(SLT* ps, int pos, SQDataType x) {
69. assert(ps);
70. assert(pos <= ps->size && pos >= 0);
71. SLCheckCapacity(ps);
72. for (int i = ps->size; i > pos; --i) {
73.         ps->a[i] = ps->a[i - 1];
74.     }
75.     ps->a[pos] = x;
76.     ++ps->size;
77. }
78. void SLErase(SLT* ps, int pos) {
79. assert(ps);
80. assert(pos < ps->size && pos >= 0);
81. for (int i = pos; i < ps->size - 1; ++i) {
82.         ps->a[i] = ps->a[i + 1];
83.     }
84.     --ps->size;
85. }
86. //int SLFind(SL* ps, SLDataType x) {
87. //    assert(ps);
88. //    for (int i = 0; i < ps->size; ++i) {
89. //        if (ps->a[i] == x) {
90. //            return i;
91. //        }
92. //    }
93. //    return -1;
94. //}

       contact.h

1. #pragma once
2. #include<stdio.h>
3. #include<stdlib.h>
4. #include<assert.h>
5. #include"seqlist.h"
6. #define NAME_MAX 100
7. 
8. #define SEX_MAX 4
9. 
10. #define TEL_MAX 11
11. 
12. #define ADDR_MAX 100
13. 
14. 
15. 
16. //前置声明
17. 
18. typedef struct SeqList contact;
19. 
20. 
21. 
22. //用户数据
23. 
24. typedef struct PersonInfo
25. 
26. {
27.   char name[NAME_MAX];
28.   char sex[SEX_MAX];
29.   int age;
30.   char tel[TEL_MAX];
31.   char addr[ADDR_MAX];
32. 
33. }PeoInfo;
34. 
35. 
36. 
37. //初始化通讯录
38. 
39. void InitContact(contact* con);
40. 
41. //添加通讯录数据
42. 
43. void AddContact(contact* con);
44. 
45. //删除通讯录数据
46. 
47. void DelContact(contact* con);
48. 
49. //展示通讯录数据
50. 
51. void ShowContact(contact* con);
52. 
53. //查找通讯录数据
54. 
55. void FindContact(contact* con);
56. 
57. //修改通讯录数据
58. 
59. void ModifyContact(contact* con);
60. 
61. //销毁通讯录数据
62. 
63. void DestroyContact(contact* con);

       contact.c

1. #define _CRT_SECURE_NO_WARNINGS 1
2. #include"contact.h"
3. #include"seqlist.h"
4. void InitContact(contact* con)
5. {
6.   SeqListInit( con);
7. }
8. 
9. void AddContact(contact* con) {
10.   PeoInfo info;
11.   printf("请输⼊姓名:\n");
12.   scanf("%s", &info.name);
13.   printf("请输⼊性别:\n");
14.   scanf("%s", &info.sex);
15.   printf("请输⼊年龄:\n");
16.   scanf("%d", &info.age);
17.   printf("请输⼊联系电话:\n");
18.   scanf("%s", &info.tel);
19.   printf("请输⼊地址:\n");
20.   scanf("%s", &info.addr);
21.   SeqListPushBack(con, info);
22.   printf("插⼊成功!\n");
23. }
24. int FindByName(contact* con, char name[]) {
25.   for (int i = 0; i < con->size; i++)
26.   {
27.     if (0 == strcmp(con->a[i].name, name)) {
28.       return i;
29.     }
30.   }
31.   return -1;
32. }
33. void DelContact(contact* con)
34. {
35.   char name[NAME_MAX];
36.   printf("请输⼊要删除的⽤⼾姓名:\n");
37.   scanf("%s", name);
38.   int pos = FindByName(con, name);
39.   if (pos < 0)
40.   {
41.     printf("要删除的⽤⼾不存在,删除失败!\n");
42.     return;
43.   }
44.   SeqListErase(con, pos);
45.   printf("删除成功!\n");
46. }
47. void ShowContact(contact* con) 
48. {
49.   printf("%-10s %-4s %-4s %15s %-20s\n", "姓名", "性别", "年龄", "联系电话", "地址");
50.     for (int i = 0; i < con->size; i++)
51.     {
52.       printf("%-10s %-4s %-4d %15s %-20s\n",
53.         con->a[i].name,
54.         con->a[i].sex,
55.         con->a[i].age,
56.         con->a[i].tel,
57.         con->a[i].addr);
58.     }
59. }
60. void ModifyContact(contact* con)
61. {
62.   char name[NAME_MAX];
63.   printf("请输入要修改的名字: ");
64.   scanf("%s", name);
65.   int pos = FindByName(con, name);
66.   if (pos < 0)
67.   {
68.     printf("要删除的⽤⼾不存在,删除失败!\n");
69.     return;
70.   }
71.   PeoInfo info;
72.   printf("请输⼊要修改的姓名:\n");
73.   scanf("%s", &con->a[pos].name);
74.   printf("请输⼊要修改的性别:\n");
75.   scanf("%s", &con->a[pos].sex);
76.   printf("请输⼊要修改的年龄:\n");
77.   scanf("%d", &con->a[pos].age);
78.   printf("请输⼊要修改的联系电话:\n");
79.   scanf("%s", &con->a[pos].tel);
80.   printf("请输⼊要修改的地址:\n");
81.   scanf("%s", &con->a[pos].addr);
82.   printf("修改成功!\n");
83. }
84. void DestroyContact(contact* con)
85. {
86.   SeqListDesTroy(con);
87. }

最后:

       本篇文章是基础顺序表的扩展,如若觉得不大理解,可翻阅顺序表,顺序表明白了此篇文章也会明白的。

完!

相关文章
|
4月前
|
存储
基于静态顺序表实现通讯录
基于静态顺序表实现通讯录
|
2月前
|
存储 C语言
顺序表项目实战(基于动态顺序表实现通讯录)
顺序表项目实战(基于动态顺序表实现通讯录)
|
3月前
|
算法
顺序表的应用
顺序表的应用
31 5
|
3月前
|
存储 算法
顺序表专题
顺序表专题
36 4
|
3月前
基于顺序表 --- 实现简易【通讯录】
基于顺序表 --- 实现简易【通讯录】
18 0
|
4月前
|
存储 算法
|
4月前
顺序表的应用之通讯录
学习了顺序表之后,我们也得知道它的实际用途吧!所以,我们今天来学习一下通讯录的实现。
35 0
|
4月前
|
C语言
【通讯录项目 (2 / 3)】基于顺序表的通讯录实现——顺序表功能实现
顺序表的功能我们已经实现,我们使用的是最简单的顺序表,所以整个过程看起来没有困难。在下一篇文章中我们将进行通讯录的实现。 在通讯录里,顺序表的类型不在是简单的" int ",而是结构体类型。 下面给出通讯录的基本功能供大家参考预习。
33 0
|
4月前
|
数据管理
【通讯录项目 (3 / 3)】基于顺序表的通讯录实现——通讯录项目实现
通讯录项目我们实现了大部分内容,接下来你可以自行探索,丰富功能。
33 0
|
4月前
|
存储
实现顺序表的增删查改
现在让我们探索数据结构这个美妙的世界吧!
28 0