一.前言
嗨嗨嗨,又和大家见面了!前面我们讲到了如何实现一个循序表。现在我们开始讲解如何基于循序表来实现通讯录功能。
二.正文
通讯录中的SeqlList.h
#pragma once
//#define SLDateType int
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
typedef PerInfo SLDateType;//通讯录中SeqList.h与顺序表中SeqList.h的区别只是在通讯录中将int换成了结构体PerInfo
typedef struct SeqList
{
SLDateType* arr;
int size;
int capacity;
}SL;
void SLInit();//循序表的初始化
void SLDestroy();//顺序表的销毁
void SLPushBack();//尾部插入
void SLPushFront();//头部插入
void SLPopBack();//尾部删除
void SLPopFront();//头部删除
void SLInsert();//指定位置插入
void SLErase();//指定位置删除
int SLFind();//查找数据
通讯录中的Contact.h
#pragma once
#define NAME_MAX 20
#define GENDER_MAX 20
#define TEL_MAX 20
#define ADDR_MAX 20
typedef struct PersonInfo
{
char name[NAME_MAX];
char gender[GENDER_MAX];
int age;
char tel[TEL_MAX];
char addr[ADDR_MAX];
}PerInfo;
typedef struct SeqList Contact;
void ContactInit(Contact* con);//通讯录的初始化
void ContactDestroy();//通讯录的销毁
void ContactAdd();//通讯录添加数据
void ContactDel();//通讯录删除数据
void ContactModify();//通讯录修改数据
int ContactFind();//通讯录查找数据
void ContactShow();//通讯录展示数据
通讯录中的SeqList.c
#include"SeqList.h"
void SLInit(SL* ps)//循序表的初始化函数的实现
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
void SLDestroy(SL* ps)//顺序表销毁的函数实现
{
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 ? 6 : 2 * ps->capacity;
SLDateType* tmp = (SLDateType*)realloc(ps->arr, NewCapacity * sizeof(SLDateType));
if (tmp == NULL)
{
perror("realloc faile!");
return ;
}
ps->arr = tmp;
ps->capacity = NewCapacity;
}
}
//void SLPrint(SL* ps)
//{
// for (int i = 0; i < ps->size; i++)
// {
// printf("%d ", ps->arr[i]);
// }
// printf("\n");
//}
//void SLPrint(SL s)
//{
// for (int i = 0; i <s .size; i++)
// {
// printf("%d ", s.arr[i]);
// }
// printf("\n");
//}
void SLPushBack(SL* ps, SLDateType x)//尾插函数的实现
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size] = x;
ps->size++;
}
void SLPushFront(SL* ps, SLDateType x)//头插函数的实现
{
assert(ps);
SLCheckCapacity(ps);
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = x;
ps->size++;
}
void SLPopBack(SL* ps)//尾删函数的实现
{
assert(ps);
assert(ps->size);
ps->size--;
}
void SLPopFront(SL* ps)//头删函数的实现
{
for (int i = 0; i < (ps->size) - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
void SLInsert(SL* ps, int pos, SLDateType x)//指定位置的插入
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
for (int i = ps->size; i >= pos + 1; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
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 - 2; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//int SLFind(SL* ps, SLDateType x)
//{
// assert(ps);
// for (int i = 0; i <ps-> size; i++)
// {
// if (ps->arr[i] ==x)
// {
// return i;
// }
// }
// return -1;
//}
通讯录中的Contact.c
#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
#include"SeqList.h"
#include"Contact.h"
#include<string.h>
void ContactInit(Contact* con)
{
SLInit(con);
}
void ContactDestroy(Contact* con)
{
SLDestroy(con);
}
void ContactAdd(Contact* con)
{
PerInfo pf;
printf("请输入用户的姓名\n");
scanf("%s", pf.name);
printf("请输入用户的性别\n");
scanf("%s", pf.gender);
printf("请输入用户的年龄\n");
scanf("%d", &pf.age);
printf("请输入用户的电话\n");
scanf("%s", pf.tel);
printf("请输入用户的地址\n");
scanf("%s", pf.addr);
SLPushBack(con, pf);
}
int ContactFind(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 = ContactFind(con, name);
if (find < 0)
{
printf("没有找到该联系人\n");
ContactShow(con);
return;
}
else
{
SLErase(con, find);
printf("删除成功\n");
return;
}
}
void ContactShow(Contact* con)
{
printf("姓名 ");
printf("性别 ");
printf("年龄 ");
printf("电话 ");
printf("地址 ");
printf("\n");
for (int i = 0; i < con->size; i++)
{
printf("%s ",con->arr[i].name);
printf("%s ", con->arr[i].gender);
printf("%d ", con->arr[i].age);
printf("%s ", con->arr[i].tel);
printf("%s ", con->arr[i].addr);
printf("\n");
}
}
void ContactModify(Contact* con)
{
char name[NAME_MAX];
printf("输入要修改人姓名\n");
scanf("%s", name);
int find = ContactFind(con, name);
if (find < 0)
{
printf("要修改的联系人数据不存在!\n");
ContactShow(con);
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");
}
测试通讯录功能test.c
//#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
#include"Contact.h"
int main()
{
//SL sl;
//SLInit(&sl);
//SLPushBack(&sl, 0);
//SLPushBack(&sl, 1);
//SLPushBack(&sl, 2);
//SLPushBack(&sl, 3);
//SLPushFront(&sl, 3);
// SLPushFront(&sl, 4);
//SLPopBack(&sl);
// SLPopFront(&sl);
// SLInsert(&sl, 3, 99);
//SLErase(&sl, 1);
/*SLFind(&sl, 2);
SLPrint(&sl);
int find = SLFind(&sl, 2);
if (find < 0)
{
printf("没有找到\n");
}
else
{
printf("找到了,该数据下标是%d\n", find);
}*/
Contact Con;
ContactInit(&Con);
ContactAdd(&Con);
ContactAdd(&Con);
//ContactShow(&Con);
ContactDel(&Con);
ContactDestroy(&Con);
return 0;
}
三.结言
今天的分享结束,下次再见了同学们!