目录
顺序表
通讯录的底层逻辑实际上就是顺序表,所以我们先对顺序表进行实现
顺序表的头文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "catacts.h"
typedef personinfo SLdatatype;
//动态数据表结构
typedef struct Seqlist
{
SLdatatype* arr;
int size;//有效数据个数
int capacity;//当前空间大小
}SL;
//初始化
void SLinit(SL* ps);
void pushback(SL* ps, SLdatatype x);
void pushfront(SL* ps, SLdatatype x);
void SLprint();
void POPback(SL* ps);
void POPfront(SL* ps);
//顺序表的销毁
void SLdestory(SL* ps);
void SLInsert(SL* ps, int pos, SLdatatype x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLdatatype x);
顺序表的源文件
#define _CRT_SECURE_NO_WARNINGS
#include "test.h"
//初始化
void SLinit(SL* ps)
{
ps->arr = NULL;
ps->capacity =ps->size = 0;
//ps->arr=malloc(10);
//ps->capacity =ps->size=10;
}
//销毁
void SLdestory(SL* ps)
{
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
//封装申请空间函数
void SLcheckcapacity(SL*ps)
{
if (ps->capacity == ps->size)
{
//申请空间
int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
SLdatatype* p = (SLdatatype*)realloc(ps->arr, newcapacity * sizeof(SLdatatype));
if (p == NULL)
{
perror("realloc fail!");
return 1;
}
else
{
ps->arr = p;
ps->capacity = newcapacity;
}
}
}
//尾部插入
void pushback(SL* ps, SLdatatype x)
{
assert(ps);
//插入数据之前先看空间够不够
SLcheckcapacity(ps);
ps->arr[ps->size] = x;
ps->size++;
}
//头部插入
void pushfront(SL* ps, SLdatatype 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 SLprint(SL s)
{
for (int i = 0; i < s.size; i++)
{
printf("%d ", s.arr[i]);
}
printf("\n");
}
//尾部删除
void POPback(SL* ps)
{
assert(ps);
assert(ps->size);
//ps->arr[ps->size - 1] = -1;
ps->size--;
}
//头部删除
void POPfront(SL* ps)
{
assert(ps);
assert(ps->size);
for (int i = 0; i<ps->size-1; i++)
{
ps->arr[i] = ps->arr[i + 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[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-1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//查找数据
//int SLFind(SL* ps, SLdatatype x)
//{
// for(int i=0;i<ps->size ;i++)
// {
// if (ps->arr[i] == x)
// {
// return i;
// }
// }
// return -1;
//}
通讯录
通讯录的头文件
#pragma once
//姓名 性别 年龄 家庭住址
#define NAME_MAX 20
#define GENDER_MAX 10
#define ADDR_MAX 100
#define TEL_MAX 20
typedef struct personinfo
{
char name[NAME_MAX];
char gender[GENDER_MAX];
int age;
char addr[ADDR_MAX];
char tel[TEL_MAX];
}personinfo;
//前置声明
struct Seqlist;//原因是头文件不能互相包含
//给顺序表改个名字
typedef struct Seqlist catacts;
//初始化
void catactsInit(catacts* con);
//销毁
void catactsDestroy(catacts* con);
//添加
void catactsAdd(catacts* con);
//删除
void catactsDelet(catacts* con);
//查找
void catactsFind(catacts* con);
//修改
void catactsModify(catacts* con);
//展示
void catactsShow(catacts* con);
通讯录的源文件
#define _CRT_SECURE_NO_WARNINGS
#include "catacts.h"
#include "test.h"
#include <string.h>
void catactsInit(catacts* con)
{
//对通讯录初始化实际上是对顺序表进行初始化
//顺序表已经在test中初始化好了
SLinit(con);
}
void catactsDestroy(catacts* con)
{
SLdestory(con);
}
//添加数据
void catactsAdd(catacts* con)
{
personinfo 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);
pushback(con, info);
}
//删除
//删除数据必须存在才可以执行删除操作
//查找-姓名-性别-电话号码
int FindByname(catacts* con, char name[])
{
for (int i = 0; i < con->size; i++)
{
if (0 == strcmp(con->arr[i].name, name))
{
return i;
}
}
return -1;
}
void catactsDelet(catacts* 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 catactsShow(catacts* con)
{
printf("%s %s %s %s %s\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
);
}
}
void catactsModify(catacts* 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 catactsFind(catacts* con)
{
char name[NAME_MAX];
printf("输入想要查找的联系人姓名:\n");
scanf("%s", name);
int find = FindByname(con, name);
if (find < 0)
{
printf("该联系人不存在\n");
return;
}
printf("%s %s %s %s %s\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
);
}