顺序表的应用——通讯录

通讯录的实现分为五个文件分别进行编写,分别为:SeqList.c,SeqList.h,Contact.c,Contact.h,test.c
其中前两个文件为上一篇博客中的顺序表的操作后三个文件为通讯录功能的实现

  • SeqList.h
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef CInfo SLDataType;//将数据类型改变为struct,从而实现通讯录的存储效果
typedef struct SeqList
{
	SLDataType* a;
	int size;
	int capacity;
}SL;

void SLInit(SL* ps);//顺序表的初始化

void SLDestroy(SL* ps);//顺序表的销毁

void SLPushBack(SL* ps, SLDataType x);//顺序表的尾插

void SLPushFront(SL* ps, SLDataType x);//顺序表的头插

void SLPopBack(SL* ps);//顺序表的尾删

bool SLIsEmpty(SL* ps);//判断顺序表是否为空

void SLPopFront(SL* ps);顺序表的头删

void SLInsert(SL* ps, int pos, SLDataType x);//顺序表任意位置的插入

void SLErase(SL* ps, int pos);//顺序表任意位置的删除
  • SeqList.c
#include"SeqList.h"

void SLInit(SL* ps)
{
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

void SLDestroy(SL* ps)
{
	if (ps->a)
		free(ps);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	int newcapacity = ps->capacity == 0 ? 4 : ps->capacity;
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newcapacity * 2 * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("errno\n");
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
	ps->a[ps->size] = x;
	ps->size++;
}

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("errno\n");
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
	for (size_t i = ps->size; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}

bool SLIsEmpty(SL* ps)
{
	assert(ps);
	return ps->size == 0;
}

void SLPopBack(SL* ps)
{
	assert(ps);
	assert(SLIsEmpty(ps));
	ps->size--;
}

void SLPopFront(SL* ps)
{
	assert(ps);
	assert(SLIsEmpty(ps));
	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);
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("errno\n");
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
	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(!SLIsEmpty(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--;
}
  • test.c
#include"SeqList.h"
#include"Contact.h"

void menu()
{
	printf("*************     通讯录     ************\n");
	printf("******1.增加联系人    2.删除联系人*******\n");
	printf("******3.修改联系人    4.查找联系人*******\n");
	printf("******5.查看通讯录    0.退出      *******\n");
	printf("*****************************************\n");
}

int main()
{
	contact con;
	ContactInit(&con);
	int input = 0;
	do
	{
		menu();
		printf("请输入操作的编号:\n");
	    scanf("%d",&input);
		switch (input)
		{
			case 1:
				ContactAdd(&con);
				break;
			case 2:
				ContactDel(&con);
				break;
			case 3:
				ContcatModify(&con);
				break;
			case 4:
				Contactfind(&con);
				break;
			case 5:
				Contactshow(&con);
				break;
			case 0:
				printf("已退出\n");
				break;
			default:
				printf("请输入正确的操作序号\n");
				break;
		}
	} while (input);
	return 0;
}

打印目录,让使用通讯录的人通过输入的数字来实现相对应的操作
在这里插入图片描述

  • Contact.h
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 20
#define MAX_ADDR 20

typedef struct ContactInfo
{
	char name[MAX_NAME];
	char sex[MAX_SEX];
	int age;
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
}CInfo;

typedef struct SeqList contact;

void ContactInit(contact* pcon);//通讯录的初始化

void ContactDestory(contact* pcon);//通讯录的销毁

void ContactAdd(contact* pcon);//通讯录联系人的增加

void ContactDel(contact* pcon);//通讯录联系人的删除

void ContcatModify(contact* pcon);//通讯录联系人的修改

void Contactshow(contact* pcon);//通讯录联系人的展示

void Contactfind(contact* pcon);//通讯录联系人的查找
  • Contact.c
#include"Contact.h"
#include"SeqList.h"

void ContactInit(contact* pcon)
{
	SLInit(pcon);
}

通讯录的初始化操作同顺序表的初始化操作

void ContactDestory(contact* pcon)
{
	SLDestroy(pcon);
}

通讯录的销毁操作同顺序表的销毁操作

void ContactAdd(contact* pcon)
{
	CInfo info;
	printf("请输入联系人的姓名:\n");
	scanf("%s",info.name);
	printf("请输入联系人的性别:\n");
	scanf("%s", info.sex);
	printf("请输入联系人的年龄:\n");
	scanf("%d", &info.age);
	printf("请输入联系人的电话:\n");
	scanf("%s", info.tele);
	printf("请输入联系人的住址:\n");
	scanf("%s", info.addr);
	SLPushBack(pcon, info);
}

通讯录增加联系人的操作需要提前将联系人的信息存储到一个新建的结构体中,再将结构体尾插入通讯录中

int FindByName(contact* pcon, char name[])
{
	for (int i = 0; i < pcon->size; i++)
	{
		if (strcmp(pcon->a[i].name, name) == 0)
			return i;
	}
	return -1;
}

查找联系人是否存在的操作(以姓名为例)只需将联系人的姓名与待查找人的姓名进行对比即可

void ContactDel(contact* pcon)
{
	printf("请输入要删除联系人的姓名\n");
	char input[MAX_NAME];
	scanf("%s",input);
	int find = FindByName(pcon, input);
	if (find < 0)
	{
		printf("待删除的联系人不存在\n");
		return;
	}
	SLErase(pcon, find);
}
  1. 查找联系人是否存在
  2. 删除联系人
void ContcatModify(contact* pcon)
{
	printf("请输入要修改的联系人的姓名\n");
	char input[MAX_NAME];
	scanf("%s", input);
	int find = FindByName(pcon, input);
	if (find < 0)
	{
		printf("待修改的联系人不存在\n");
		return;
	}
	printf("请输入修改后的联系人姓名\n");
	scanf("%s",pcon->a[find].name);
	printf("请输入修改后的联系人性别\n");
	scanf("%s",pcon->a[find].sex);
	printf("请输入修改后的联系人年龄\n");
	scanf("%d",&pcon->a[find].age);
	printf("请输入修改后的联系人电话\n");
	scanf("%s", pcon->a[find].tele);
	printf("请输入修改后的联系人地址\n");
	scanf("%s", pcon->a[find].addr);
	printf("修改成功\n");
}
  1. 查找联系人是否存在
  2. 修改联系人的信息
void Contactshow(contact* pcon)
{
	printf("%s %s %s %s %s\n","姓名","性别","年龄","电话","住址");
	for (int i = 0; i < pcon->size; i++)
	{
		printf("%-4s %-4s %-4d %-4s %-4s\n",
			pcon->a[i].name,
			pcon->a[i].sex,
			pcon->a[i].age,
			pcon->a[i].tele,
			pcon->a[i].addr
		);
	}
}

从头遍历顺序表,并对数据进行打印

void Contactfind(contact* pcon)
{
	printf("请输入待查找的联系人姓名\n");
	char input[MAX_NAME];
	scanf("%s",input);
	int find = FindByName(pcon, input);
	if (find < 0)
	{
		printf("待查找的联系人不存在\n");
		return;
	}
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "住址");
	printf("%-4s %-4s %-4d %-4s %-4s\n",
			pcon->a[find].name,
			pcon->a[find].sex,
			pcon->a[find].age,
			pcon->a[find].tele,
			pcon->a[find].addr
		);
}
  1. 查找联系人是否存在
  2. 存在则输出相关联系人数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值