在之前的基础上,通过动态内存来进行优化,避免了内存的浪费,更加灵活的实现管理,且思路简单,只需将原来的数组改为指针,再使用 realloc 函数来进行内存分配。
优化方式如下:
首先将
struct Address_Lsit AL[1000];
改成了
struct Address_Lsit *AL = NULL;
数组空间是固定的所以改成用指针的方式,这样方便我们来分配内存空间,更改完成后添加了 extend 函数来进行内存空间的扩大:
void extend(struct Address_Lsit **ALp)
{
struct Address_Lsit *p = NULL;
p = realloc(*ALp, (People_num_all + 1) * sizeof(struct Address_Lsit));
if (NULL != p)
{
*ALp = p;
}
}
完成上面两部就已经完成了动态分布内存来实现通讯录的功能
ps:在最后我们可别忘记释放内存,因为那是由我们自己申请下来的,所应该有我们自己来释放,避免造成内存泄漏!!!
在结尾的后面加 free 函数即可
代码实现(环境 Visual Studio 2017)
//文件储存的方式实现通讯录 AddressList
#include <stdio.h>
#include <windows.h>
#include <string.h>
#pragma warning(disable: 4996)
static int People_num_all = 0;//通讯录人数
struct Address_Lsit
{
int age;
int phone_num;
char name[20];
char sex[3];
char Address[50];
};
void menu()
{
printf("----------------------------------------------\n");
printf("-------------- 通讯录菜单 -------------\n");
printf("----------------------------------------------\n");
printf("\n");
printf("\
1.添加联系人信息\n\
2.删除指定联系人信息\n\
3.查找指定联系人信息\n\
4.修改指定联系人信息\n\
5.显示所有联系人信息\n\
6.清空所有联系人\n \
7.以名字排序所有联系人\n\
0.退出");
printf("\n");
printf("\n");
printf("----------------------------------------------\n");
printf("请选择:");
}
//交换两个联系人信息
void exchange(struct Address_Lsit *AL1, struct Address_Lsit *AL2)
{
struct Address_Lsit AL3;
strcpy(AL3.name, AL1->name);//交换名字
strcpy(AL1->name, AL2->name);
strcpy(AL2->name, AL3.name);
strcpy(AL3.sex, AL1->sex);//交换性别
strcpy(AL1->sex, AL2->sex);
strcpy(AL2->sex, AL3.sex);
AL3.age = AL1->age;//交换年龄
AL1->age = AL2->age;
AL2->age = AL3.age;
AL3.phone_num = AL1->phone_num;//交换电话
AL1->phone_num = AL2->phone_num;
AL2->phone_num = AL3.phone_num;
strcpy(AL3.Address, AL1->Address);//交换地址
strcpy(AL1->Address, AL2->Address);
strcpy(AL2->Address, AL3.Address);
}
void show(struct Address_Lsit *AL)//----------显示信息
{
printf("姓名:%s\t", AL->name);
printf("性别:%s\t", AL->sex);
printf("年龄:%d\t", AL->age);
printf("电话:%d\t", AL->phone_num);
printf("地址:%s\n", AL->Address);
}
void add(struct Address_Lsit * AL)----------------------添加联系人信息
{
AL += People_num_all++;
printf("请输入:姓名\n");
scanf("%s", AL->name);
printf("请输入:性别\n");
scanf("%s", AL->sex);
printf("请输入:年龄\n");
scanf("%d", &(AL->age));
printf("请输入:电话\n");
scanf("%d", &(AL->phone_num));
printf("请输入:地址\n");
scanf("%s", AL->Address);
printf("添加完成\n");
}
void del(struct Address_Lsit * AL)//----------------------删除指定联系人信息
{
int flag = 0;//标志位
char w_name[20] = { 0 };
printf("请输入删除联系人的姓名\n");
scanf("%s", w_name);
for (int i = 0; i < People_num_all; i++)
{
if (!(strcmp((AL + i)->name, w_name)))
{
flag = 1;//找到了
if (i != People_num_all)
exchange((AL + i), (AL + People_num_all - 1));//和最后一个交换
break;
}
}
if (flag)
{
printf("删除完成\n");
People_num_all--;//总人数减一
}
else
{
printf("没有找到\n");
}
}
void find(struct Address_Lsit * AL)//---------------------------------查找指定联系人信息
{
char w_name[20] = { 0 };
printf("请输入要查找联系人的姓名\n");
scanf("%s", w_name);
for (int i = 0; i < People_num_all; i++)
{
if (!(strcmp((AL + i)->name, w_name)))
{
printf("已找到:\n");
show((AL + i));
return 0;
}
}
printf("不存在该联系人\n");
}
void amend(struct Address_Lsit * AL)//-----------------------修改指定联系人信息
{
char w_name[20] = { 0 };
printf("请输入要修改联系人的姓名\n");
scanf("%s", w_name);
for (int i = 0; i < People_num_all; i++)
{
if (!(strcmp((AL + i)->name, w_name)))
{
AL += i;
printf("请输入:新姓名\n");
scanf("%s", AL->name);
printf("请输入:新性别\n");
scanf("%s", AL->sex);
printf("请输入:新年龄\n");
scanf("%d", &(AL->age));
printf("请输入:新电话\n");
scanf("%d", &(AL->phone_num));
printf("请输入:新地址\n");
scanf("%s", AL->Address);
printf("添加完成\n");
return 0;
}
}
printf("不存在该联系人\n");
}
void show_all(struct Address_Lsit * AL)//显示所有联系人信息
{
for (int i = 0; i < People_num_all; i++)
{
show(AL + i);
}
printf("显示完成\n");
}
void empty(struct Address_Lsit * AL)//清空所有联系人
{
People_num_all = 0;
printf("完成清空\n");
}
void sort(struct Address_Lsit * AL)//以名字排序所有联系人
{
int i = 0;
int j = 0;
int k = 0;
char exname[20] = { 0 };
for (i = 0; i < People_num_all - 1; i++)
{
k = 0;
strcpy(exname, (AL + i)->name);
for (j = i + 1, k = i; j < People_num_all; j++)
{
if (strcmp((AL + j)->name, exname) < 0)
{
k = j;
strcpy(exname, (AL + j)->name);
}
}
if (k)
{
exchange((AL + i), (AL + k));
}
}
printf("排序完成\n");
}
void operation(int x, struct Address_Lsit * AL)
{
switch (x)
{
case 1: add(AL); break;
case 2: del(AL); break;
case 3: find(AL); break;
case 4: amend(AL); break;
case 5: show_all(AL); break;
case 6: empty(AL); break;
case 7: sort(AL); break;
case 0: break;
default: printf("请重新输入");
}
}
void extend(struct Address_Lsit **ALp)
{
struct Address_Lsit *p = NULL;
p = realloc(*ALp, (People_num_all + 1) * sizeof(struct Address_Lsit));
if (NULL != p)
{
*ALp = p;
}
}
int main()
{
int x = 1;
struct Address_Lsit *AL = NULL;
while (x)
{
menu();
scanf("%d", &x);
if( x == 1 || x == 2)
{
extend(&AL);
}
operation(x, AL);
Sleep(500);
}
system("pause");
free(AL);
return 0;
}