顺序表实现通讯录

头文件

lude<string.h>
//前置声明
typedef struct SeqList contact;
//用户数据
typedef struct PersonInfo{
	char name[NAME_MAX];
	int age;
	char gender[GENDER_MAX];
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}Info;
typedef Info SLDataType;
typedef struct SeqList
{
	SLDataType* a;
	int size;     // 有效数据个数
	int capacity; // 空间容量
}SL;
//初始化通讯录
void InitContact(contact* con);
//添加通讯录数据
void AddContact(contact* con);
//删除通讯录数据
void DelContact(contact* con);
//展示通讯录数据
void ShowContact(contact* con);
//查找通讯录数据
void FindContact(contact* con);
//修改通讯录数据
void ModifyContact(contact* con);
//销毁通讯录数据
void DestroyContact(contact* con);
//初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
//扩容
void SLCheckCapacity(SL* ps);
//头部插入删除 / 尾部插入删除
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
void SLFind(SL* ps, int x);

函数

#define _CRT_SECURE_NO_WARNINGS 1
#include "qwe.h"
void SLInit(SL* ps) {
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
//销毁
void SLDestroy(SL* ps) {
	assert(ps);
	if (ps->a) {
		free(ps->a);
	}
	ps->a = NULL;
	ps->size = ps->capacity - 0;
}
//打印
void SLPrint(SL* ps) {
	for (int i = 0; i < ps->size; i++) {
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
//扩容
void SLCheckCapacity(SL* ps) {
	if (ps->size == ps->capacity) {
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* qwe = (SLDataType*)realloc(ps->a, newcapacity * sizeof(SLDataType));
		if (qwe == NULL) {
			perror("reacoll fail!");
			exit(0);
		}
		ps->a = qwe;
		ps->capacity = newcapacity;
	}
}
//尾插
void SLPushBack(SL* ps, SLDataType x) {
	assert(ps);
	SLCheckCapacity(ps);
	ps->a[ps->size++] = x;
}
//尾删
void SLPopBack(SL* ps) {
	assert(ps);
	assert(ps->size);
	ps->size--;
}
//头插
void SLPushFront(SL* ps, SLDataType x) {
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > 0; i--) {
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}
//头删
void SLPopFront(SL* ps) {
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size - 1; i++) {
		ps->a[i] = ps->a[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->a[i] = ps->a[i - 1];
	}
	ps->a[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->a[i] = ps->a[i + 1];
	}
	ps->size--;
}
//查找
void SLFind(SL* ps, int x) {
	assert(ps);
	assert(x >= 0 && x <= ps->size);
	printf("%d", ps->a[x]);
}
int FindByName(SL* con, char name[]) {
	for (int i = 0; i < con->size; i++) {
		if (strcmp(con->a[i].name, name) == 0)
			return i;
	}
	return -1;
}
//初始化通讯录
void InitContact(contact* con) {
	SLInit(con);
}
//添加通讯录数据
void AddContact(contact* con) {
	Info info;
	printf("姓名");
	scanf("%s", info.name);
	printf("住址");
	scanf("%s", info.addr);
	printf("年龄");
	scanf("%d", &info.age);
	printf("性别");
	scanf("%s", info.gender);
	printf("电话");
	scanf("%s", info.tel);
	SLPushBack(con, info);
}
//删除通讯录数据
void DelContact(contact* con) {
	char name[NAME_MAX];
	printf("输入要删除的用户名");
	scanf("%s", name);
	int find = FindByName(con, name);
	if (find < 0) {
		printf("错误");
		return;
	}
	SLErase(con, find);
	printf("删除成功\n");
}
//展示通讯录数据
void ShowContact(contact* con) {
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "住址");

	for (int i = 0; i < con->size; i++)
	{
		printf("%s %s %d %s %s\n",
			con->a[i].name,
			con->a[i].gender,
			con->a[i].age,
			con->a[i].tel,
			con->a[i].addr
		);
	}
}
//查找通讯录数据
void FindContact(contact* con) {
	char name[NAME_MAX];
	printf("输入要查找的用户名");
	scanf("%s",name);
	int find = FindByName(con,name);
	if (find < 0) {
		printf("错误");
		return;
	}
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "住址");
	printf("%s %s %d %s %s\n",
		con->a[find].name,
		con->a[find].gender,
		con->a[find].age,
		con->a[find].tel,
		con->a[find].addr);
}
//修改通讯录数据
void ModifyContact(contact* con) {
	char name[NAME_MAX];
	printf("输入要修改的用户名");
	scanf("%s", name);
	int find = FindByName(con, name);
	if (find < 0) {
		printf("错误");
		return;
	}
	printf("请输入姓名:\n");
	scanf("%s", con->a[find].name);
	printf("请输入年龄:\n");
	scanf("%d", &con->a[find].age);
	printf("请输入性别:\n");
	scanf("%s", con->a[find].gender);
	printf("请输入电话:\n");
	scanf("%s", con->a[find].tel);
	printf("请输入地址:\n");
	scanf("%s", con->a[find].addr);
	printf("联系人修改成功!\n");
}
//销毁通讯录数据
void DestroyContact(contact* con) {
	SLDestroy(con);
}

测试

#define _CRT_SECURE_NO_WARNINGS 1
#include "qwe.h"
void menu() {
	printf("*****************通讯录***************\n");
	printf("*******1.添加联系人  2.删除联系人*****\n");//ctrl+d
	printf("*******3.修改联系人  4.查找联系人*****\n");//ctrl+d
	printf("*******5.查看通讯录  0.  退 出  ******\n");//ctrl+d
	printf("**************************************\n");
}
int main() {
	SL con;
	int qwe = 0;
	menu();
	InitContact(&con);

	
	do {
		scanf("%d",&qwe);
		switch (qwe)
		{
		case 1:
			AddContact(&con);
			break;
		case 2:
			DelContact(&con);
			break;
		case 3:
			ModifyContact(&con);
			break;
		case 4:
			FindContact(&con);
			break;
		case 5:
			ShowContact(&con);
			break;
		default:
			break;
		}
	} while (qwe != 0);
	DestroyContact(&con);
	return 0;
}

效果大概像这样

 界面输出封面没仔细弄,有需要的自己要改下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值