基于顺序表的通讯录C语言实现

关于顺序表的详细了解请见博主的另一篇博客,本文旨在对顺序表进行应用,采用C语言编写。

http://t.csdnimg.cn/tDpYz

一、驱动层

1.1 SeqList.h

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

typedef peoInfo SLDataType;

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


//顺序表初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestroy(SL* ps);
//顺序表的打印
void SLPrint(SL s);

//头部插入
void SLPushFront(SL* ps, SLDataType x);
//尾部插入
void SLPushBack(SL* ps, SLDataType x);
//头部删除
void SLPopFront(SL* ps);
//尾部删除
void SLPopBack(SL* ps);

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

1.2 SeqList.c

#include"SeqList.h"

//顺序表初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{
	assert(ps);
	if (ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->capacity = 0;
	ps->size = 0;
}
void SLCheck(SL* ps)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		int newCapacity = 0;
		if (ps->capacity == 0)
		{
			newCapacity = 4;
		}
		else
		{
			newCapacity = 2 * ps->capacity;
		}
		SLDataType* temp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
		if (temp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		//空间申请成功
		ps->arr = temp;
		ps->capacity = newCapacity;
	}
}
//头部插入
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheck(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}
//尾部插入
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheck(ps);
	ps->arr[ps->size++] = x;
}
//头部删除
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 SLPopBack(SL* ps)
{
	assert(ps);
	ps->size--;
}
//指定位置前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheck(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--;
}

二、调用层

2.1 Contact.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

#define NAME_MAX 20
#define GENDER_MAX 10
#define TEL_MAX 20
#define ADDRESS_MAX 100


//定义联系人自定义数据类型
typedef struct personInfo
{
	char name[NAME_MAX];
	char gender[GENDER_MAX];
	int age;
	char tel[TEL_MAX];
	char address[ADDRESS_MAX];
}peoInfo;


typedef struct SeqList Contact;

// 初始化通讯录
void ContactInit(Contact* con);
// 添加通讯录数据
void ContactAdd(Contact* con);
// 删除通讯录数据
void ContactDel(Contact* con);
// 展⽰通讯录数据
void ContactShow(Contact* con);
// 查找通讯录数据
void ContactFind(Contact* con);
// 修改通讯录数据
void ContactModify(Contact* con);
// 销毁通讯录数据
void ContactDestroy(Contact* con);

2.2 Contact.c

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

int FindByname(Contact* con, char* cmp)
{
	for (int i = 0; i < con->size; i++)
	{
		if (strcmp(con->arr[i].name, cmp) == 0)
		{
			return i;
			break;
		}
	}
	return -1;
}

// 初始化通讯录
void ContactInit(Contact* con)
{
	SLInit(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.address);

	SLPushBack(con, info);
}
// 删除通讯录数据
void ContactDel(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要删除的联系人姓名\n");
	scanf("%s", name);
	int ret = FindByname(con, name);
	if (ret >= 0)
	{
		SLErase(con, ret);
		printf("删除成功!\n");
	}
	else
	{
		printf("数据不存在!\n");
		return;
	}
}
// 展⽰通讯录数据
void ContactShow(Contact* con)
{
	printf("%s	%s	%s	%s	%s\n", "姓名", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%s	", con->arr[i].name );
		printf("%s	", con->arr[i].gender );
		printf("%d	", con->arr[i].age );
		printf("%s	", con->arr[i].tel );
		printf("%s	", con->arr[i].address );
		printf("\n");
	}
}
// 查找通讯录数据
void ContactFind(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要查找的联系人姓名\n");
	scanf("%s", name);
	int ret = FindByname(con, name);
	if (ret>=0)
	{
		printf("查找成功!\n");
		printf("%s	%s	%s	%s	%s\n", "姓名", "性别", "年龄", "电话", "地址");
		printf("%s	", con->arr[ret].name);
		printf("%s	", con->arr[ret].gender);
		printf("%d	", con->arr[ret].age);
		printf("%s	", con->arr[ret].tel);
		printf("%s	", con->arr[ret].address);
		printf("\n");
	}
	else
	{
		printf("数据不存在!\n");
		return;
	}

}
// 修改通讯录数据
void ContactModify(Contact* con)
{
	char name[NAME_MAX];
	printf("请输入要修改的联系人姓名\n");
	scanf("%s", name);
	int ret = FindByname(con, name);
	if (ret >= 0)
	{
		printf("请输入新的联系人姓名\n");
		scanf("%s", con->arr[ret].name);

		printf("请输入新的联系人性别\n");
		scanf("%s", con->arr[ret].name);

		printf("请输入新的联系人年龄\n");
		scanf("%d", &(con->arr[ret].age));

		printf("请输入新的联系人电话\n");
		scanf("%s", con->arr[ret].tel);

		printf("请输入新的联系人住址\n");
		scanf("%s", con->arr[ret].address);

		printf("修改成功!\n");
	}
	else
	{
		printf("数据不存在!\n");
		return;
	}
}
// 销毁通讯录数据
void ContactDestroy(Contact* con)
{
	SLDestroy(con);
}

三、主函数

3.1 main.c

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#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()
{
	int a = -1;
	Contact con;
	ContactInit(&con);

	do {
		menu();
		printf("请选择您的操作:\n");
		scanf("%d", &a);
		switch (a)
		{
		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 (a != 0);

	ContactDestroy(&con);
	return 0;
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值