深度解析通讯录动态内存管理版本

通讯录动态管理版本将静态版本的结构体数组替换为动态开辟空间的结构体,该结构体指针指向被开辟的空间,当空间不够时,使用realloc进行增加

关键部分:

1、动态的结构体

struct contacts
{
	char name[MAX_NAME];
	char sex[MAX_SEX];
	int age;
	char tel[MAX_TEL];
	char address[MAX_ADDRESS];
};
struct con
{
	struct contacts *pelist;//因为使用malloc,所以需要指针
	int count;//人数记录,记录空间中存储了多少的信息
	int memory;//内存容量,表示pelist指向的空间大小,如果与count相等,则需要扩容;
};

 该结构体中,使用了两个变量count和memory,直接理解为人员信息和内存空间

深度解析通讯录动态内存管理版本_第1张图片

 2、需要扩容的位置,使用时只有新增人员信息时才会涉及到增加

void add(struct con* conlist)
{
	assert(conlist);
	if (conlist->count == conlist->memory)
	{
		struct contacts *pf = (struct contacts *)realloc(conlist->pelist, (conlist->memory + 2)*sizeof(struct contacts));
		if (pf == NULL)
		{
			perror("realloc error");
			SYS;
			return 1;
		}
		conlist->pelist = pf;
		conlist->memory += 2;
		printf("扩容成功\n");
		
	}//如果容量已满,则需要扩容
	printf("please input name->:");
	scanf("%s", (conlist->pelist + conlist->count)->name);
	printf("please input sex->:");
	scanf("%s", (conlist->pelist + conlist->count)->sex);
	printf("please input age->:");
	scanf("%d", &((conlist->pelist + conlist->count)->age));//这里为什么加取地址
	printf("please input tel->:");
	scanf("%s", (conlist->pelist + conlist->count)->tel);
	printf("please input address->:");
	scanf("%s", (conlist->pelist + conlist->count)->address);
	conlist->count++;
}

所以,在新增人员信息时,要对memory和count进行比较,如果两个参数相等时,表示需要扩容

3、对内存的释放

退出程序之前,需要释放malloc开辟的空间

void freecontact(struct con*conlist)
{
	free(conlist->pelist);
	conlist->pelist = NULL;
}

下面是完整的函数

头文件部分

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#define SYS system("pause")
#define R0 return 0
#include 

#define MAX_NAME 30
#define MAX_SEX 5
#define MAX_TEL 11
#define MAX_ADDRESS 50
#define MAX_PELIST 100


struct contacts
{
	char name[MAX_NAME];
	char sex[MAX_SEX];
	int age;
	char tel[MAX_TEL];
	char address[MAX_ADDRESS];
};

//struct con
//{
//	struct contacts pelist[MAX_PELIST];
//	int count;
//};
//该结构体为静态版本

struct con
{
	struct contacts *pelist;//因为使用malloc,所以需要指针
	int count;//人数记录,记录空间中存储了多少的信息
	int memory;//内存容量,表示pelist指向的空间大小,如果与count相等,则需要扩容;
};
void add(struct con* conlist);
void init(struct con*conlist);
void modify(struct con*conlist);
void print(struct con*conlist);
void search(struct con*conlist);
void del(struct con*conlist);
void sort(struct con*conlist);
void freecontact(struct con*conlist);

 主体部分

#include"contact.h"

void menu()
{
	printf("----------------------\n");
	printf("--------通 讯 录------\n");
	printf("--------1、新增-------\n");
	printf("--------2、修改-------\n");
	printf("--------3、删除-------\n");
	printf("--------4、查找-------\n");
	printf("--------5、打印-------\n");
	printf("--------6、排序-------\n");
	printf("--------0、退出-------\n");
	printf("----------------------\n");
}


int main()
{
	struct con conlist;
	void(*fun[7])(struct con *conlist) = { 0, add, modify, del, search, print, sort };
	int input = 1;
	//初始化函数
	init(&conlist);
	//创建函数数组
	menu();
	while (input)
	{
		printf("enter options->");
		scanf("%d", &input);
		if (input == 0)
		{
			freecontact(&conlist);
			printf("exit contacts\n");
			break;
		}
		else if (input > 0 && input <= 6)
		{
			fun[input](&conlist);
		}
		else
		{
			printf("input error ");
			printf("please re-enter.\n");
		}
	}
	SYS;
	R0;
}

 函数部分

#include"contact.h"

//struct contacts
//{
//	char name[MAX_NAME];
//	char sex[MAX_SEX];
//	int age;
//	char tel[MAX_TEL];
//	char address[MAX_ADDRESS];
//};


//搜索排序名字
static int cmp_name(const void*e1, const void *e2)
{
	return strcmp(((struct contacts *)e1)->name, ((struct contacts *)e2)->name);
}

//搜索年龄排序
static int cmp_age(const void*e1, const void *e2)
{
	return ((struct contacts *)e1)->age - ((struct contacts *)e2)->age;
}


//搜索函数
static int findname(struct con*conlist, char *name)
{
	int i = 0;
	while (i < (conlist->count))
	{
		if (strcmp(name, conlist->pelist[i].name) == 0)
		{
			return i;
		}
		i++;
	}
	return -1;
}

//初始化通讯录静态版本
//void init(struct con*conlist)
//{
//	assert(conlist);
//	conlist->count = 0;
//	memset(conlist->pelist, 0, sizeof(conlist->pelist));
//}
//初始化通讯录动态版本
void init(struct con*conlist)
{
	assert(conlist);
	conlist->count = 0;
	conlist->pelist = (struct contacts *)malloc(3 * sizeof(struct contacts ));
	if (conlist->pelist == NULL)
	{
		perror("malloc pelist error");
		SYS;
		return 1;
	}
	conlist->memory = 3;//首先初始化3个空间

}

//新增通讯录内容
void add(struct con* conlist)
{
	assert(conlist);
	if (conlist->count == conlist->memory)
	{
		struct contacts *pf = (struct contacts *)realloc(conlist->pelist, (conlist->memory + 2)*sizeof(struct contacts));
		if (pf == NULL)
		{
			perror("realloc error");
			SYS;
			return 1;
		}
		conlist->pelist = pf;
		conlist->memory += 2;
		printf("扩容成功\n");
		
	}//如果容量已满,则需要扩容
	printf("please input name->:");
	scanf("%s", (conlist->pelist + conlist->count)->name);
	printf("please input sex->:");
	scanf("%s", (conlist->pelist + conlist->count)->sex);
	printf("please input age->:");
	scanf("%d", &((conlist->pelist + conlist->count)->age));//这里为什么加取地址
	printf("please input tel->:");
	scanf("%s", (conlist->pelist + conlist->count)->tel);
	printf("please input address->:");
	scanf("%s", (conlist->pelist + conlist->count)->address);
	conlist->count++;
}


//修改通讯录
void modify(struct con*conlist)
{
	assert(conlist);
	char name[MAX_NAME];
	if (conlist->count == 0)
	{
		printf("通讯录没有内容\n");
		return;
	}
	printf("search name\n");
	scanf("%s", name);
	int ret = findname(conlist, name);
	if (ret != -1)
	{
		printf("please input name->:");
		scanf("%s", (conlist->pelist + conlist->count)->name);
		printf("please input sex->:");
		scanf("%s", (conlist->pelist + conlist->count)->sex);
		printf("please input age->:");
		scanf("%d", &((conlist->pelist + conlist->count)->age));//这里为什么加取地址
		printf("please input tel->:");
		scanf("%s", (conlist->pelist + conlist->count)->tel);
		printf("please input address->:");
		scanf("%s", (conlist->pelist + conlist->count)->address);
	}
	return;

}

//打印
void print(struct con*conlist)
{
	int i = 0;
	printf("%30s%5s%5s%11s%50s\n", "name", "sex", "age", "tel", "address");
	while (i < conlist->count)
	{
		printf("%30s%5s%5d%11s%50s\n", conlist->pelist[i].name, conlist->pelist[i].sex, conlist->pelist[i].age, conlist->pelist[i].tel, conlist->pelist[i].address);
		i++;
	}
}

//查找通讯录
void search(struct con*conlist)
{
	assert(conlist);
	char name[30];
	printf("enter name\n");
	scanf("%s", name);
	int ret = findname(conlist, name);
	if (ret != -1)
	{
		printf("%30s%5s%5s%11s%50s\n", "name", "sex", "age", "tel", "address");
		printf("%30s%5s%5d%11s%50s\n", conlist->pelist[ret].name, conlist->pelist[ret].sex, conlist->pelist[ret].age, conlist->pelist[ret].tel, conlist->pelist[ret].address);

	}
	else
	{
		printf("no info to return\n");
	}
}

//删除通讯录
void del(struct con*conlist)
{
	assert(conlist);
	char name[30];
	printf("enter name\n");
	scanf("%s", name);
	int ret = findname(conlist, name);
	if (ret != -1)
	{
		while (ret < conlist->count)
		{
			conlist->pelist[ret] = conlist->pelist[ret + 1];
			ret++;
		}
		printf("erasing end\n");
		conlist->count--;
	}
	else
	{
		printf("del error\n");
	}
}

void sort(struct con*conlist)
{
	int i = 0;
	while (i < conlist->count)
	{
		qsort(conlist->pelist, conlist->count, sizeof(struct contacts), cmp_name);
		i++;
	}
}

void freecontact(struct con*conlist)
{
	free(conlist->pelist);
	conlist->pelist = NULL;
}

 

 

 

你可能感兴趣的:(c语言,开发语言)