【数据结构】_基于顺序表实现通讯录
目录
1. 自定义类型设计
2. 目录结构及头文件的包含
3. 通讯录实现
3.1 Contact.h
3.2 Contact.c
3.3 Test_Contact.c
关于存储基本类型的顺序表的实现,详见下文:
【数据结构】_顺序表-CSDN博客文章浏览阅读568次,点赞22次,收藏9次。线性表是n个具有相同特性的数据元素的有限序列。常见的线性表有:顺序表、链表、栈、队列、字符串等;。线性表在物理上存储时,通常以数组和链式结构的形式存储,分别称之为顺序表和链表。本文介绍顺序表。https://blog.csdn.net/m0_63299495/article/details/145301073本文介绍存储自定义类型的顺序表,以实现通讯录为例。
1. 自定义类型设计
#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;
2. 目录结构及头文件的包含
1、基于已实现的SeqList.h与SeqList.c文件,新建以下三个文件:
Contact.h文件:通讯录结构及通讯录方法的声明;
Contact.c文件:通讯录方法的实现;
Test_Contact.c:测试通讯录方法;
(SeqList.h与SeqList.c文件见本文篇首链接文章原文)
2、关于Contact.h与SeqList.h的包含:
由于在SeqList中,需将原始存储基本类型int修改为存储自定义类型结构体peoInfo:
// SeqList.h
typedef struct personInfo SLDataType;
故需在SeqList.h中包含Contact.h;
而在Contact.h中不能再包含SeqList.h,否则就会导致头文件的交叉包含,可采用前置声明:
// Contact.h
typedef struct SeqList Contact;
3. 通讯录实现
3.1 Contact.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#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 ContactDestory(Contact* con);
void ContactAdd(Contact* con);
void ContactDel(Contact* con);
void ContactModify(Contact* con);
int ContactFindByName(Contact* con,char name[]);
void ContactFind(Contact* con);
void ContactShow(Contact* con);
3.2 Contact.c
#define _CRT_SECURE_NO_WARNINGS
#include"Contact.h"
#include"SeqList.h"
#include<string.h>
void ContactInit(Contact* con) {
SLInit(con);
}
void ContactDestory(Contact* con) {
SLDestory(con);
}
void ContactAdd(Contact* con) {
// 获取联系人信息
peoInfo person;
printf("Please input name:\n");
scanf("%s", person.name);
printf("Please input gender:\n");
scanf("%s", person.gender);
printf("Please input age:\n");
scanf("%d", &person.age);
printf("Please input Telephone number:\n");
scanf("%s", person.tel);
printf("Please input address:\n");
scanf("%s", person.addr);
// 将联系人信息插入通讯录
SLPushBack(con, person);
printf("Add successfully!\n");
}
void ContactDel(Contact* con) {
// 先查找
char name[NAME_MAX];
printf("Please input the name of the contact to be deleted:\n");
scanf("%s", name);
int findRet = ContactFindByName(con,name);
if (findRet < 0) {
printf("The contact is not exit!\n");
return;
}
// 删除
SLErase(con, findRet);
printf("Delete Successfully!\n");
}
void ContactModify(Contact* con) {
// 先查找
char name[NAME_MAX];
printf("Please input the name of the contact to be modified:\n");
scanf("%s", name);
int findRet = ContactFindByName(con, name);
if (findRet < 0) {
printf("The contact is not exit!\n");
return;
}
// 修改
printf("Please input the name to be modified:\n");
scanf("%s", con->arr[findRet].name);
printf("Please input the gender to be modified:\n");
scanf("%s", con->arr[findRet].gender);
printf("Please input the age to be modified:\n");
scanf("%d", &con->arr[findRet].age);
printf("Please input the telephone to be modified:\n");
scanf("%s", con->arr[findRet].tel);
printf("Please input the address to be modified:\n");
scanf("%s", con->arr[findRet].addr);
printf("Modify Successfully!\n");
}
int ContactFindByName(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 ContactFind(Contact* con) {
char name[NAME_MAX];
printf("Please input the name of the contact to be found:\n");
scanf("%s", name);
int findRet = ContactFindByName(con,name);
if (findRet < 0) {
printf("");
return;
}
printf("%4s %4s %1s %4s %5s\n", "Name", "Gender", "Age", "Tele", "Address");
printf("%s %s %d %s %s\n",
con->arr[findRet].name,
con->arr[findRet].gender,
con->arr[findRet].age,
con->arr[findRet].tel,
con->arr[findRet].addr);
}
void ContactShow(Contact* con) {
assert(con);
printf("%4s %4s %1s %4s %5s\n", "Name", "Gender", "Age", "Tele", "Address");
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);
}
}
3.3 Test_Contact.c
#define _CRT_SECURE_NO_WARNINGS
#include"Contact.h"
#include"SeqList.h"
void menu() {
printf("********** AddressBook **********\n");
printf("******** 1.Add 2.Delete ******\n");
printf("******** 3.Modify 4.Find ********\n");
printf("******** 5.Show 0.Exit ********\n");
}
int main() {
int option = -1;
Contact con;
ContactInit(&con);
do {
menu();
printf("Please choose:\n");
scanf("%d", &option);
switch (option)
{
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("Address Book exit.");
break;
default:
printf("Option error!Please input once more.\n");
break;
}
} while (option);
ContactDestory(&con);
return 0;
}