顺序表的应用---通讯录

说明1

通讯录的实现依赖于顺序表的实现,顺序表里面的的数据类型各种各样,可以是整形,字符型,还可以是浮点型去去,但是只要把里面的数据类型用一个结构体来代替,就可以实现通讯录项目

说明2

1.实现的过程中可能会出现头文件交叉包含的问题,这是可以使用前置声明,避免的头文件的交叉包含问题,

int add(int x,int y);//函数的前置声明
int main()
{
    return 0;
}
 
int add(int x,int y)
{
    //函数的实现
}

通讯录的实现

SeqList.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
#include"string.h"
typedef Info SLtypedat;
typedef struct SeqList
{
	SLtypedat* arr;
	int capcity;//容量
	int size;//有效个数
}SL;
void SLinit(SL* ps);//初始化顺序表
void checkSL(SL* ps);//检查是否增加容量
void SLprint(SL* ps);
void SLpushfront(SL* ps, SLtypedat x);//顺序表的头插
void SLpushback(SL* ps, SLtypedat x);//顺序包的u尾部删除
void SLpopback(SL* ps);//顺序表的尾部删除
void SLpopfront(SL* ps);//顺序表的头部删除
void SLinsert(SL* ps, int pos, SLtypedat x);//特定位置的插入
void SLerase(SL* ps, int pos);//删除指定位置的数据
void SLdestory(SL* ps);//数据的修改

SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SLinit(SL* ps)
{
	ps->arr = NULL;
	ps->capcity = ps->size = 0;
}
void checkSL(SL* ps)
{
	
	if (ps->size == ps->capcity)
	{
		int newcapticy = (ps->capcity ? 0 : 2 *ps->capcity);
		SLtypedat* temp = (SLtypedat*)realloc(ps->arr, newcapticy * sizeof(SLtypedat));
		if (temp == NULL)
		{
			perror("temp");
			exit(1);
		}
		ps->arr = temp;
		ps->capcity = newcapticy;
	}
}
void SLpushfront(SL* ps, SLtypedat x)
{
	assert(ps != NULL);
	checkSL(ps);
	for (int i = ps->size-1; i >= 0; i--)
	{
		ps->arr[i + 1] = ps->arr[i];
	}
	ps->arr[0] = x;
	ps->size++;
}
void SLprint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
}
void SLpushback(SL* ps, SLtypedat x)
{
	assert(ps);
	checkSL(ps);
	ps->arr[ps->size++] = x;
}
void SLpopback(SL* ps)
{
	assert(ps);
	ps->size--;
}
void SLpopfront(SL* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size-2; i++)
	{
		ps->arr[i] = ps->arr[i+1];
	}
	ps->size--;

}
void SLinsert(SL* ps, int pos, SLtypedat x)
{
	assert(ps);
	assert((pos >= 0) && (pos < ps->size));
	checkSL(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);
	for (int i = pos; i<=ps->size-2; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
void SLdestory(SL* ps)
{
	assert(ps);
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capcity = 0;
}

Contact.h

#pragma once
#define name_max 20
#define tel_max 12
#define addr_max 30
#define gender_max 2
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 Contactdestory(Contact* pcon);//通讯录的销毁
void Contactadd(Contact* pcon);//添加联系人
void Contactprint(Contact* pcon);//查看通讯录
int ConFindbyname(Contact* pcon);//查找联系人的下标
void Contactdel(Contact* pcon);//联系人的删除
void Contactfind(Contact* pcon);//查找联系人
void Contactmodefy(Contact* pcon);//联系人的修改

Contact.c

#include"SeqList.h"

void Contactinit(Contact* pcon)
{
	SLinit(pcon);
}
void Contactdestory(Contact* pcon)
{
	SLdestory(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);
}
void Contactprint(Contact* pcon)
{
	printf("%s %s %s %s %s\n", "姓名", "年龄", "性别", "电话", "住址");
	for (int i = 0; i < pcon->size; i++)
	{
		printf("%s %d %s %s %s\n",pcon->arr[i].name,
			pcon->arr[i].age,
			pcon->arr[i].gender,
			pcon->arr[i].tel,
			pcon->arr[i].addr);
	}
}
int ConFindbyname(Contact* pcon)
{
	char name[name_max];
	printf("请输入联系人姓名\n");
	scanf("%s", name);
	for (int i = 0; i < pcon->size; i++)
	{
		if (strcmp(name, pcon->arr[i].name) == 0)
		{
			return i;
		}
	}
	return -1;
}
void Contactdel(Contact* pcon)
{
	
	int ret = ConFindbyname(pcon);
	if (ret < 0)
	{
		printf("您要删除的联系人不存在\n");
	}
	else
	{
		SLerase(pcon, ret);
	}
}
void Contactfind(Contact* pcon)
{
	int ret = ConFindbyname(pcon);
	if (ret < 0)
	{
		printf("您要查找的联系人不存在\n");
	}
	else
	{
		printf("%s %d %s %s %s\n", pcon->arr[ret].name,
			pcon->arr[ret].age,
			pcon->arr[ret].gender,
			pcon->arr[ret].tel,
			pcon->arr[ret].addr);
	}
}
void Contactmodefy(Contact* pcon)
{
	int ret = ConFindbyname(pcon);
	if (ret < 0)
	{
		printf("您要修改的联系人不存在\n");
	}
	else
	{
		printf("请输入新的姓名\n");
		scanf("%s", pcon->arr[ret].name);
		printf("请输入新的年龄\n");
		scanf("%d", &pcon->arr[ret].age);
		printf("请输入新的性别\n");
		scanf("%s", pcon->arr[ret].gender);
		printf("请输入新的电话\n");
		scanf("%s", pcon->arr[ret].tel);
		printf("请输入新的家庭住址\n");
		scanf("%s", pcon->arr[ret].addr);
	}
}

Contacttest.c    

#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void menu()
{
	printf("***************通讯表**************\n");
	printf("***** 1.添加联系人 2.删除联系人****\n");
	printf("***** 3.查找联系人 4.修改联系人****\n");
	printf("***** 5.查看通讯表 0.退出通讯表****\n");
	printf("***********************************\n");
}
int main()
{
	Contact con;
	Contactinit(&con);
	int select = 0;
	do
	{
		menu();
		printf("请输入你的选择\n");
		scanf("%d", &select);
		
		switch(select)
		{
		case 1://添加联系人
			Contactadd(&con);
			break;
		case 2://删除联系人
			Contactdel(&con);
			break;
		case 3://查找联系人
			Contactfind(&con);
			break;
		case 4://修改联系人
			Contactmodefy(&con);
			break;
		case 5://查看通讯表
			Contactprint(&con);
			break;
		case 0://退出通讯表
			printf("退出通讯表中.....\n");
			break;
		default:
			break;

		}
	} while (select!=0);
	Contactdestory(&con);
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DS顺序表(也称为数组)是一种线性数据结构,它使用连续的存储空间来存储元素。我可以告诉你如何进行一些常见的操作,例如插入、删除和访问元素。 1. 插入元素: 要在顺序表中插入元素,首先需要确定插入的位置。假设要在第i个位置插入元素x,需要将第i个位置及其之后的所有元素向后移动一位,然后将x放入第i个位置。这样可以保持顺序表的连续性。 代码示例: ```python def insert_element(arr, i, x): n = len(arr) arr.append(None) # 在末尾添加一个空元素 for j in range(n, i, -1): arr[j] = arr[j-1] arr[i] = x # 示例使用 my_list = [1, 2, 3, 4, 5] insert_element(my_list, 2, 10) print(my_list) # 输出: [1, 2, 10, 3, 4, 5] ``` 2. 删除元素: 要从顺序表中删除元素,首先需要确定删除的位置。假设要删除第i个位置的元素,需要将第i+1个位置及其之后的所有元素向前移动一位,然后将最后一个元素删除。 代码示例: ```python def delete_element(arr, i): n = len(arr) for j in range(i, n-1): arr[j] = arr[j+1] arr.pop() # 示例使用 my_list = [1, 2, 3, 4, 5] delete_element(my_list, 2) print(my_list) # 输出: [1, 2, 4, 5] ``` 3. 访问元素: 顺序表的元素可以通过索引访问。要访问第i个位置的元素,可以直接使用arr[i]进行访问。 代码示例: ```python def access_element(arr, i): return arr[i] # 示例使用 my_list = [1, 2, 3, 4, 5] print(access_element(my_list, 2)) # 输出: 3 ``` 这些是DS顺序表的一些常见操作,希望能对你有所帮助!如果有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值