【数据结构与算法】动态顺序表实战项目:通讯录
🥕个人主页:开敲🍉
🔥所属专栏:数据结构与算法🍅
目录
【数据结构与算法】动态顺序表实战项目:通讯录
编辑
1. PerInfo.h 头文件声明
2. PerInfo.c 源文件定义
3. Test.c 源文件测试
动态顺序表实战项目:通讯录,近500行的实现代码。
1. PerInfo.h 头文件声明
#pragma once
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>#define NAME_MAX 20
#define GENDER_MAX 10
#define TEL_MAX 20
#define ADDR_MAX 100
typedef struct PerInfo
{
char name[NAME_MAX];
char gender[GENDER_MAX];
int age;
char tel[TEL_MAX];
char addr[ADDR_MAX];
int size;
int capacity;
}PerInfo;
typedef struct SeqList
{
PerInfo* arr;
int size;
int capacity;
}SL;
//初始化
void PerInfoInit(SL* per);//打印
void PerInfoPrint(SL* per);
//释放
void PerInfoRelease(SL* per);//插入数据(尾插)
void PerInfoPushBack(SL* per);//插入数据(头插)
void PerInfoPushHead(SL* per);//插入数据(指定插入)
void PerInfoPushDesi(SL* per);
//删除数据(尾删)
void PerInfoPopBack(SL* per);//删除数据(头删)
void PerInfoPopHead(SL* per);//删除数据(指定删除)
void PerInfoPopDesi(SL* per);//查
void PerInfoFind(SL* per);//改
void PerInfoChange(SL* per);
2. PerInfo.c 源文件定义
#define _CRT_SECURE_NO_WARNINGS 1
#include "PerInfo.h"
//初始化
void PerInfoInit(SL* per)
{
assert(per);
per->arr = (PerInfo*)calloc(sizeof(PerInfo),sizeof(PerInfo));
per->size = 0;
per->capacity = sizeof(PerInfo);
}
//释放
void PerInfoRelease(SL* per)
{
if (per->arr)
{
free(per->arr);
}
per->arr = NULL;
per->capacity = per->size = 0;
}
//打印
void PerInfoPrint(SL* per)
{
printf("姓名 性别 年龄 电话 地址\n");
assert(per);
int i = 0;
if (per->size == 0)
{
printf("\n (无联系人)\n");
return;
}
for (i = 0; i < per->size; i++)
{
printf("%s %10s %4d %15s %15s",
per->arr[i].name,
per->arr[i].gender,
per->arr[i].age,
per->arr[i].tel,
per->arr[i].addr
);
printf("\n");
}
printf("\n");
}
//插入数据(尾插)
void PerInfoPushBack(SL* per)
{
assert(per);
//扩容
while (per->size >= per->capacity)
{
PerInfo* ptr = (PerInfo*)realloc(per->arr, sizeof(PerInfo) * (per->capacity) * 2);
if (ptr == NULL)
{
perror("realloc");
return;
}
per->arr = ptr;
}
per->capacity *= 2;
printf("请输入要添加的联系人姓名:");
scanf("%s", per->arr[per->size].name);
printf("请输入要添加的联系人性别:");
scanf("%s", per->arr[per->size].gender);
printf("请输入要添加的联系人年龄:");
scanf("%d", &(per->arr[per->size].age));
printf("请输入要添加的联系人电话:");
scanf("%s", per->arr[per->size].tel);
printf("请输入要添加的联系人地址:");
scanf("%s", per->arr[per->size].addr);
per->size++;
}
//插入数据(头插)
void PerInfoPushHead(SL* per)
{
assert(per);
int num = per->size;
//扩容
while (per->size >= per->capacity)
{
PerInfo* ptr = (PerInfo*)realloc(per->arr, sizeof(PerInfo) * (per->capacity) * 2);
if (ptr == NULL)
{
perror("realloc");
return;
}
per->arr = ptr;
}
while (num > 0)
{
per->arr[num] = per->arr[num - 1];
num--;
}
printf("请输入要添加的联系人姓名:");
scanf("%s", per->arr[0].name);
printf("请输入要添加的联系人性别:");
scanf("%s", per->arr[0].gender);
printf("请输入要添加的联系人年龄:");
scanf("%d", &(per->arr[0].age));
printf("请输入要添加的联系人电话:");
scanf("%s", per->arr[0].tel);
printf("请输入要添加的联系人地址:");
scanf("%s", per->arr[0].addr);
per->size++;
}//插入数据(指定插入)
void PerInfoPushDesi(SL* per)
{
assert(per);
//扩容
while (per->size >= per->capacity)
{
PerInfo* ptr = (PerInfo*)realloc(per->arr, sizeof(PerInfo) * (per->capacity) * 2);
if (ptr == NULL)
{
perror("realloc");
return;
}
per->arr = ptr;
}
int adr = 0;
printf("请输入要插入的位置:");
scanf("%d", &adr);
int num = per->size;
while (num >= adr)
{
per->arr[num] = per->arr[num - 1];
num--;
}
printf("请输入要添加的联系人姓名:");
scanf("%s", per->arr[adr-1].name);
printf("请输入要添加的联系人性别:");
scanf("%s", per->arr[adr-1].gender);
printf("请输入要添加的联系人年龄:");
scanf("%d", &(per->arr[adr-1].age));
printf("请输入要添加的联系人电话:");
scanf("%s", per->arr[adr-1].tel);
printf("请输入要添加的联系人地址:");
scanf("%s", per->arr[adr-1].addr);
per->size++;
}
int FindByName(SL* per,char* adr)
{
int i = 0;
for (i = 0; i < per->size; i++)
{
if (0 == strcmp(adr, per->arr[i].name))
{
return i;
}
}
return -1;
}//删除数据(尾删)
void PerInfoPopBack(SL* per)
{
assert(per);
if (per->size > 0)
{
per->size--;
}
else
{
printf("删除失败:没有数据可以删除\n");
return;
}
}//删除数据(头删)
void PerInfoPopHead(SL* per)
{
assert(per);
int num = 0;
if (per->size > 0)
{
while (num < per->size)
{
per->arr[num] = per->arr[num + 1];
num++;
}
}
else
{
printf("删除失败:没有数据可以删除\n");
return;
}
per->size--;
}
//删除数据(指定删除)
void PerInfoPopDesi(SL* per)
{
assert(per);
char adr[NAME_MAX] = { 0 };
printf("请输入要删除的联系人姓名:");
scanf("%s", adr);
int find = FindByName(per,adr);
if (find >= 0 && find < per->size)
{
int flag = find;
while (flag < per->size)
{
per->arr[flag] = per->arr[flag + 1];
flag++;
}
printf("删除成功\n");
}
else
{
printf("删除失败:该位置没有数据可以删除\n");
return;
}
per->size--;
}//查
void PerInfoFind(SL* per)
{
assert(per);
char adr[NAME_MAX] = { 0 };
printf("请输入要查询的联系人姓名:");
scanf("%s", adr);
int find = FindByName(per,adr);
if (find >= 0 && find < per->size)
{
printf("查询成功:\n");
printf("姓名 性别 年龄 电话 地址\n");
printf("%s %10s %4d %15s %15s",
per->arr[find].name,
per->arr[find].gender,
per->arr[find].age,
per->arr[find].tel,
per->arr[find].addr
);
}
else
{
printf("查无此人\n");
return;
}
}
//改
void PerInfoChange(SL* per)
{
assert(per);
char adr[NAME_MAX] = { 0 };
printf("请输入要更改的联系人姓名:");
scanf("%s", adr);
int find = FindByName(per, adr);
if (find >= 0 && find < per->size)
{
printf("请输入要修改的内容代号(姓名:name、性别:gen:、年龄:age、电话:tel、地址:addr):");
char s[100] = { 0 };
lable:
scanf("%s", s);
if (0 == strcmp(s, "name"))
{
printf("请输入更换后的名字:");
scanf("%s", per->arr[find].name);
}
else if (0 == strcmp(s, "gen"))
{
printf("请输入更换后的性别:");
scanf("%s", per->arr[find].gender);
}
else if (0 == strcmp(s, "age"))
{
printf("请输入更换后的年龄:");
scanf("%d", &(per->arr[find].age));
}
else if (0 == strcmp(s, "tel"))
{
printf("请输入更换后的电话:");
scanf("%s", per->arr[find].tel);
}
else if (0 == strcmp(s, "addr"))
{
printf("请输入更换后的地址:");
scanf("%s", per->arr[find].addr);
}
else
{
printf("输入有误,请重新输入:");
goto lable;
}
}
else
{
printf("查无此人\n");
return;
}
}
3. Test.c 源文件测试
#define _CRT_SECURE_NO_WARNINGS 1
#include "PerInfo.h"
void menu()
{
printf("*******************************************\n");
printf("*********** 1. 尾插 2. 头插 *************\n");
printf("*********** 3. 尾删 4. 头删 *************\n");
printf("********* 5. 指定插入 6. 指定删除 ********\n");
printf("*********** 7. 查找 8. 更改 *************\n");
printf("*********** 0. 退出 *************\n");
printf("*******************************************\n");
printf("请输入要进行的操作:");
}
enum Contact
{
Exit,
PushBack,
PushHead,
PopBack,
PopHead,
PushDesi,
PopDesi,
Find,
Change,
};
int main()
{
//TestPerInfo();
SL per;
PerInfoInit(&per);
int input = 0;
do
{
menu();
scanf("%d", &input);
int i = (enum Contact)input;
switch (i)
{
case PushBack:
PerInfoPushBack(&per);
PerInfoPrint(&per);
break;
case PushHead:
PerInfoPushHead(&per);
PerInfoPrint(&per);
break;
case PopBack:
PerInfoPopBack(&per);
PerInfoPrint(&per);
break;
case PopHead:
PerInfoPopHead(&per);
PerInfoPrint(&per);
break;
case PushDesi:
PerInfoPushDesi(&per);
PerInfoPrint(&per);
break;
case PopDesi:
PerInfoPopDesi(&per);
PerInfoPrint(&per);
break;
case Find:
PerInfoFind(&per);
break;
case Change:
PerInfoChange(&per);
PerInfoPrint(&per);
break;
case Exit:
printf("退出程序\n");
break;
default:
printf("输入非法,请重新输入\n");
break;
}} while (input);
return 0;
}
创作不易,点个赞呗,蟹蟹啦~