目录
[1. SeqList.h 头文件](#1. SeqList.h 头文件)
[2. SeqList.c 源文件](#2. SeqList.c 源文件)
[3. Contact.h 头文件](#3. Contact.h 头文件)
[4. Contact.c 源文件](#4. Contact.c 源文件)
[5. test.c 源文件](#5. test.c 源文件)
[6. 代码补充](#6. 代码补充)
[6.1 基于顺序表实现通讯录项目](#6.1 基于顺序表实现通讯录项目)
[6.2 对顺序表改名操作注意事项编辑](#6.2 对顺序表改名操作注意事项编辑)
正文开始:
1. SeqList.h 头文件
cpp
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Contact.h"
typedef peoInfo SLDataType;
//动态顺序表
typedef struct SeqList
{
SLDataType* arr;
int size; //有效数据个数
int capacity; //空间大小
}SL;
//顺序表初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
//头部插入 / 尾部插入
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);
//头部删除 / 尾部删除
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);
//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
2. SeqList.c 源文件
cpp
#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{
if (ps->arr) //等价于 if(ps->arr != NULL)
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
//插入数据之前先看空间够不够
if (ps->capacity == ps->size)
{
//三目表达式
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));//要申请多大的空间
if (tmp == NULL)
{
perror("realloc fail!");
exit(1);//直接退出程序,不再继续执行
}
//空间申请成功
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps); //等价与assert(ps != NULL)
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
//头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
//先让顺序表中已有的数据整体往后挪动一位
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];//arr[1] = arr[0]
}
ps->arr[0] = x;
ps->size++;
}
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);//顺序表不为空
--ps->size;
}
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
//数据整体往前挪动一位
for (int i = 0; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1]; //arr[size-2] = arr[size-1]
}
ps->size--;
}
//在指定位置之前插入数据
// 1 2 size = 2
//pos 0 -1 100000
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
//插入数据前先查看空间够不够
SLCheckCapacity(ps);
//让pos及之后的数据整体往后挪动一位
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1];//arr[pos+1] = arr[pos]
}
ps->arr[pos] = x;
ps->size++;
}
//删除指定位置的数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
3. Contact.h 头文件
cpp
#pragma once
#define NAME_MAX 20
#define GENDER_MAX 10
#define TEL_MAX 20
#define ADDR_MAX 100
//定义联系人数据 结构
//姓名 性别 年龄 电话 地址
typedef struct personInfo
{
char name[NAME_MAX];
char gender[GENDER_MAX];
int age;
char tel[TEL_MAX];
char addr[ADDR_MAX];
}peoInfo;
//要用到顺序表相关的方法,对通讯录的操作实际就是对顺序表进行操作
//给顺序表改个名字,叫做通讯录。
typedef struct SeqList Contact; //sl
//typedef SL Contact; 此写法错误
//通讯录相关的方法
//通讯录的初始化
void ContactInit(Contact* con);
//通讯录的销毁
void ContactDesTroy(Contact* con);
//通讯录添加数据
void ContactAdd(Contact* con);
//通讯录删除数据
void ContactDel(Contact* con);
//通讯录的修改
void ContactModify(Contact* con);
//通讯录查找
void ContactFind(Contact* con);
//展示通讯录数据
void ContactShow(Contact* con);
4. Contact.c 源文件
cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Contact.h"
#include "SeqList.h"
//通讯录的初始化
void ContactInit(Contact* con)//sl
{
//实际上要进行的是顺序表的初始化
//顺序表的初始化已经实现好了
SLInit(con);
}
//通讯录的销毁
void ContactDesTroy(Contact* con)
{
SLDestroy(con);
}
//通讯录添加数据
void ContactAdd(Contact* con)
{
//获取用户输入的内容:姓名+性别+年龄+电话+地址
peoInfo info;
printf("请输入要添加的联系人姓名:\n");
scanf("%s", info.name); //此处因为info.name为数组首元素地址,所以无需取地址符号
printf("请输入要添加的联系人性别:\n");
scanf("%s", info.gender);
printf("请输入要添加的联系人年龄:\n");
scanf("%d", &info.age);
printf("请输入要添加的联系人电话:\n");
scanf("%s", info.tel);
printf("请输入要添加的联系人住址:\n");
scanf("%s", info.addr);
//往通讯录中添加联系人数据
SLPushBack(con, info);
}
//展示通讯录数据
void ContactShow(Contact* con)
{
//表头:姓名 性别 年龄 电话 地址
printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
//遍历通讯录,按照格式打印每个联系人数据
for (int i = 0; i < con->size; i++)
{
printf("%3s %3s %3d %3s %3s\n", //手动调整一下格式
con->arr[i].name,
con->arr[i].gender,
con->arr[i].age,
con->arr[i].tel,
con->arr[i].addr
);
}
}
int FindByName(Contact* con, char name[])
{
for (int i = 0; i < con->size; i++)
{
if (0 == strcmp(con->arr[i].name, name))
{
//找到了
return i;
}
}
//没有找到
return -1;
}
//通讯录删除数据
void ContactDel(Contact* con)
{
//要删除的数据必须要存在,才能执行删除操作
//查找
char name[NAME_MAX];
printf("请输入要删除的联系人姓名:\n");
scanf("%s", name);
int find = FindByName(con, name);
if (find < 0)
{
printf("要删除的联系人数据不存在!\n");
return;
}
//要删除的联系人数据存在--->知道了要删除的联系人数据对应的下标
SLErase(con, find);
printf("删除成功!\n");
}
//通讯录的修改
void ContactModify(Contact* con)
{
//要修改的联系人数据存在
char name[NAME_MAX];
printf("请输入要修改的用户姓名:\n");
scanf("%s", name);
int find = FindByName(con, name);
if (find < 0)
{
printf("要修改的联系人数据不存在!\n");
return;
}
//直接修改
printf("请输入新的姓名:\n");
scanf("%s", con->arr[find].name);
printf("请输入新的性别:\n");
scanf("%s", con->arr[find].gender);
printf("请输入新的年龄:\n");
scanf("%d", &con->arr[find].age);
printf("请输入新的电话:\n");
scanf("%s", con->arr[find].tel);
printf("请输入新的住址:\n");
scanf("%s", con->arr[find].addr);
printf("修改成功!\n");
}
//通讯录查找
void ContactFind(Contact* con)
{
//11
char name[NAME_MAX];
printf("请输入要查找的联系人姓名\n");
scanf("%s", name);
int find = FindByName(con, name);
if (find < 0)
{
printf("要查找的联系人数据不存在!\n");
return;
}
// 姓名 性别 年龄 电话 地址
// 11 11 11 11 11
printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
printf("%3s %3s %3d %3s %3s\n", //手动调整一下格式
con->arr[find].name,
con->arr[find].gender,
con->arr[find].age,
con->arr[find].tel,
con->arr[find].addr
);
}
5. test.c 源文件
cpp
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
//通讯录的测试
void test1()
{
//创建的通讯录对象 实际上就是 顺序表对象,等价于SL sl
Contact con;
ContactInit(&con);
ContactDesTroy(&con);
}
void test2()
{
Contact con;
ContactInit(&con);
ContactAdd(&con);
ContactShow(&con);
ContactDesTroy(&con);
}
void test3()
{
Contact con;
ContactInit(&con);
ContactAdd(&con);
ContactAdd(&con);
ContactShow(&con);
ContactDel(&con);
ContactShow(&con);
ContactDesTroy(&con);
}
void test4()
{
Contact con;
ContactInit(&con);
ContactAdd(&con);
ContactAdd(&con);
ContactShow(&con);
ContactModify(&con);
ContactShow(&con);
ContactDesTroy(&con);
}
void test5()
{
Contact con;
ContactInit(&con);
ContactAdd(&con);
ContactAdd(&con);
ContactShow(&con);
ContactFind(&con);
ContactDesTroy(&con);
}
//int main()
//{
// //test1();
// //test2();
// //test3();
// //test4();
// test5();
//
// return 0;
//}
void menu()
{
printf("******************通讯录******************\n");
printf("*******1.增加联系人 2.删除联系人********\n");
printf("*******3.修改联系人 4.查找联系人********\n");
printf("*******5.展示联系人 0. 退出 *********\n");
printf("******************************************\n");
}
int main()
{
int op = -1;
Contact con;
ContactInit(&con);
do {
menu();
printf("请选择您的操作:\n");
scanf("%d", &op);
//要根据对应的op执行不同的操作
switch (op)
{
case 1:
ContactAdd(&con);
break;
case 2:
ContactDel(&con);
break;
case 3:
ContactModify(&con);
break;
case 4:
ContactFind(&con);
break;
case 5:
ContactShow(&con);
break;
case 0:
printf("退出通讯录....\n");
break;
default:
printf("输入错误,请重新选择您的操作!\n");
break;
}
} while (op != 0);
ContactDesTroy(&con);
return 0;
}
6. 代码补充
6.1 基于顺序表实现通讯录项目

6.2 对顺序表改名操作注意事项
1. 在头文件Contact.h中,对顺序表重命名的操作中不能将代码写为" typedef SL Contact; ",因为在头文件Contact.h中没有包含头文件SeqList.h(此举相当于程序预处理、编译等过程中,头文件SeqList.h中的内容因为没有在头文件Contact.h中展开,所以头文件Contact.h中的" typedef SL Contact; "这行代码,编译器不能解读出将结构体struct SeqList改名为SL的SL的含义,所以会报错)。从而用前置声明: "typedef struct SeqList Contact;" ,这一操作才能将顺序表正确重命名为Contact(重命名为了让程序有更好的阅读性)。而头文件Contact.h中之所以不选择包含头文件SeqList.h,是因为头文件SeqList.h中已经包含了头文件Contact.h,所以如果头文件Contact.h中再包含头文件SeqList.h的话会出现交叉使用头文件的现象,此现象会报错。
**2.**而在头文件SeqList.h却能直接将peoInfo (头文件Contact.h中的结构体struct personInfo的重命名) 重命名为SLDataType,是因为头文件SeqList.h中包含了头文件Contact.h。