前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >顺序表实现通讯录

顺序表实现通讯录

作者头像
发布2024-04-30 21:19:28
820
发布2024-04-30 21:19:28
举报
文章被收录于专栏:转自CSDN转自CSDN

SeqList.h 单链表的基文件的头文件

代码语言:javascript
复制
#pragma once
//顺序表结构
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contacts.h"
#define N 100

#if 0
//静态顺序表
struct SeqList
{
	int arr[N];
	int size;//有效数据个数
};
#endif

//typedef int SLDateType;
typedef peoInfo SLDateType;

//动态数据表
typedef struct SeqList
{
	SLDateType* arr;
	int size;//有效数据个数
	int capacity;//空间大小
}SL;

//typedef struct SeqList SL;

//顺序表

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

//头插 & 尾插
void SLPushBack(SL* ps,SLDateType x);//尾插
void SLPushFront(SL* ps,SLDateType x);//头插

void SLPopBack(SL * ps);
void SLPopFront(SL* ps);

void SLPrint(SL s);

// 指定位置之前插入和删除
void SLInsert(SL* ps, int pos, SLDateType x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLDateType x);

?SeqList.c文件

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS
#include "SeqList.h"
void SLInit(SL * ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

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

void SLCheckCapacity(SL* ps)
{
	//ps->arr[ps->size] = x;
	//++ps->size;
	//先看有没有空间
	if (ps->capacity == ps->size)
	{
		//申请空间 用realloc
		//三目表达式
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDateType* tmp = (SLDateType*)realloc(ps->arr, newCapacity * 2 * sizeof(SLDateType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);//直接退出程序
		}
		//申请成功
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}

void SLPushBack(SL* ps, SLDateType x)
{
	assert(ps);	
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
}

void SLPushFront(SL* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[0] = arr[1]
	}
	ps->arr[0] = x;
	ps->size++;
}

//void SLPrint(SL s)
//{
//	for (int i = 0; i < s.size; i++)
//	{
//		printf("%d ", s.arr[i]);
//	}
//	printf("\n");
//}

void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	//顺序表不为空
	//ps->arr[ps->size - 1] = -1;
	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];//arr[0] = arr[1]
	}
	ps->size--;
}

//pos 要大于等于0
void SLInsert(SL* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	//判断增容
	SLCheckCapacity(ps);
	//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);

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

//int SLFind(SL* ps, SLDateType x)
//{
//	assert(ps);
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->arr[i] == x)
//		{
//			return i;
//		}
//	}
//	return -1;
//}

通讯录的头文件 Contacts.h

代码语言:javascript
复制
#pragma once
//定义联系人数据 结构
//name gender age call adress
#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 SeqList Contact;
//通讯初始化
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);

Contacts.c文件

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS
#include"Contacts.h"
#include"SeqList.h"

//初始化
void ContactInit(Contact* con)
{
	SLInit(con);
}
void ContactDesTroy(Contact* con)
{
	SLDestroy(con);
}
void ContactAdd(Contact* con)
{
	//姓名+性别+年龄+电话+地址
	//char name[NAME_MAX];
	//char gender[GENDER_MAX];
	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);
	//添加数据
	SLPushFront(con,info);
}
int FindByName(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);
	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("没这个人\n");
		return;
	}
	//删除
	SLErase(con, find);
	printf("给这玩意删除了\n");
}
void ContactShow(Contact* con)
{
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
	for (int i = 0; i < con->size; i++)
	{
		printf("%s %s %d %s %s\n",\
			con->arr[i].name,	\
			con->arr[i].gender,	\
			con->arr[i].age,	\
			con->arr[i].tel,	\
			con->arr[i].addr     );
	}
}
void ContactModify(Contact* con)
{
	//要修改的联系人数据存在
	char name[NAME_MAX];
	printf("请输入要修改的名字;\n");
	scanf("%s", name);
	int find = FindByName(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");
}
void ContactFind(Contact* con)
{
	char name[NAME_MAX];
	printf("查找名字\n");
	scanf("%s", name);
	int find = FindByName(con, name);
	if (find < 0)
	{
		printf("未找到\n");
		return;
	}
	printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
	printf("%s %s %d %s %s\n",       \
			con->arr[find].name,   \
			con->arr[find].gender, \
			con->arr[find].age,    \
			con->arr[find].tel,    \
			con->arr[find].addr);

}

主文件

代码语言:javascript
复制
#define _CRT_SECURE_NO_WARNINGS

#include "SeqList.h"
//struct SeqList_A//静态顺序表
//{
//	int arr[100];//定长数组
//	int size;//当前有效数据个数
//};
//
//struct SeqList_B//动态顺序表
//{
//	int* arr;//
//	int szie;//有效数据个数
//	int capacity;//空间大小
//};

void SLTest01()
//{
//	//SL sl;
//	初始化
//	//SLInit(&sl);
//	增删查改
//	//SLPushBack(&sl,1);
//	//SLPushBack(&sl,2);
//	//SLPushBack(&sl,3);
//	//SLPrint(sl);
//	//SLInsert(&sl, 2, 2);
//	//SLPrint(sl);
//	//SLInsert(&sl,sl.size, 122);
//	//SLPrint(sl);
//	//SLErase(&sl, 2);
//	//SLErase(&sl, 2);
//	//SLErase(&sl, 2);
//	//SLErase(&sl, 0);
//	//SLErase(&sl, 0);
//	//SLPrint(sl);
//	销毁
//	//SLDestroy(&sl);
//	//int find = SLFind(&sl, 4);
//	//if (find < 0)
//	//{
//	//	printf("未找到");
//	//}
//	//else
//	//{
//	//	printf("位置为 %d", find);
//	//}
//}
//
void ContactTest()
//{
//	Contact con;//通讯录对象
//	ContactInit(&con);
//	ContactAdd(&con);
//	ContactShow(&con);
//
//}

void meau()
{
	printf("*********通讯录********\n");
	printf("*1.增加数据 2.删除数据*\n");
	printf("*3.修改数据 4.查找数据*\n");
	printf("*5.展示数据 0.退出程序*\n");
	printf("***********************\n");
}

int main()
{
	int op = -1;
	Contact con;
	ContactInit(&con);
	do 
	{
		meau();
		printf("请选择操作:\n");
		scanf("%d", &op);
		switch (op)
		{
		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");
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (op != 0);

	ContactDesTroy(&con);
	return 0;
}
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com