顺序表的应用—通讯录项目

SeqList.h

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

//静态顺序表
#define N 100
//typedef int SLDataType;
typedef Info SLDataType;

//struct SeqList
//{
//	SLDataType a[N];
//	int size;
//};

//动态顺序表
typedef struct SeqList
{
	SLDataType* arr;//存储数据的底层结构
	int capacity;//记录顺序表的空间大小
	int size;//记录顺序表当前有效的数据个数
}SL;

//typedef struct SeqList SL;

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

//打印
void SLPrint(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

#include "SeqList.h"
#include <stdio.h>
//初始化
void SLInit(SL* ps) {//实参传得是地址
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps) {
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
		if (tmp = NULL) {
			perror("realloc fail!");
			exit(1);
		}
		//扩容成功
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}
void SLPushBack(SL* ps, SLDataType x){
	assert(ps);
	//assert(ps!=NULL);
	//if (ps == NULL) {
	//	return;
	//}
	//空间不够,扩容
	SLCheckCapacity(ps);

	//空间足够,直接插入
	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];
	}
	ps->arr[0] = x;
	ps->size++; 
}

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

	//顺序表不为空
	ps->size--;
}

void SLPopFront(SL* ps) {
	assert(ps);
	assert(ps->size);

	//不为空执行挪动操作
	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);

	//pos及之后的数据往后挪动一位,pos空出来
	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);
	
	//pos以后的数据往前挪动一位
	for (int i = pos; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i +1];
	}
	ps->size--;
}
//在顺序表中查找x
int SLFind(SL* ps, SLDataType x) {
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i].name == x.name) {
			return i;
		}
	}
	return -1;
}

//销毁
void SLDestroy(SL* ps) {
	assert(ps);

	if (ps->arr) {
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLPrint(SL* ps){
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

Contact.h


#define NAME_MAX 100
#define GENDER_MAX 10
#define TEL_MAX 12
#define ADDR_MAX 100

//通讯录数据类型
typedef struct PersonInfo
{
	char name[NAME_MAX];
	int age;
	char gender[GENDER_MAX];
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}Info;

//使用顺序表的前置声明
struct SeqList;

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 ContactFind(Contact* pcon);
void ContactShow(Contact* pcon);

 Contact.c

#include "Contact.h"
#include "SeqList.h"
//通讯录的初始化和销毁
//SL* ps
void ContactInit(Contact* pcon){
	SLinit(pcon);
}
//实际初始化的还是顺序表
void ContactDesTroy(Contact* pcon) {
	SLDestroy(pcon);
}

//增加、删除、修改、查找、查看通讯录
void ContactAdd(Contact* pcon){
	//创建联系人结构体
	Info info;
	printf("请输入联系人姓名:\n");
	scanf("%s", info.name);
	printf("请输入联系人年龄:\n");
	scanf("%d", &info.age);
	printf("请输入联系人性别:\n");
	scanf("%s", info.gender);
	printf("请输入联系人电话:\n");
	scanf("%s", info.tel);
	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->arr[i].name, name) == 0) {
			//找到了
			return i;
		}
	}
	return -1;
}

void ContactDel(Contact* pcon) {
	//删除之前一定要先查找
	//找到了,可以删除
	//找不到,不能执行删除
	printf("请输入要删除的联系人的姓名:\n");
	char name[NAME_MAX];
	scanf("%s", name);

	int findIndex = FindByName(pcon, name);
	if (findIndex < 0) {
		printf("要删除的联系人不存在!\n");
		return;
	}
	//执行删除操作
	SLErase(pcon, findIndex);
	printf("联系人删除成功!\n");
}
void ContactModify(Contact* pcon) {
	//修改之前一定要先查找
	//找到了,可以修改
	//找不到,不能执行修改

	printf("请输入要修改的联系人的姓名:\n");
	char name[NAME_MAX];
	scanf("%s", name);

	int findIndex = FindByName(pcon, name);
	if (findIndex < 0) {
		printf("要修改的联系人不存在!\n");
		return;
	}
	//执行修改操作
	printf("请输入姓名:\n");
	scanf("%s", pcon->arr[findIndex].name);
	printf("请输入年龄:\n");
	scanf("%d", pcon->arr[findIndex].age);
	printf("请输入性别:\n");
	scanf("%s", pcon->arr[findIndex].gender);
	printf("请输入电话:\n");
	scanf("%s", pcon->arr[findIndex].tel);
	printf("请输入住址:\n");
	scanf("%s", pcon->arr[findIndex].addr);

	printf("联系人修改成功!\n");

}
void ContactFind(Contact* pcon) {
	char name[NAME_MAX];
	printf("请输入要查找的联系人的姓名:\n");
	scanf("%s", name);

	int findIndex = FindByName(pcon, name);
	if (findIndex < 0) {
		printf("该联系人不存在!\n");
		return;
	}
	//找到了,打印一下查找的联系人信息
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "住址");
	printf("请输入姓名:\n");
	scanf("%s", pcon->arr[findIndex].name);
	printf("请输入年龄:\n");
	scanf("%d", pcon->arr[findIndex].age);
	printf("请输入性别:\n");
	scanf("%s", pcon->arr[findIndex].gender);
	printf("请输入电话:\n");
	scanf("%s", pcon->arr[findIndex].tel);
	printf("请输入住址:\n");
	scanf("%s", pcon->arr[findIndex].addr);
}
void ContactShow(Contact* pcon) {

	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "住址");

	for (int i = 0; i < pcon->size; i++)
	{
		printf("%s %s %d %s %s\n",
			pcon->arr[i].name,
			pcon->arr[i].gender,
			pcon->arr[i].age,
			pcon->arr[i].addr
			);
	}
}

ConTest.c

#include "SeqList.h"

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

	}
int main111()
{
	int op = -1;
	//创建通讯录结构对象
	Contact con;
	ContactInit(&con);
	do {
		menu();
		printf("请选择您的操作:\n");
		scanf("%d", &op);

		switch (op)
		{
			case 1:
				ContactAdd(&con);
				break;
			case 2:
				ContactDel(&con);
				break;
			case 3:
				ContactModify(&con);
				break;
			case 4:

				break;
			case 5:
				ContactShow(&con);
				break;
			case 0:
				printf("通讯录退出中...\n");
			default:
				break;
		}
	} while (op!=0);
	//销毁通讯录
	ContactDesTroy(&con);
	return 0;
}


//测试通讯录
int main() {
	Contact con;
	ContactInit(&con);
	ContactAdd(&con);
	ContactAdd(&con);
	ContactAdd(&con);
	ContactDesTroy(&con);

	return 0;
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值