顺序表应用——通讯录实现

目录

前言:

一、开始菜单

二、通讯录代码实现

        2.1 通讯录初始化

        2.2 增加联系人

        2.3 删除联系人

        2.4  修改联系人

        2.5 通讯录的销毁

三、总代码

最后:


前言:

        通讯录实现的基础是基于顺序表的实现,对于顺序表如有疑惑之处可翻看之前文章。

一、开始菜单

        菜单的打印类似于之前游戏开始菜单,这里直接上代码,不再过多介绍:

void menu()
{
	printf("********************************\n");
	printf("*****1、添加⽤⼾ 2、删除⽤⼾*****\n");
	printf("*****3、查找⽤⼾ 4、修改⽤⼾*****\n");
	printf("*****5、展⽰⽤⼾ 0、退出 *****\n");
	printf("********************************\n");

}
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			
			break;
		case 2:
			
			break;
		case 3:
			
			break;
		case 4:
			
			break;
		case 5:
			
			break;
		default:
			printf("输⼊有误,请重新输⼊\n");
			break;

		}
	} while (input);
	return 0;
}

        这便是一个简易的操作界面,我们逐渐往里面扩充。

二、通讯录代码实现

        通讯录大家都见过,无外乎有这几大功能:增加联系人,删除联系人,修改联系人,查找联系人,我们将围绕这几大功能进行实现。

        2.1 通讯录初始化

        通讯录的初始化与顺序表相同,所以我们可直接调用,代码如下:

void InitContact(contact* con)
{
	 SeqListInit( con);
}

        2.2 增加联系人

        增加联系人与顺序表的插入原理相同,只需简单修改即可,大家可在这里自行选择头插或尾插,实现代码如下:

void AddContact(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);
	SeqListPushBack(con, info);
	printf("插⼊成功!\n");
}

        2.3 删除联系人

        在我们进行删除联系人之前,我们应该进行查找是否存在,存在了我们在对其进行删除,在查找时可对其姓名、性别等进行查找,这里以姓名进行查找,代码实现如下:

int FindByName(contact* con, char name[]) {
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->a[i].name, name)) {
			return i;
		}
	}
	return -1;
}
void DelContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输⼊要删除的⽤⼾姓名:\n");
	scanf("%s", name);
	int pos = FindByName(con, name);
	if (pos < 0)
	{
		printf("要删除的⽤⼾不存在,删除失败!\n");
		return;
	}
	SeqListErase(con, pos);
	printf("删除成功!\n");
}

        2.4  修改联系人

        修改原理与删除类似都是先查找,在进行操作,代码实现如下:

void ModifyContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输入要修改的名字: ");
	scanf("%s", name);
	int pos = FindByName(con, name);
	if (pos < 0)
	{
		printf("要删除的⽤⼾不存在,删除失败!\n");
		return;
	}
	PeoInfo info;
	printf("请输⼊要修改的姓名:\n");
	scanf("%s", &con->a[pos].name);
	printf("请输⼊要修改的性别:\n");
	scanf("%s", &con->a[pos].sex);
	printf("请输⼊要修改的年龄:\n");
	scanf("%d", &con->a[pos].age);
	printf("请输⼊要修改的联系电话:\n");
	scanf("%s", &con->a[pos].tel);
	printf("请输⼊要修改的地址:\n");
	scanf("%s", &con->a[pos].addr);
	printf("修改成功!\n");
}

        2.5 通讯录的销毁

        销毁与初始化类似,这里不过多讲解,代码如下:

void DestroyContact(contact* con)
{
	SeqListDesTroy(con);
}

三、总代码

        test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
#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;
	InitContact(&con);
	int input = 0;
	do
	{
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			AddContact(&con);
			break;
		case 2:
			DelContact(&con);
			break;
		case 3:
			FindContact(&con);
			break;
		case 4:
			ModifyContact(&con);
			break;
		case 5:
			ShowContact(&con);
			break;
		default:
			printf("输⼊有误,请重新输⼊\n");
			break;
		}
	} while (input);
	return 0;
}

        seqlist.h

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h> 
#include"contact.h"
//数据类型为PersonInfo
typedef struct PersonInfo SQDataType;
//typedef int SQDataType;
//动态顺序表
typedef struct SeqList {
	SQDataType* a;
	int size;//保存有效数据个数
	int capacity;//空间的⼤⼩
}SLT;
//初始化与销毁
void SeqListInit(SLT* psl);
void SeqListDesTroy(SLT* psl);
void SeqListPrint(SLT sl);
void CheckCapacity(SLT* psl);
// 头部插⼊删除 / 尾部插⼊删除
void SeqListPushBack(SLT* psl, SQDataType x);
void SeqListPushFront(SLT* psl, SQDataType x);
void SeqListPopBack(SLT* psl);
void SeqListPopFront(SLT* psl);
//查找
int SeqListFind(SLT* psl, SQDataType x);
// 在指定位置之前插⼊/删除
//void SeqListInsert(SLT* psl, int pos, SQDataType x);
void SeqListInsert(SLT* psl, size_t pos, SQDataType x);
void SeqListErase(SLT* psl, size_t pos);
size_t SeqListSize(SLT* psl);
//修改指定位置的值
void SeqListAt(SLT* psl, size_t pos, SQDataType x);

        seqlist.c

#include "SeqList.h"

void SLInit(SLT* ps) {
    assert(ps);
    ps->a == NULL;
    ps->capacity = ps->size = 0;
}
void SLDestroy(SLT* ps) {
    assert(ps);
    if (ps->a) {
        free(ps->a);
    }
    ps->a = NULL;
    ps->size = ps->capacity = 0;
}

//void SLPrint(SL* ps) {
//    assert(ps);
//    for (int i = 0; i < ps->size; ++i) {
//        printf("%d ", ps->a[i]);
//    }
//    printf("\n");
//}
void SLCheckCapacity(SLT* ps) {
    assert(ps);
    if (ps->size == ps->capacity) {
        SQDataType* tmp = (SQDataType*)realloc(ps->a, sizeof(SQDataType) * ps->capacity * 2);
        if (tmp == NULL) {
            perror("realloc fail!\n");
            exit(1);
        }
        ps->a = tmp;
        ps->capacity *= 2;
    }
}
void SLPushBack(SLT* ps, SQDataType x) {
    assert(ps);
    //    SLCheckCapacity(ps);
    //    ps->a[ps->size++] = x;
    SLInsert(ps, ps->size, x);
}
void SLPopBack(SLT* ps) {
    assert(ps);
    assert(ps->size > 0);
    //    --ps->size;
    SLErase(ps, ps->size - 1);
}
void SLPushFront(SLT* ps, SQDataType x) {
    assert(ps);
    //    SLCheckCapacity(ps);
    //    for (int i = ps->size; i > 0; --i) {
    //        ps->a[i] = ps->a[i-1];
    //    }
    //    ps->a[0] = x;
    //    ++ps->size;
    SLInsert(ps, 0, x);
}
void SLPopFront(SLT* ps) {
    assert(ps);
    assert(ps->size > 0);
    //    for (int i = 0; i < ps->size-1; ++i) {
    //        ps->a[i] = ps->a[i+1];
    //    }
    //    --ps->size;
    SLErase(ps, 0);
}
//指定位置之前插入数据
void SLInsert(SLT* ps, int pos, SQDataType x) {
    assert(ps);
    assert(pos <= ps->size && pos >= 0);
    SLCheckCapacity(ps);
    for (int i = ps->size; i > pos; --i) {
        ps->a[i] = ps->a[i - 1];
    }
    ps->a[pos] = x;
    ++ps->size;
}
void SLErase(SLT* ps, int pos) {
    assert(ps);
    assert(pos < ps->size && pos >= 0);
    for (int i = pos; i < ps->size - 1; ++i) {
        ps->a[i] = ps->a[i + 1];
    }
    --ps->size;
}
//int SLFind(SL* ps, SLDataType x) {
//    assert(ps);
//    for (int i = 0; i < ps->size; ++i) {
//        if (ps->a[i] == x) {
//            return i;
//        }
//    }
//    return -1;
//}

        contact.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"seqlist.h"
#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 InitContact(contact* con);

//添加通讯录数据

void AddContact(contact* con);

//删除通讯录数据

void DelContact(contact* con);

//展示通讯录数据

void ShowContact(contact* con);

//查找通讯录数据

void FindContact(contact* con);

//修改通讯录数据

void ModifyContact(contact* con);

//销毁通讯录数据

void DestroyContact(contact* con);

        contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
#include"seqlist.h"
void InitContact(contact* con)
{
	 SeqListInit( con);
}

void AddContact(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);
	SeqListPushBack(con, info);
	printf("插⼊成功!\n");
}
int FindByName(contact* con, char name[]) {
	for (int i = 0; i < con->size; i++)
	{
		if (0 == strcmp(con->a[i].name, name)) {
			return i;
		}
	}
	return -1;
}
void DelContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输⼊要删除的⽤⼾姓名:\n");
	scanf("%s", name);
	int pos = FindByName(con, name);
	if (pos < 0)
	{
		printf("要删除的⽤⼾不存在,删除失败!\n");
		return;
	}
	SeqListErase(con, pos);
	printf("删除成功!\n");
}
void ShowContact(contact* con) 
{
	printf("%-10s %-4s %-4s %15s %-20s\n", "姓名", "性别", "年龄", "联系电话", "地址");
		for (int i = 0; i < con->size; i++)
		{
			printf("%-10s %-4s %-4d %15s %-20s\n",
				con->a[i].name,
				con->a[i].sex,
				con->a[i].age,
				con->a[i].tel,
				con->a[i].addr);
		}
}
void ModifyContact(contact* con)
{
	char name[NAME_MAX];
	printf("请输入要修改的名字: ");
	scanf("%s", name);
	int pos = FindByName(con, name);
	if (pos < 0)
	{
		printf("要删除的⽤⼾不存在,删除失败!\n");
		return;
	}
	PeoInfo info;
	printf("请输⼊要修改的姓名:\n");
	scanf("%s", &con->a[pos].name);
	printf("请输⼊要修改的性别:\n");
	scanf("%s", &con->a[pos].sex);
	printf("请输⼊要修改的年龄:\n");
	scanf("%d", &con->a[pos].age);
	printf("请输⼊要修改的联系电话:\n");
	scanf("%s", &con->a[pos].tel);
	printf("请输⼊要修改的地址:\n");
	scanf("%s", &con->a[pos].addr);
	printf("修改成功!\n");
}
void DestroyContact(contact* con)
{
	SeqListDesTroy(con);
}

最后:

        本篇文章是基础顺序表的扩展,如若觉得不大理解,可翻阅顺序表,顺序表明白了此篇文章也会明白的。

完!

  • 30
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要定义通讯录的联系人实体类: ```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 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值