实现通讯录(顺序表版本)

一、功能要求

(1)⾄少能够存储100个⼈的通讯信息

(2)能够保存⽤⼾信息:名字、性别、年龄、电话、地址等

(3)增加联系⼈信息

(4)删除指定联系⼈

(5)查找制定联系⼈

(6)修改指定联系⼈

(7)显⽰联系⼈信息

二、代码实现

所需的头文件与源文件

SeqList.h

SeqList.c  //顺序表实现

contact.h

contact.c //通讯录实现

test.c    //测试

SeqList.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"contact.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 ps);
//扩容
void SLCheckCapacity(SL* ps);

//头部插入 / 尾部插入
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);
int SLFind(SL* ps, SLDataType x);

SeqList.c

#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)
{
	if (ps->capacity == ps->size)
	{
		//申请空间(增容用realloc)
		//要先判断capacity是否为零,如果为真默认给4个空间,如果为假直接乘2(三目操作符)
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = ps->arr = (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)

	//插入数据之前要看空间够不够
	SLCheckCapacity(ps);

	//ps->arr[ps->size] = x;
	//++ps->size;
	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 SLPrint(SL ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d ", ps.arr[i]);
	}
	printf("\n");
}

//头删
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 SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	//顺序表不为空
	--ps->size;
}

//在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType 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];//arr[pos+1] = arr[pos]
	}
	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--;
}

contact.h

#pragma once
#define NAME_MAX 100
#define SEX_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100

//前置声明

typedef struct SeqList contact;

//用户数据
//姓名 性别 年龄 电话 地址
typedef struct PersonInfo

{
	char name[NAME_MAX];
	char sex[SEX_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];

}PeoInfo;

//初始化通讯录
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);

contact.c

#include"contact.h"
#include"SeqList.h"

//初始化通讯录
void ContactInit(contact* con)
{
	//实际上就是顺序表的初始化
	//顺序表的初始化已经实现好了直接调用就行
	SLInit(con);
}

//销毁通讯录数据
void ContactDestroy(contact* con)
{
	SLDestroy(con);

}

//添加通讯录数据
void ContactAdd(contact* con)
{
	//获取用户输入的内容:姓名 性别 年龄 电话 地址
	PeoInfo info;
	printf("请输入要添加的联系人姓名:\n");
	scanf("%s", info.name);

	printf("请输入要添加的联系人性别:\n");
	scanf("%s", info.sex);

	printf("请输入要添加的联系人年龄:\n");
	scanf("%d", &info.age);

	printf("请输入要添加的联系人电话:\n");
	scanf("%s", info.tel);

	printf("请输入要添加的联系人地址:\n");
	scanf("%s", info.addr);

	//往通讯录中添加联系人数据
	SLPushBack(con, info);
}

int FindByName(contact* con,char name[])
{
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->arr[i].name, name))
		{
			//找到了
			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("%3s %2s %d %6s %s\n",
			con->arr[i].name,
			con->arr[i].sex,
			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].sex);

	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("%3s %2s %d %6s %s\n",
		con->arr[find].name,
		con->arr[find].sex,
		con->arr[find].age,
		con->arr[find].tel,
		con->arr[find].addr
	);
}

test.c

#include"SeqList.h"

void menu()
{
	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 {
		menu();
		printf("请输入您的操作:\n");
		scanf("%d", &op);
		//根据不同的选择执行不同的操作
		switch (op)
		{
		case 1:
			ContactAdd(&con);
			break;
		case 2:
			ContactAdd(&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 (op != 0);

	ContactDestroy(&con);
	return 0;
}

效果如图

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
首先,我们需要定义通讯录的联系人实体类: ```java public class Contact { private String name; private String phoneNumber; private String email; public Contact(String name, String phoneNumber, String email) { this.name = name; this.phoneNumber = phoneNumber; this.email = email; } public String getName() { return name; } public String getPhoneNumber() { return phoneNumber; } public String getEmail() { return email; } @Override public String toString() { return "Name: " + name + ", Phone number: " + phoneNumber + ", Email: " + email; } } ``` 接下来,我们需要实现顺序表类: ```java public class ContactList { private Contact[] contacts; private int size; public ContactList(int capacity) { contacts = new Contact[capacity]; size = 0; } public int getSize() { return size; } public void addContact(Contact contact) { if (size == contacts.length) { throw new RuntimeException("Contact list is full"); } contacts[size++] = contact; } public Contact getContact(int index) { if (index < 0 || index >= size) { throw new RuntimeException("Invalid index"); } return contacts[index]; } public void removeContact(int index) { if (index < 0 || index >= size) { throw new RuntimeException("Invalid index"); } for (int i = index; i < size - 1; i++) { contacts[i] = contacts[i + 1]; } contacts[size - 1] = null; size--; } public void updateContact(int index, Contact contact) { if (index < 0 || index >= size) { throw new RuntimeException("Invalid index"); } contacts[index] = contact; } public Contact[] getAllContacts() { Contact[] result = new Contact[size]; for (int i = 0; i < size; i++) { result[i] = contacts[i]; } return result; } } ``` 然后,我们可以在主函数中使用顺序表类来实现通讯录: ```java public static void main(String[] args) { ContactList contactList = new ContactList(10); contactList.addContact(new Contact("Alice", "1234567890", "alice@example.com")); contactList.addContact(new Contact("Bob", "2345678901", "bob@example.com")); contactList.addContact(new Contact("Charlie", "3456789012", "charlie@example.com")); contactList.removeContact(1); contactList.updateContact(0, new Contact("Alice", "0987654321", "alice@example.com")); Contact[] contacts = contactList.getAllContacts(); for (Contact contact : contacts) { System.out.println(contact); } } ``` 输出结果: ``` Name: Alice, Phone number: 0987654321, Email: alice@example.com Name: Charlie, Phone number: 3456789012, Email: charlie@example.com ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值