当前位置: 首页 > news >正文

数据结构——基于顺序表实现通讯录

一、. 基于动态顺序表实现通讯录

1.1  功能要求

1)⾄少能够存储100个⼈的通讯信息

2)能够保存⽤⼾信息:名字、性别、年龄、电话、地址等
3)增加联系⼈信息
4)删除指定联系⼈
5)查找制定联系⼈
6)修改指定联系⼈
7)显⽰联系⼈信息

1.2   思路分析

我们之前创建的顺序表可以实现连续存储数据(类型可以为整型、字符等),但无论是哪种类型,存储信息都比较单一,但是通讯录存储信息比较多,有联系人姓名、性别、年龄等,所以我们把一个联系人的所有信息作为一个整体存储到顺序表,原来我们写的是整型作为数据存储每个数组元素空间,现在转化通讯录,把一个人的所有信息打包变为结构体然后存储到数组元素元素的空间,然后基于顺序表实现通讯录功能。

1.3 通讯录的实现

因为我们是基于顺序表实现通讯录,先将顺序表写好

1.3.1 顺序表

1.3.1.1  SeqList.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
//#include"Contact.h"
#define NAME_MAX 100
#define SEX_MAX 10
#define TEL_MAX 15
#define ADDR_AMX 20
struct ContactInfo
{char name[NAME_MAX];char sex[SEX_MAX];int age;char tel[TEL_MAX];char addr[ADDR_AMX];
};
typedef struct ContactInfo  SLDateType;
typedef struct SeqList
{SLDateType* a;int size;//空间有效数据int capacity;//空间大小
}SL;//初始化和销毁
void SLInit(SL* ps);
void SLDestory(SL* ps);//尾插和头插
void SLPushBack(SL* ps, SLDateType x);
void SLPushFront(SL* ps, SLDateType x);
//void SLPrint(SL* ps);//头删和尾删
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);//在指定位置之前插入
void SLInsert(SL* ps, int pos, SLDateType x);
//删除指定位置
void SLDel(SL* ps, int pos);

注:这里值得注意的是我们将通讯信息结构体定义在SeqList.h中,然后重命名,后面的顺序表结构也是这样,那为什么不把通讯信息结构体定义在Contact.h,然后再SeqList.h中包含Contact.h,对typedef struct ContactInfo  SLDateType

这样看似没问题,实际会造成重命名的问题,所以我们字书时要规范。

输出结果:

1.3.1.2  SeqList.c
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
#include<stdio.h>
#include<assert.h>
#include<stdbool.h>
void SLInit(SL* ps)
{ps->a = NULL;ps->size = ps->capacity = 0;
}
void SLDestory(SL* ps)
{if (ps->a)free(ps->a);ps->a = NULL;ps->size = ps->capacity = 0;
}
int  SLCheckCapacity(SL* ps)
{assert(ps);int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;if (ps->size == ps->capacity)//进行扩容{SLDateType* ret = (SLDateType*)realloc(ps->a, newcapacity * sizeof(SLDateType));if (ret == NULL){perror("realloc fail:");return 1;}ps->a = ret;ps->capacity = newcapacity;}
}void SLPushBack(SL* ps, SLDateType x)//1.若空间足够,进行插入  2.若空间不够,进行扩容(以1.5或2倍原来大小进行扩容)
{assert(ps);SLCheckCapacity(ps);ps->a[ps->size] = x;ps->size++;
}
//void SLPrint(SL* ps)
//{
//	assert(ps);
//	for (size_t i = 0; i < ps->size; i++)
//	{
//		printf("%d ", ps->a[i]);
//	}
//	printf("\n");
//}
void SLPushFront(SL* ps, SLDateType x)
{assert(ps);SLCheckCapacity(ps);for (size_t i = ps->size; i > 0; i--){ps->a[i] = ps->a[i-1];}ps->a[0] = x;ps->size++;
}bool SLIsEmpty(SL * ps)//若返回值为假,则有数据,反之,则无
{assert(ps);return ps->size == 0;
}
void SLPopBack(SL* ps)
{assert(ps);//还需要判断是否有数据assert(!SLIsEmpty(ps));ps->size--;
}
void SLPopFront(SL* ps)
{assert(ps);//还需要判断是否有数据assert(!SLIsEmpty(ps));for (size_t i = 0; i < ps->size-1; i++){ps->a[i] = ps->a[i + 1];}ps->size--;
}// 在指定位置之前插入
void SLInsert(SL* ps, int pos, SLDateType x)
{assert(ps);SLCheckCapacity(ps);//防止越界访问assert(pos>= 0 && pos<= ps->size);//端点位置为头插和尾插for (size_t i = ps->size; i >pos; i--){ps->a[i] = ps->a[i - 1];}ps->a[pos] = x;ps->size++;
}
//删除指定位置
void SLDel(SL* ps, int pos)
{assert(ps);assert(!SLIsEmpty(ps));assert(pos>= 0 && pos< ps->size);//端点位置为头删和尾删for (size_t i = pos; i < ps->size-1; i++){ps->a[i] = ps->a[i + 1];}ps->size--;
}

1.3.2  通讯录的初始化+销毁

1.3.2.1 Contact.h
#define _CRT_SECURE_NO_WARNINGStypedef  struct ContactInfo CInfo;
typedef struct SeqList Contact;//将顺序表重命名为通讯录(这里只是声明,无需包含Seqlist.h)//通讯录的初始化和销毁
void ContactInit(Contact* con);
void ContactDestroy(Contact* con);

注:这里没有包含SeqList.h,为什么可以重定义结构体和顺序表呢?

因为这里我们只是声明,没有使用,当在text.c使用时,它就会将Contact.h的CInfo和Contact

展开。

 1.3.2.2 Contact.c
#define _CRT_SECURE_NO_WARNINGS
#include"Contact.h"
#include"SeqList.h"
#include <stdlib.h>
#include<stdio.h>
#include<assert.h>
void ContactInit(Contact* con)
{SLInit(con);
}
void ContactDestroy(Contact* con)
{SLDestory(con);
}

这里直接借用顺序表实现

1.3.3   通讯录的添加+删除

1.3.3.1 Contact.h
//通讯录的初始化和销毁
void ContactInit(Contact* con);
void ContactDestroy(Contact* con);
1.3.3.2 Contact.c
void ContactAdd(Contact* con)
{assert(con);CInfo info;printf("请输入姓名:\n");scanf("%s", info.name);printf("请输入性别:\n");scanf("%s", info.sex);printf("请输入年龄:\n");scanf("%d", &info.age);printf("请输入电话号码:\n");scanf("%s", info.tel);printf("请输入地址:\n");scanf("%s", info.addr);//将数据进行尾插SLPushBack(con,info);}
int FindByName(Contact* con,char name[])
{for (int i = 0; i < con->size; i++){if (strcmp(con->a[i].name, name)==0){return i;}}return -1;
}void ContactDel(Contact* con)
{assert(con);char name[NAME_MAX];printf("需要删除的姓名:\n");scanf("%s", name);//先查找该信息位置int ret=FindByName(con,name);if (ret < 0){printf("没找到该联系人\n");return 1;}SLDel(con, ret);//删除指定位置
}

注: info.name这个是数组名,所以不用取地址

1.3.4  查看通讯录 

1.3.4.1 Contact.h
//查看通讯录
void ContactShow(Contact* con);
1.3.4.2  Contact.c
void ContactShow(Contact* con)
{assert(con);assert(con->a);printf("%-4s%-4s%-4s%-4s%-4s\n", "姓名", "性别", "年龄", "电话", "住址");for (int i = 0; i < con->size;i++){printf("%-4s %-4s %-4d%-4s%-4s\n",con->a[i].name, con->a[i].sex, con->a[i].age, con->a[i].tel, con->a[i].addr);}
}

1.3.5  修改通讯录

1.3.5.1 Contact.h
//修改通讯录
void ContactChange(Contact* con);
1.3.5.2 Contact.c
void ContactChange(Contact* con)
{assert(con);char name[NAME_MAX];printf("需要修改的姓名:\n");scanf("%s",name);//先查找该信息位置int ret = FindByName(con, name);if (ret < 0){printf("没找到该联系人\n");return 1;}printf("请输入修改的姓名:\n");scanf("%s",con->a[ret].name);printf("请输入修改的性别:\n");scanf("%s", con->a[ret].sex);printf("请输入修改的年龄:\n");scanf("%d", &con->a[ret].age);printf("请输入修改的电话号码:\n");scanf("%s", con->a[ret].tel);printf("请输入修改的地址:\n");scanf("%s", con->a[ret].addr);printf("修改成功!\n");}

1.3.6 查找指定联系人 

1.3.6.1 Contact.h
//查找指定联系人
void ContactFind(Contact* con);
1.3.6.2 Contact.c
void ContactFind(Contact* con)
{assert(con);char name[NAME_MAX];printf("需要查找的姓名:\n");scanf("%s", name);//先查找该信息位置int ret = FindByName(con, name);if (ret < 0){printf("没找到该联系人\n");return 1;}printf("%-4s%-4s%-4d%-4s%-4s\n",con->a[ret].name, con->a[ret].sex, con->a[ret].age, con->a[ret].tel, con->a[ret].addr);
}

1.3.7 菜单界面

为方便调用通讯录的各种功能,我们做一个菜单界面

1.3.7 .1 text.c
void menu()
{ printf("******************    通讯录   **********************\n");printf("***   1.添加联系人          2.删除联系人       ******\n");printf("***   3.修改通讯录          4.查看指定联系人   ******\n");printf("***   5.查看通讯录          0.退出通讯录       ******\n");printf("*****************************************************\n");}
int main()
{int n = -1;Contact con;ContactInit(&con);do{menu();printf("请输入你的选择;\n");scanf("%d", &n);switch (n){case 1:ContactAdd(&con);break;case 2:ContactDel(&con);break;case 3:ContactChange(&con);break;case 4:ContactFind(&con);break;case 5:ContactShow(&con);break;case 0:break;default:printf("输入错误,请从新输入!\n");break;}}while (n);ContactDestroy(&con);return 0;
}

1.3.8  测试界面

 

http://www.lryc.cn/news/216731.html

相关文章:

  • 行业追踪,2023-11-03
  • JSPv2之El
  • 出现 gpg: cancelled by user时的处理方法
  • MySQL中表的增删改查
  • web.py python服务器两种模板template使用方法
  • Flutter 01 目录结构入门
  • Esxi安装OpenWrt
  • tuple 简易实现(C++ 模板元编程)
  • Http代理与socks5代理有何区别?如何选择?(二)
  • java中main方法和@Test注解的区别
  • C++进阶语法——STL 标准模板库(下)(Standard Template Library)【学习笔记(七)】
  • 力扣:求最长公共前缀
  • Redis入门04-消息通知
  • 关于idea使用的一些操作设置
  • CLion 2023.2.2(C ++ IDE智能代码编辑器)
  • 企业级API资产如何管理
  • Git https方式拉的代码IDEA推送代码报错
  • C++ capacity()用法总结
  • TensorFlow2.0教程1-Eager
  • Redis学习系统(持续更新中)
  • el-select获取id和name
  • 最简单的驱动程序
  • MFC String类的初始化学习
  • vue项目使用vite设置proxy代理,vite.config.js配置,解决本地跨域问题
  • 实用-----七牛云绑定自定义域名 配置 HTTPS
  • 【TS篇三】类、函数、for-of循环、类型推断
  • Chatgpt批量改写文章网页版可多开软件-自动登录换号生成word或者TXT
  • Modelsim 使用教程(4)—— Working With Multiple Libraries
  • 【重要】浏览器输入地址提示【您的连接不是私密连接】解决方法
  • ESP-07S烧写固件记录