数据结构——顺序表(用顺序表实现通讯录)

1.顺序表概念及结构

1.1 线性表

        线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是⼀种在实际中⼴泛使⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串...
        线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

1.2 顺序表分类

顺序表和数组的区别
        ◦ 顺序表的底层结构是数组,对数组的封装,实现了常⽤的增删改查等接⼝。
顺序表分类
        ◦ 静态顺序表
                 概念:使⽤定⻓数组存储元素

静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费 

        ◦ 动态顺序表

1.3 动态顺序表的实现 

#define INIT_CAPACITY 4
typedef int SLDataType;
// 动态顺序表 -- 按需申请
typedef struct SeqList
{
 SLDataType* a;
 int size; // 有效数据个数
 int capacity; // 空间容量
}SL;
//初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
//扩容
void SLCheckCapacity(SL* ps);
//头部插⼊删除 / 尾部插⼊删除
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
//指定位置之前插⼊/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLDataType x);

接下来,我们就开始实现通讯录。

2. 通讯录的实现 

2.1 功能要求

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

2.2 代码实现

【思考1】⽤静态顺序表和动态顺序表分别如何实现?
【思考2】如何保证程序结束后,历史通讯录信息不会丢失?
//SeqList.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<stdlib.h> 
#include"contact.h"
//数据类型为PersonInfo
typedef struct PersonInfo SQDataType;
//typedef int SQDataType;
//动态顺序表
typedef struct SeqList {
 SQDataType* a;
 int size;//保存有效数据个数
 int capacity;//空间的⼤⼩
}SLT;
//初始化与销毁
void SeqListInit(SLT* psl);
void SeqListDesTroy(SLT* psl);
void SeqListPrint(SLT sl);
void CheckCapacity(SLT* psl);
// 头部插⼊删除 / 尾部插⼊删除
void SeqListPushBack(SLT* psl, SQDataType x);
void SeqListPushFront(SLT* psl, SQDataType x);
void SeqListPopBack(SLT* psl);
void SeqListPopFront(SLT* psl);
//查找
int SeqListFind(SLT* psl, SQDataType x);
// 在指定位置之前插⼊/删除
//void SeqListInsert(SLT* psl, int pos, SQDataType x);
void SeqListInsert(SLT* psl, size_t pos, SQDataType x);
void SeqListErase(SLT* psl, size_t pos);
size_t SeqListSize(SLT* psl);
//修改指定位置的值
void SeqListAt(SLT* psl, size_t pos, SQDataType x);

//contact.h
#pragma once
#define NAME_MAX 100
#define SEX_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100

//前置声明
typedef struct SeqList contact;
//⽤⼾数据
typedef struct PersonInfo
{
 char name[NAME_MAX];
 char sex[SEX_MAX];
 int age;
 char tel[TEL_MAX];
 char addr[ADDR_MAX];
}PeoInfo;
//初始化通讯录
void InitContact(contact* con);
//添加通讯录数据
void AddContact(contact* con);
//删除通讯录数据
void DelContact(contact* con);
//展⽰通讯录数据
void ShowContact(contact* con);
//查找通讯录数据
void FindContact(contact* con);
//修改通讯录数据
void ModifyContact(contact* con);
//销毁通讯录数据
void DestroyContact(contact* con);
//contact.c
#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"
#include"SeqList.h"
void LoadContact(contact* con) {
 FILE* pf = fopen("contact.txt", "rb");
 if (pf == NULL) {
 perror("fopen error!\n");
 return;
 }
 //循环读取⽂件数据
 PeoInfo info;
 while (fread(&info,sizeof(PeoInfo),1,pf))
 {
 SeqListPushBack(con, info);
 }
printf("历史数据导⼊通讯录成功!\n");
}
void InitContact(contact* con) {
 SeqListInit(con);
 LoadContact(con);
}
void AddContact(contact* con) {
 PeoInfo 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);
 SeqListPushBack(con, info);
 printf("插⼊成功!\n");
}
int FindByName(contact* con, char name[]) {
 for (int i = 0; i < con->size; i++)
 {
 if (0 == strcmp(con->a[i].name, name)) {
 return i;
 }
 }
 return -1;
}
void DelContact(contact* con){
 char name[NAME_MAX];
 printf("请输⼊要删除的⽤⼾姓名:\n");
 scanf("%s", name);
 int pos = FindByName(con, name);
 if (pos < 0) {
 printf("要删除的⽤⼾不存在,删除失败!\n");
 return;
 }
SeqListErase(con, pos);
 printf("删除成功!\n");
}
void ShowContact(contact* con){
 printf("%-10s %-4s %-4s %15s %-20s\n", "姓名", "性别", "年龄", "联系电话", 
 for (int i = 0; i < con->size; i++)
 {
 printf("%-10s %-4s %-4d %15s %-20s\n",
 con->a[i].name,
 con->a[i].sex,
 con->a[i].age,
 con->a[i].tel,
 con->a[i].addr);
 }
}
void FindContact(contact* con){
 char name[NAME_MAX];
 printf("请输⼊要查找的⽤⼾姓名:\n");
 scanf("%s", name);
 int pos = FindByName(con, name);
 if (pos < 0) {
 printf("要查找的⽤⼾不存在,查找失败!\n");
 return;
 }
 printf("查找成功!\n");
 printf("%-10s %-4s %-4d %15s %-20s\n", 
 con->a[pos].name,
 con->a[pos].sex,
 con->a[pos].age,
 con->a[pos].tel,
 con->a[pos].addr);
}
void ModifyContact(contact* con) {
 char name[NAME_MAX];
 printf("请输⼊要修改的⽤⼾名称:\n");
 scanf("%s", name);
 int pos = FindByName(con, name);
 if (pos < 0) {
 printf("要查找的⽤⼾不存在,修改失败!\n");
 return;
 }
PeoInfo info;
 printf("请输⼊要修改的姓名:\n");
 scanf("%s", &con->a[pos].name);
 printf("请输⼊要修改的性别:\n");
 scanf("%s", &con->a[pos].sex);
 printf("请输⼊要修改的年龄:\n");
 scanf("%d", &con->a[pos].age);
 printf("请输⼊要修改的联系电话:\n");
 scanf("%s", &con->a[pos].tel);
 printf("请输⼊要修改的地址:\n");
 scanf("%s", &con->a[pos].addr);
 printf("修改成功!\n");
}
void SaveContact(contact* con) {
 FILE* pf = fopen("contact.txt", "wb");
 if (pf == NULL) {
 perror("fopen error!\n");
 return;
 }
 //将通讯录数据写⼊⽂件
 for (int i = 0; i < con->size; i++)
 {
 fwrite(con->a + i, sizeof(PeoInfo), 1, pf);
 }
 printf("通讯录数据保存成功!\n");
}
void DestroyContact(contact* con) {
 SaveContact(con);
 SeqListDesTroy(con);
}
//test.c
#include"SeqList.h"
#include"contact.h"
void menu() {
 //通讯录初始化
 contact con;
 InitContact(&con);
 int op = -1;
do {
 printf("********************************\n");
 printf("*****1、添加⽤⼾ 2、删除⽤⼾*****\n");
 printf("*****3、查找⽤⼾ 4、修改⽤⼾*****\n");
 printf("*****5、展⽰⽤⼾ 0、退出 *****\n");
 printf("********************************\n");
 printf("请选择您的操作:\n");
 scanf("%d", &op);
 switch (op)
 {
 case 1:
 AddContact(&con);
 break;
 case 2:
 DelContact(&con);
 break;
 case 3:
 FindContact(&con);
 break;
 case 4:
 ModifyContact(&con);
 break;
 case 5:
 ShowContact(&con);
 break;
 default:
 printf("输入有误,请重新输入\n");
 break;
 }
} while (op!=0);
 //销毁通讯录
 DestroyContact(&con);
}

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

于本淡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值