顺序表之通讯录教学

通讯录是基于顺序表的基础上实现的。顺序表包括内容定义,初始化,插入或删除数据,查找数据。相同的就可以转移到通讯录中的添加联系人,删除联系人,查找联系人,修改联系人。代码细节有注释讲解。

运行效果:

在这里插入图片描述

顺序表基础头文件

#pragma once
#include"Contact.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

//动态顺序表
//typedef char SLDataType;
// 
//更改数据类型为通讯录数据类型
//以下的方式任意选择即可
typedef struct ContactInfo SLDataType;
//typedef CInfo SLDataType;

typedef struct SeqList
{
	SLDataType* a;
	int size;  //顺序表中有效的数据个数
	int capacity;  //顺序表当前的空间大小
}SL;
//typedef struct SeqList 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);
void SLPopFront(SL* ps);

void SLPrint(SL* ps);
bool SLIsEmpty(SL* ps);

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

bool SLFind(SL* ps, SLDataType x);

顺序表内容文件

#include"SeqList.h"

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

void SLCheckCapacity(SL* ps) {
	if (ps->size == ps->capacity) {
		//空间不足以再额外插入一个数据
		//扩容
		int newCapcity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapcity * sizeof(SLDataType));
		if (tmp == NULL) {
			perror("realloc fail!\n");
			return 1;
		}
		ps->a = tmp;
		ps->capacity = newCapcity;
	}
}

//尾插
void SLPushBack(SL* ps, SLDataType x) {
	//assert(ps != NULL);
	//暴力的方式
	assert(ps);
	//柔和方式
	//if (ps == NULL) {
	//	return;
	//}

	//1)空间足够,直接尾插
	//2)空间不够,扩容
	SLCheckCapacity(ps);
	//直接插入数据
	ps->a[ps->size++] = x;
}
//头插
void SLPushFront(SL* ps, SLDataType x) {
	assert(ps);
	//判断空间是否足够,不够则扩容
	SLCheckCapacity(ps);
	//空间足够,历史数据后移一位
	for (size_t i = ps->size; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}
//尾插
void SLPopBack(SL* ps) {
	//判断顺序表是否为空
	assert(ps);
	assert(!SLIsEmpty(ps));
	//ps->a[ps->size - 1] = 0;
	ps->size--;
}
void SLPopFront(SL* ps) {
	assert(ps);
	assert(!SLIsEmpty(ps));
	//让后面的数据往前挪动一位
	for (size_t i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}
void SLPrint(SL* ps) {
	for (size_t i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
bool SLIsEmpty(SL* ps) {
	assert(ps);
	//这样子是不对的,这里只能判断空间是否足够
	//return ps->size == ps->capacity;
	return ps->size == 0;
}

//在指定的位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x) {
	assert(ps);
	//不要忘了对pos加以限制
	assert(pos >= 0 && pos <= ps->size);
	//扩容
	SLCheckCapacity(ps);
	//把pos位置及以后的数据往后挪动一位
	//循环条件里i的初始值是size还是size-1都是可以的,但不同的初始值对应不同的结束条件
	//for (int i = ps->size; i > pos; i--)
	//{
	//	//pos+1
	//	ps->a[i] = ps->a[i - 1];//pos->a[pos+1] = ps->a[pos]
	//}
	for (int i = ps->size - 1; i > pos - 1; i--)
	{
		//最后一次进来的i是pos
		ps->a[i + 1] = ps->a[i];//ps->a[pos+1] = ps->a[pos]
	}
	ps->a[pos] = x;
	ps->size++;
}
//删除指定位置的数据
void SLErase(SL* ps, int pos) {
	assert(ps);
	assert(!SLIsEmpty(ps));
	//pos的范围需要限制
	assert(pos >= 0 && pos < ps->size);

	for (int i = pos; i < ps->size - 1; i++)
	{
		//最后一次进来的i的数据ps->size-2
		ps->a[i] = ps->a[i + 1];//ps->a[ps->size-2] = ps->a[ps->size-1]
	}
	ps->size--;
}

//bool SLFind(SL* ps, SLDataType x) {
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->a[i].name == x) {
//			//找到了
//			return true;
//		}
//	}
//	return false;
//}

通讯录头文件

#pragma once
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
//创建保存联系人数据的结构
#define NAME_MAX 100
#define SEX_MAX 10
#define TEL_MAX  15
#define ADDR_MAX 100

struct ContactInfo
{
	char name[NAME_MAX];//使用定长数组还是动态数组呢?,字符数组,数组名就是数组的地址
	char sex[SEX_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
};
typedef struct ContactInfo CInfo;

//通讯录底层是顺序表来实现
typedef struct SeqList contact;

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

//添加联系人
void ContactAdd(contact* pcon);
//删除联系人
void ContactDel(contact* pcon);
//修改联系人
void ContactModify(contact* pcon);
//查看通讯录
void ContactShow(contact* pcon);
//查找指定联系人
void ContactFind(contact* pcon);

通讯录内容文件

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

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

//添加联系人
void ContactAdd(contact* pcon) {
	//接下来要获取的信息都是CInfo结构体里要求的数据
	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.tel);
	printf("请输入联系人的住址:\n");
	scanf("%s", info.addr);

	//联系人数据都获取到了,并保存到了结构体info中
	//往通讯录(顺序表)中插入数据
	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 name[NAME_MAX];
	scanf("%s", name);
	int findidex = FindByName(pcon, name);
	if (findidex < 0) {
		printf("要删除的联系人不存在!\n");
		return;
	}
	//找到了,要删除findidx位置的数据
	SLErase(pcon, findidex);
}
//修改联系人
void ContactModify(contact* pcon)
{
	char name[NAME_MAX];
	scanf("%s", name);
	int find = FindByName(pcon, name);
	if (find < 0)
	{
		printf("查找的联系人不存在");
			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].tel);
	printf("请输入新用户的地址\n");
	scanf("%s", pcon->a[find].addr);
	
	printf("修改成功\n");
}
//查看通讯录
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].tel,
			pcon->a[i].addr
		);
	}
}
//查找指定联系人
void ContactFind(contact* pcon)
{
	char name[NAME_MAX];
	printf("请输入要查找联系人的名字\n");
	scanf("%s", name);
	int find = FindByName(pcon, name);
	if (find < 0)
	{
		printf("未找到你要的联系人\n");
		return; 
	}
	//找到了,打印当前联系人
	else
	{
		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].tel,
			pcon->a[find].addr
		);
	}
}

测试文件

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


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

}
int main() {
	//SLtest();
	//SLtest02();
	int op = -1;
	contact con;
	ContactInit(&con);
	do
	{
		mnue();
		printf("请选择操作\n");
		scanf("%d", &op);
		switch (op)
		{
		case 1:
			ContactAdd(&con);
			break;
		case 2:
			ContactFind(&con);
			break;
		case 3:
			ContactDel(&con);
			break;
		case 4:
			ContactModify(&con);
			break;
		case 5:
			ContactShow(&con);
			break;
		case 0:
			printf("goodbye~\n");
			break;
		default:
			printf("输入错误\n");
			break;
		}
	} while (op);
	ContactDestroy(&con);
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值