顺序表实现简易通讯录(C语言实现)(内附源码)

代码实现如下:

#define _CRT_SECURE_NO_WARNINGS
#define INIT_CAPACITY 4

#define NAME_MAX 100
#define SEX_MAX 10
#define TEL_MAX 15
#define ADDR_MAX 100

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef struct ContactInfo SLDataType;

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

typedef struct SeqList contact;

typedef struct ContactInfo
{
	char name[NAME_MAX];//使用
	char sex[SEX_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}CInfo;


//顺序表
void SLInit(SL* ps)//初始化
{
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

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

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

void SLExpend(SL* ps)//空间扩容
{
	if (ps->size == ps->capacity)
	{
		if (ps->capacity == 0)
			ps->capacity = INIT_CAPACITY;
		else
			ps->capacity *= 2;
		SLDataType* tem = (SLDataType*)realloc(ps->a, 2 * ps->capacity * sizeof(SLDataType));
		ps->capacity *= 2;
		if (tem == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->a = tem;
		tem = NULL;

	}
}

void SLPushBack(SL* ps, SLDataType x)//尾插
{
	assert(ps);
	SLExpend(ps);
	ps->a[ps->size++] = x;

}

void SLPushFront(SL* ps, SLDataType x)//头插
{
	assert(ps);
	SLExpend(ps);

	int i = 0;
	for (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(ps->size);
	ps->size--;
}


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

	int i;
	for (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);
	SLExpend(ps);

	int i = 0;
	
	for (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(pos >= 0 && pos < ps->size);
	assert(ps->size);
	int i = 0;

	for (i = pos; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;

}


/*int SLFind(SL* ps, SLDataType x)//查找指定元素
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (x == ps->a[i])
			return i;
	}
	return -1;
}*/

void ContactInit(contact* con)//通讯录初始化
{
	SLInit(con);
}

void ContactDestroy(contact* con)//销毁
{
	SLDestroy(con);
}

void ContactPrint(contact* con)//打印
{
	printf("\n%-15s %-15s %-15s %-15s %-15s\n", "姓名", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%-15s %-15s %-15d %-15s %-15s\n",
			con->a[i].name,
			con->a[i].sex,
			con->a[i].age,
			con->a[i].tel,
			con->a[i].addr);
	}
	printf("\n");
}

void ContactAdd(contact* con)//增加联系人
{
	CInfo CI;
	printf("\n请输入要添加的联系人信息:\n");

	printf("请输入联系人姓名:\n");
	scanf(" %s", CI.name);

	printf("请输入联系人性别:\n");
	scanf(" %s", CI.sex);

	printf("请输入联系人年龄:\n");
	scanf(" %d", &(CI.age));

	printf("请输入联系人电话号:\n");
	scanf(" %s", CI.tel);

	printf("请输入联系人地址:\n");
	scanf(" %s", CI.addr);

	SLPushBack(con, CI);
}

int FindIndex(contact* con,char name[])//按姓名找下标
{
	int i ;
	for (i = 0; i < con->size; i++)
	{
		if (strcmp(con->a[i].name, name) == 0)
			return i;
	}
	return -1;
}
void ContactDelete(contact* con)//根据姓名删除
{
	char name[NAME_MAX];
	printf("\n请输入要删除联系人的姓名:\n");
	scanf(" %s", name);

	int findindex = FindIndex(con, name);
	if(findindex < 0)
	{
		printf("要删除的人不存在!\n");
		return;
	}
	SLErase(con, findindex);
}

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

	int findindex = FindIndex(con, name);
	if (findindex < 0)
	{
		printf("要删除的人不存在!\n");
		return;
	}

	printf("\n%-15s %-15s %-15s %-15s %-15s\n", "姓名", "性别", "年龄", "电话", "地址");
	printf("%-15s %-15s %-15d %-15s %-15s\n",
			con->a[findindex].name,
			con->a[findindex].sex,
			con->a[findindex].age,
			con->a[findindex].tel,
			con->a[findindex].addr);
	printf("\n");
	
}


void ContactModify(contact* con)//修改联系人
{
	printf("\n请输入要修改联系人姓名: ");
	char name[NAME_MAX];
	scanf("%s", name);
	int findidex = FindIndex(con, name);
	if (findidex < 0)
	{
		printf("要查找的人不存在!\n");
		return;
	}
	printf("\n请输入要修改联系人的信息:\n");
	printf("请输入要修改联系人姓名:\n");
	scanf("%s", con->a[findidex].name);

	printf("请输入要修改联系人性别:\n");
	scanf("%s", con->a[findidex].sex);

	printf("请输入要修改联系人年龄:\n");
	scanf("%d", &(con->a[findidex].age));

	printf("请输入要修改联系人电话:\n");
	scanf("%s", (con->a[findidex].tel));

	printf("请输入要修改联系人地址:\n");
	scanf("%s", con->a[findidex].addr);

	printf("\n");
}

int main()
{
	
	contact info;
	SLInit(&info);

	ContactAdd(&info);//增加联系人
	ContactAdd(&info);//增加联系人
	ContactAdd(&info);//增加联系人
	ContactPrint(&info);//打印

	ContactDelete(&info);//删除
	ContactPrint(&info);//打印

	ContactFind(&info);//查找
	
	ContactModify(&info);
	ContactPrint(&info);//打印

	SLDestroy(&info);
	return 0;
}

代码运行结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值