数据结构2:基于顺序表的通讯录项目

头文件

SeqList.h

#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"

#define INIT_CAPACITY 4

//将顺序表数据类型调整为联系人信息
typedef PersonInfo SLDataType;
//typedef int SLDataType;

//动态顺序表 -- 按需申请
typedef struct SeqList {
	SLDataType* a;
	int size;
	int capacity;
}SL;

//打印
void SLPrint(SL* ps);

//初始化和销毁
void SLInit(SL* ps);
void SLDestory(SL* ps);

//扩容
void SLCheckCapacity(SL* ps);

//头部插入删除 / 尾部插入删除
void SLPushBack(SL* ps, SLDataType x);
void SLPushFront(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPopFront(SL* ps);

//查找
void SLInsert(SL* ps, int pos, SLDataType x);

//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);

Contact.h

#pragma once


#define NAME_MAX 100
#define GENDER_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100

//将顺序表重命名为通讯录
typedef struct SeqList Contact;
//这里的struct SeqList是前置声明,声明完之后再进行重命名
//因为这个文件中并没有定义struct SeqList,而又不能包含"SeqList.h"头文件
//因为本文件已经被该头文件包含,所以只能采取前置声明的方式


//定义用户数据类型
typedef struct PersonInfo {
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}PersonInfo;

//通讯录初始化
void ContactInit(Contact* con);

//通讯录销毁
void ContactDestory(Contact* con);

//添加联系人
void ContactPushBack(Contact* con);

//展示通讯录
void ContactPrint(Contact* con);

//删除联系人
void ContactDele(Contact* con);

//修改联系人
void ContactModify(Contact* con);

//查找联系人
void ContactFind(Contact* con);

//读取文件
void LoadContact(Contact* con);

//保存文件
void SaveContact(Contact* con);

实现文件

SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"SeqList.h"

//需屏蔽,因为数据类型已改变
//打印
//void SLPrint(SL* ps)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		printf("%d ", ps->a[i]);
//	}
//	printf("\n");
//}

//初始化
void SLInit(SL* ps)
{
	ps->size = 0;
	ps->capacity = INIT_CAPACITY;
	SLDataType* arr = (SLDataType*)malloc(sizeof(SLDataType) * INIT_CAPACITY);
	if (arr == NULL) {
		perror("malloc fail!\n");
		exit(1);
	}
	else {
		ps->a = arr;
		arr = NULL;
	}
}

//销毁
void SLDestory(SL* ps)
{
	assert(ps);
	//如果顺序表为非空链表,就将ps->a的空间释放
	if(ps->a)
		free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

//判断是否需要扩容,如果需要则进行扩容
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		ps->capacity *= 2;
		SLDataType* arr = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * (ps->capacity));
		if (arr == NULL)
		{
			perror("realloc fail!\n");
			exit(1);
		}
		ps->a = arr;
	}
}

//尾插
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}

//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size - 1; i >= 0; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[0] = x;
	ps->size++;
}

//尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	ps->size--;
}

//头删
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	for (int i = 0; i < ps->size - 1; i++) {
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

//找到某元素第一次出现的位置
//int SLFind(SL* ps, SLDataType x)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->a[i] == x)
//			return i;
//	}
//	return -1;
//}

//指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos <= ps->size && ps >= 0);
	SLCheckCapacity(ps);
	for (int i = ps->size - 1; i >= pos; i--)
	{
		ps->a[i + 1] = ps->a[i];
	}
	ps->a[pos] = x;
	ps->size++;
}

//删除指定位置的数据
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos <= ps->size && ps >= 0);
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

Contact.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"SeqList.h"
#include"Contact.h"
#include<string.h>

//通讯录初始化
void ContactInit(Contact* con)
{
	SLInit(con);
}

//通讯录销毁
void ContactDestory(Contact* con)
{
	SLDestory(con);
}

//添加联系人
void ContactPushBack(Contact* con)
{
	//创建联系人结构
	PersonInfo a;
	printf("情输入联系人姓名:\n");
	scanf("%s", &(a.name));
	printf("情输入联系人性别:\n");
	scanf("%s", &(a.gender));
	printf("情输入联系人电话号码:\n");
	scanf("%s", &(a.tel));
	printf("情输入联系人地址:\n");
	scanf("%s", &(a.addr));

	//尾插
	SLPushBack(con, a);
}

//展示通讯录
void ContactPrint(Contact* con)
{
	assert(con);
	if (con->size == 0) {
		printf("通讯录为空。\n");
		return;
	}
	printf("姓名   性别   电话   地址\n");
	for (int i = 0; i < con->size; i++)
	{
		printf("%s ", con->a[i].name);
		printf("%s ", con->a[i].gender);
		printf("%s ", con->a[i].tel);
		printf("%s ", con->a[i].addr);
		printf("\n");
	}
}

//删除联系人
void ContactDele(Contact* con)
{
	char name[NAME_MAX] = { 0 };
	printf("请输入要删除的联系人姓名:");
	scanf("%s", name);
	int dele = 0;
	for (int i = 0; i < con->size; i++)
	{
		if (strcmp(con->a[i].name, name) == 0)
		{
			SLErase(con, i);
			dele = 1;
			printf("删除成功!\n");
			break;
		}
	}
	if(dele == 0)
		printf("联系人信息不存在!\n");
}

//修改联系人
void ContactModify(Contact* con)
{
	assert(con);
	char name[NAME_MAX];
	printf("请输入要修改的联系人姓名:");
	scanf("%s", name);
	for (int i = 0; i < con->size; i++)
	{
		if (strcmp(con->a[i].name, name) == 0)
		{
			printf("情输入新的联系人姓名:\n");
			scanf("%s", &(con->a[i].name));
			printf("情输入联系人性别:\n");
			scanf("%s", &(con->a[i].gender));
			printf("情输入联系人电话号码:\n");
			scanf("%s", &(con->a[i].tel));
			printf("情输入联系人地址:\n");
			scanf("%s", &(con->a[i].addr));
			printf("修改成功!\n");
			return;
		}
	}
	printf("无此联系人信息!\n");
}

//查找联系人
void ContactFind(Contact* con)
{
	assert(con);
	char name[NAME_MAX];
	printf("请输入要查找的联系人姓名:");
	scanf("%s", name);
	for (int i = 0; i < con->size; i++)
	{
		if (strcmp(con->a[i].name, name) == 0)
		{
			printf("姓名   性别   电话   地址\n");
			printf("%s ", con->a[i].name);
			printf("%s ", con->a[i].gender);
			printf("%s ", con->a[i].tel);
			printf("%s ", con->a[i].addr);
			printf("\n");
			return;
		}
	}
	printf("无此联系人信息!\n");
	return;
}

//读取文件
void LoadContact(Contact* con)
{
	FILE* pf = fopen("contact.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen fail!\n");
		exit(1);
	}
	else {
		PersonInfo tmp;
		while (fread(&tmp, sizeof(PersonInfo), 1, pf))
		{
			SLPushBack(con, tmp);
		}
	}
	fclose(pf);
	pf = NULL;
	printf("读取成功!\n");
}

//保存文件
void SaveContact(Contact* con)
{
	FILE* pf = fopen("contact.txt", "wb");
	if (pf == NULL)
	{
		perror("fopen fail!\n");
		exit(1);
	}
	else {
		for (int i = 0; i < con->size; i++)
		{
			fwrite(&(con->a[i]), sizeof(PersonInfo), 1, pf);
		}
	}
	fclose(pf);
	pf = NULL;
	printf("保存成功!\n");
	return;
}

测试文件

text.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"SeqList.h"
#include"Contact.h"

void menu()
{
	printf("*************************************************\n");
	printf("*************1.增加联系人  2.删除联系人**********\n");
	printf("*************3.修改联系人  4.查找联系人**********\n");
	printf("*************5.打印通讯录  0.退出****************\n");
	printf("*************************************************\n");
	printf("请选择要进行的操作:\n");
}

//测试通讯录
void Test2(Contact* con)
{
	int input = 0;
	do {
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			ContactPushBack(con);
			break;
		case 2:
			ContactDele(con);
			break;
		case 3:
			ContactModify(con);
			break;
		case 4:
			ContactFind(con);
			break;
		case 5:
			ContactPrint(con);
		default:
			break;
	}
	} while (input);
	printf("正在退出...\n");
}

int main()
{
	//Test1();
	Contact a;
	Contact* con = &a;
	//初始化
	ContactInit(con);

	//读取数据
	LoadContact(con);

	Test2(con);

	//保存数据
	SaveContact(con);

	//销毁
	ContactDestory(con);
	return 0;
}
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值