C语言笔记26 •顺序表应用•

基于动态顺序表实现通讯录项目

1.通讯录其实也就是顺序表,就是把里面存的数据类型变了一下 ,所以有一些方法对于顺序表适用,对于通讯录也是适用的(初始化,销毁,内存空间扩容)。

2.要用到顺序表相关的方法,对通讯录的操作实际就是对顺序表进行操作。所以实现的过程中需要调用顺序表的头文件和实现文件。

3.顺序表的介绍可以看上篇博客:C语言笔记25 •顺序表介绍•-CSDN博客

4.实现通讯录这个项目也是创建三个文件:                                                                                         

通讯录的头文件:Contact.h

通讯录的实现文件:Contact.c

通讯录的测试文件:Contact_test.c

顺序表头文件:SeqList.h    

顺序表实现文件:SeqList.c

5.具体看以下代码:

//Contact.h

#pragma once
//定义联系人信息所需要的宏
#define NAME_MAX 20
#define GENDER_MAX 10
#define TEL_MAX 20
#define ADDR_MAX 100


//定义联系人的 数据结构
//姓名 性别 年龄 电话 住址
typedef struct personInfo
{
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}peoInfo;   //重定义结构体

//typedef struct personInfo peoInfo; //重定义结构体

typedef struct SeqList Contact;//前置声明  不能typedef SL Contact;其一是SL也是顺序表重定义的名称,其二这样需要加载头文件SeqList.h (SeqList.h也调用了Contact.h)这两个头文件相互调用会矛盾 ,解决方法就是“前置声明”


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

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

void ContactAdd(Contact* con);//通讯录添加数据

void ContactDel(Contact* con);//通讯录删除数据

void ContactModify(Contact* con);//通讯录的修改

void ContactFind(Contact* con);//通讯录查找

void ContactShow(Contact* con);//展示通讯录数据

//Contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include <string.h>
#include "Contact.h"
#include "SeqList.h"

void ContactInit(Contact* con)
{
	SLInit(con);//顺序表初始化
}
void ContactDesTroy(Contact* con)
{
	SLDestroy(con);//顺序表销毁
}
void ContactAdd(Contact* con)//通讯录添加数据
{
	//获取用户输入的内容:姓名+性别+年龄+电话+地址
	peoInfo info;
	printf("请输入要添加联系人的姓名\n");
	scanf("%s", info.name);
	printf("请输入要添加联系人的性别\n");
	scanf("%s", info.gender);
	printf("请输入要添加联系人的年龄\n");
	scanf("%d", &info.age);
	printf("请输入要添加联系人的电话\n");
	scanf("%s", info.tel);
	printf("请输入要添加联系人的地址\n");
	scanf("%s", info.addr);

	//往通讯录中添加联系人数据
	SLPushback(con, info);
}
void ContactShow(Contact* con)//展示通讯录数据
{
	printf("姓名 性别 年龄 电话 地址\n");
	for (int i = 0; i < con->size; i++)
	{
		printf("%3s %4s %5d %5s %4s\n",
			con->arr[i].name,
			con->arr[i].gender,
			con->arr[i].age,
			con->arr[i].tel,
			con->arr[i].addr);
	}
}
int findperson(Contact* con, char name[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (strcmp(con->arr[i].name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}
void ContactDel(Contact* con)//通讯录删除数据
{
	char name[NAME_MAX];
	printf("请输入你要删除联系人的姓名\n");
	scanf("%s", name);
	if (findperson(con, name) >= 0)
	{
		SLErase(con, findperson(con, name));
		printf("删除成功\n");
		ContactShow(con);
	}
	else
	{
		printf("对不起,删除失败,通讯录中不存在该联系人\n");
	}
}

void ContactModify(Contact* con)//通讯录的修改
{
	//检查修改的联系人数据是否存在
	char name[NAME_MAX];
	printf("请输入要修改的用户姓名:\n");
	scanf("%s", name);
	int find = findperson(con, name);
	if (find < 0)
	{
		printf("要修改的联系人数据不存在!\n");
		return;
	}
	//修改的联系人数据存在,直接修改
	printf("请输入要修改联系人的姓名\n");
	scanf("%s", con->arr[find].name);
	printf("请输入要修改联系人的性别\n");
	scanf("%s", con->arr[find].gender);
	printf("请输入要修改联系人的年龄\n");
	scanf("%d", &con->arr[find].age);
	printf("请输入要修改联系人的电话\n");
	scanf("%s", con->arr[find].tel);
	printf("请输入要修改联系人的地址\n");
	scanf("%s", con->arr[find].addr);
	printf("修改成功\n");
	ContactShow(con);
}
void ContactFind(Contact* con)//通讯录查找
{
	//检查修改的联系人数据是否存在
	char name[NAME_MAX];
	printf("请输入要查找的用户姓名:\n");
	scanf("%s", name);
	int find = findperson(con, name);
	if (find < 0)
	{
		printf("要查找的联系人数据不存在!\n");
		return;
	}
	//存在
	printf("姓名 性别 年龄 电话 地址\n");
	printf("%3s %4s %5d %5s %4s\n",
		con->arr[find].name,
		con->arr[find].gender,
		con->arr[find].age,
		con->arr[find].tel,
		con->arr[find].addr);
}
//SeqList.h    

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

//typedef int SLDataType;//顺序表中数据的类型
typedef peoInfo SLDataType;//通讯录中自定义的 数据类型  要包含定义peoInfo的头文件 不然会报错找不到自定义的数据类型

typedef struct SeqList
{
	SLDataType* arr;
	int size;
	int capacity;

}SL;
//typedef struct SeqList SL;

//顺序表初始化
void SLInit(SL* ps);
//顺序表销毁
void SLDestroy(SL* ps);

//数据打印
void SLprint(SL sl);

//插入数据之前先看空间够不够
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 SLErase(SL* ps, int pos);
//查找
int SLFind(SL* ps, SLDataType x);
//SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"

//顺序表初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
//顺序表销毁
void SLDestroy(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

//插入数据之前先看空间够不够
void SLCheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)//空间不够了  需要申请内存
	{
		int Newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* temp = realloc(ps->arr, Newcapacity * sizeof(SLDataType));
		if (temp == NULL)
		{
			perror("realloc");
			exit(1);
			//return 1;
		}
		ps->arr = temp;//内存申请成功
		ps->capacity = Newcapacity;
	}
}
//数据打印
//void SLprint(SL sl)
//{
//	for (int i = 0; i < sl.size; i++)
//	{
//		printf("%d ", sl.arr[i]);
//	}
//	printf("\n");
//}
//尾部插入
void SLPushback(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	//ps->arr[ps->size] = x;
	//++ps->size;
	ps->arr[ps->size++] = x;
}
//头部插入
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	//先让顺序表中已有的数据整体往后挪动一位
	for (int i = ps->size;i>0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[1]=arr[0]
	}
	ps->arr[0] = x;
	ps->size++;//长度+1
}

//尾部删除
void SLPopBack(SL* ps)
{
	assert(ps);
	ps->size--;//--ps->size
}
//头部删除
void SLPopFront(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[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->arr[i] = ps->arr[i-1];
	}
	ps->arr[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->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
//查找
//int SLFind(SL* ps, SLDataType x)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//		{
//			//printf("找到了!\n");
//			return i;//找到了
//		}
//	}
//	return -1;//找不到
//	//printf("找不到您所要查找的数据!\n");
//}

//Contact_test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "Contact.h"
#include "SeqList.h"

void contact_test()
{
	Contact con;//创建的通讯录对象  实际上就是 顺序表对象,等价于SL sl
	ContactInit(&con);//顺序表初始化
	ContactAdd(&con);// 往通讯录中添加联系人数据
	ContactAdd(&con);// 往通讯录中再次添加联系人数据
	//ContactShow(&con);//展示通讯录数据
	//ContactDel(&con);//通讯录删除数据
	//ContactModify(&con);//通讯录修改数据
	ContactFind(&con);//通讯录查找联系人

	ContactDesTroy(&con);//顺序表销毁
}

void menu()
{
	printf("******************通讯录******************\n");
	printf("*******1.增加联系人   2.删除联系人********\n");
	printf("*******3.修改联系人   4.查找联系人********\n");
	printf("*******5.展示联系人   0.   退出  *********\n");
	printf("******************************************\n");
}
int main()
{
	Contact con;
	ContactInit(&con);
	//contact_test();
	int input = 1;
	do
	{
		menu();
		printf("请输入您要进行的项目:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			ContactAdd(&con);
			break;
		case 2:
			ContactDel(&con);
			break;
		case 3:
			ContactModify(&con);
			break;
		case 4:
			ContactFind(&con);
			break;
		case 5:
			ContactShow(&con);
			break;
		case 0:
			printf("退出通讯录....\n");
			break;
		default:
			printf("输入错误,请重新选择您的操作!\n");
			break;
		}
	} while (input);

	ContactDesTroy(&con);//顺序表销毁
	return 0;
}

6.运行界面: 

  • 20
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值