最近要到做大学生做C语言实验的时候了吧。老师们一般都会要求大家完成通讯录、学生信息管理系统等等,那么今天我就带大家从头开始使用顺序表实现通讯录管理,帮助大家完成实验。
一、什么是顺序表
在开始讲解代码之前还是有必要再复习一下顺序表的相关知识的。顺序表是一种线性表,在逻辑上是连续的,在物理上是不连续的 ;顺序表的底层是数组,通过对数组进行封装,实现增删改查。
二、文件结构说明
在实现通讯录之前,我们会先对顺序表的实现进行讲解,方便大家更好地理解顺序表,并自己进行实现。我们先将文件分为SeqList.h,SeqList.c和test.c。
SeqList.h:顺序表函数声明
SeqList.c:实现顺序表函数
test.c:调用函数进行测试
Contact.h:通讯录函数声明
Contact.c:通讯录函数实现
三、顺序表实现
1.SeqList.h
SeqList.h中的代码如下所示,其中包括我们创建的数据结构--顺序表以及要实现的方法。
cpp
#pragma once
//被多个文件包含时,确保头文件只被解析一次
//把要引用的库写在头文件中
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLDataType;
//暂时不知道要存储什么类型的数据,方便后续修改代码
typedef struct SeqList
{
SLDataType* arr;
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.SeqList.c
下面我们在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)
{
free(ps->arr);
ps->arr = NULL; //释放之后记得置空
ps->size = ps->capacity = 0;
}
//打印
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
//扩容
void SLCheckCapacity(SL* ps)
{
assert(ps); //断言一下,ps指向不能为空
//插入数据前要检查顺序表的空间是否足够
if (ps->size == ps->capacity)
{
//如果空间不够的话,若是空间大小目前为0,则分配四个空间,否则扩大一倍
//增容⼀般是呈2或3倍的增⻓,势必会有⼀定的空间浪费
//如果增容过小,则需要频繁增容,会造成资源浪费
//如果增容倍数过大,空间浪费会过多,所以我们选择扩大一倍
int newCapacity = (ps->capacity == 0 ? 4 : 2 * ps->capacity);
//realloc会复制原来的数据,但失败会返回NULL指针,丢失原来的数据
//所以我们先创建一个临时指针来指向增容后的地址
SLDataType* tmp = realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc");
exit(1); //退出程序
}
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
SLCheckCapacity(ps);
//ps->size指向的是最后一个数据的下一个位置
ps->arr[ps->size++] = x;
//使用完size的要+1
}
//头插
void SLPushFront(SL* ps, SLDataType x)
{
SLCheckCapacity(ps);
//头插要把所有数据往后移一位
for (int i = ps->size; i > 0; i--)
{
//第一次是ps->arr[ps->size]=ps->arr[ps->size-1},所以开始条件如上
ps->arr[i] = ps->arr[i - 1];
//最后一次是ps->arr[1]=ps->arr[0],所以结束条件如上
}
ps->arr[0] = x;
ps->size++;
}
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size); //有效数据个数要大于0
//把有效数据个数直接-1即可,后面进行增删改查等操作会直接覆盖这个值
ps->size--;
}
//头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
//直接把数据整体往前移动一位
for (int i = 0; i < ps->size - 1; i++)
{
//第一次ps->arr[0]=ps->arr[1]
ps->arr[i] = ps->arr[i + 1];
//最后一次ps->arr[ps->size-2]=ps->arr[ps->size-1]
}
ps->size--;
}
//指定位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
for (int i = ps->size; i > pos; i--)
{
//ps->arr[ps->size]=ps->arr[ps->size-1]
ps->arr[i] = ps->arr[i - 1];
//ps->arr[pos+1]=ps->arr[pos]
}
ps->arr[pos] = x;
ps->size++;
}
//指定位置删除数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(ps->size);
assert(pos >= 0 && pos <= ps->size - 1);
for (int i = pos; i < ps->size-1; i++)
{
//ps->arr[pos]=ps->arr[pos+1]
ps->arr[i] = ps->arr[i + 1];
//ps->arr[ps->size-2]=ps->arr[ps->size-1]
}
ps->size--;
}
//查找数据
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i]== x)
{
return i; //找到就返回下标
}
}
return - 1; //-1表示未找到
}
四、通讯录实现
实现了顺序表之后,通讯录就很简单了,就是套用在SeqList.c中实现的函数完成要操作的内容,并在终端实现交互。
1.SeqList.h
cpp
#pragma once
//被多个文件包含时,确保头文件只被解析一次
#include "Contact.h"
//把要引用的库写在头文件中
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef peoInfo SLDataType;
typedef struct SeqList
{
SLDataType* arr;
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.Contact.h
cpp
#pragma once
#define NAME_MAX 100
#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;
//前置声明,文件不能相互包含,所以提前声明结构体SeqList
typedef struct SeqList 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);
3.SeqList.c
cpp
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include "SeqList.h"
//初始化
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
//销毁
void SLDestroy(SL* ps)
{
free(ps->arr);
ps->arr = NULL; //释放之后记得置空
ps->size = ps->capacity = 0;
}
////打印,这个函数用的整型,注释掉
//void SLPrint(SL* ps)
//{
// for (int i = 0; i < ps->size; i++)
// {
// printf("%d ", ps->arr[i]);
// }
// printf("\n");
//}
//扩容
void SLCheckCapacity(SL* ps)
{
assert(ps); //断言一下,ps指向不能为空
//插入数据前要检查顺序表的空间是否足够
if (ps->size == ps->capacity)
{
//如果空间不够的话,若是空间大小目前为0,则分配四个空间,否则扩大一倍
//增容⼀般是呈2或3倍的增⻓,势必会有⼀定的空间浪费
//如果增容过小,则需要频繁增容,会造成资源浪费
//如果增容倍数过大,空间浪费会过多,所以我们选择扩大一倍
int newCapacity = (ps->capacity == 0 ? 4 : 2 * ps->capacity);
//realloc会复制原来的数据,但失败会返回NULL指针,丢失原来的数据
//所以我们先创建一个临时指针来指向增容后的地址
SLDataType* tmp = realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc");
exit(1); //退出程序
}
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
SLCheckCapacity(ps);
//ps->size指向的是最后一个数据的下一个位置
ps->arr[ps->size++] = x;
//使用完size的要+1
}
//头插
void SLPushFront(SL* ps, SLDataType x)
{
SLCheckCapacity(ps);
//头插要把所有数据往后移一位
for (int i = ps->size; i > 0; i--)
{
//第一次是ps->arr[ps->size]=ps->arr[ps->size-1},所以开始条件如上
ps->arr[i] = ps->arr[i - 1];
//最后一次是ps->arr[1]=ps->arr[0],所以结束条件如上
}
ps->arr[0] = x;
ps->size++;
}
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size); //有效数据个数要大于0
//把有效数据个数直接-1即可,后面进行增删改查等操作会直接覆盖这个值
ps->size--;
}
//头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
//直接把数据整体往前移动一位
for (int i = 0; i < ps->size - 1; i++)
{
//第一次ps->arr[0]=ps->arr[1]
ps->arr[i] = ps->arr[i + 1];
//最后一次ps->arr[ps->size-2]=ps->arr[ps->size-1]
}
ps->size--;
}
//指定位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
for (int i = ps->size; i > pos; i--)
{
//ps->arr[ps->size]=ps->arr[ps->size-1]
ps->arr[i] = ps->arr[i - 1];
//ps->arr[pos+1]=ps->arr[pos]
}
ps->arr[pos] = x;
ps->size++;
}
//指定位置删除数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(ps->size);
assert(pos >= 0 && pos <= ps->size - 1);
for (int i = pos; i < ps->size-1; i++)
{
//ps->arr[pos]=ps->arr[pos+1]
ps->arr[i] = ps->arr[i + 1];
//ps->arr[ps->size-2]=ps->arr[ps->size-1]
}
ps->size--;
}
////查找数据
//int SLFind(SL* ps, SLDataType x)
//{
// assert(ps);
// for (int i = 0; i < ps->size; i++)
// {
// if (ps->arr[i]== x) //这里假定是整型了,注释掉
// {
// return i; //找到就返回下标
// }
// }
//
// return - 1; //-1表示未找到
//}
4.Contact.c
cpp
#define _CRT_SECURE_NO_WARNINGS
//直接调用顺序表的方法
#include "SeqList.h"
#include <string.h>
//初始化通讯录数据
void ContactInit(Contact* con)
{
SLInit(con);
}
//销毁通讯录数据
void ContactDesTroy(Contact* con)
{
SLDestroy(con);
}
//添加通讯录数据
void ContactAdd(Contact* con)
{
peoInfo info;
printf("请输入要添加的联系人姓名:\n");
scanf("%s", 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);
}
//查找数据下标
int FindByName(Contact* con, char name[])
{
for (int i = 0; i < con->size; i++)
{
if (strcmp(con->arr[i].name, name) == 0)
{
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)
{
char name[NAME_MAX];
printf("请输入要查找的联系人姓名:\n");
scanf("%s", name);
int find = FindByName(con, name);
if (find < 0)
{
printf("未找到要查找的联系人,查找失败!\n");
return;
}
printf("姓名 性别 年龄 电话 地址\n");
printf("%s %s %d %s %s\n",
con->arr[find].name,
con->arr[find].gender,
con->arr[find].age,
con->arr[find].tel,
con->arr[find].addr);
}
//展示通讯录数据
void ContactShow(Contact* con)
{
printf("姓名 性别 年龄 电话 地址\n");
for (int i = 0; i < con->size; i++)
{
printf("%s %s %d %s %s\n",
con->arr[i].name,
con->arr[i].gender,
con->arr[i].age,
con->arr[i].tel,
con->arr[i].addr);
}
}
5.test.c
cpp
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
void menu()
{
printf("******通讯录系统******\n");
printf("*****1.添加联系人*****\n");
printf("*****2.修改联系人*****\n");
printf("*****3.删除联系人*****\n");
printf("*****4.查找联系人*****\n");
printf("*****5.展示联系人*****\n");
printf("***** 0.退出 *****\n");
}
int main()
{
int input = 0;
Contact con;
ContactInit(&con);
do {
menu();
printf("请输入要选择的操作:\n");
scanf("%d", &input);
switch (input)
{
case 1:
ContactAdd(&con);
break;
case 2:
ContactModify(&con);
break;
case 3:
ContactDel(&con);
break;
case 4:
ContactFind(&con);
break;
case 5:
ContactShow(&con);
break;
case 6:
printf("退出通讯录\n");
break;
}
} while (input != 0);
ContactDesTroy(&con);
return 0;
}
代码运行结果可自己运行程序获得,这里不做演示。