引言
顺序表是一种基本的数据结构,它使用连续的存储空间来存放具有相同类型的数据元素。在许多编程语言中,顺序表可以用数组来实现。由于其元素在内存中紧密排列,顺序表支持快速地通过索引访问任何位置上的元素。
通讯录的实现
要求:
- 能够存放100人的信息
- 能够保存用户的名字,年龄,性别,电话和住址
- 可以增加,删除,修改,查找,显示联系人信息
接下来给出该项目各个文件的代码ovo
SeqList.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "contact.h"
typedef PeoInfo SLDataType;
typedef struct SeqList
{
SLDataType* arr;//指针
int capacity;//空间容量
int size;//有效数据个数
}SL;
//初始化和销毁
void SLInit(SL* ps);
void SLDestory(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);
//销毁顺序表
void SLDestroy(SL* ps);
SeqList.c
#include "SeqList.h"
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->capacity = 0;
ps->size = 0;
}//初始化
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}//打印元素
//扩容
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4: 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc");
return 1;
}
//扩容成功
ps->arr = tmp;
ps->capacity = newcapacity;
}
}
//头部插入
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 SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size] = x;
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];
}
ps->size--;
}
//尾部删除
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);//确保顺序表不为空
ps->size--;//使得尾部数据无效即可
}
//指定位置插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
for (int i =ps->size; i>pos; i--)//pos位置及以后的数据往后挪
{
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, SLDataType x)
//{
// assert(ps);
// for (int i = 0; i < ps->size; i++)
// {
// if (ps->arr[i] == x)
// {
// return i;//找到了返回下标位置
// }
// }
// return -1;
//}
//销毁
void SLDestroy(SL* ps)
{
assert(ps);
if (ps->arr)
{
free(ps->arr);//不为空才释放
}
ps->arr = NULL;
ps->capacity = ps->size = 0;
}
contact.h
#define NAME_MAX 100
#define SEX_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100
//前置声明
typedef struct SeqList contact;//把SeqList重命名为contact ,顺序表变通讯录
//用户数据
typedef struct PersonInfo
{
char name[100];
int age;
char gender[10];
char tel[20];
char addr[20];
}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 "SeqList.h"
void LoadContact(contact* con)
{
FILE* pf = fopen("contact.txt", "rb");
if (pf == NULL)
{
perror("fopen error\n");
return;
}
//循环读取数据文件
PeoInfo info;
while (fread(&info, sizeof(PeoInfo), 1, pf))
{
SLPushBack(con, info);
}
printf("历史数据导入通讯录成功");
}
void InitContact(contact* con)
{
SLInit(con);
LoadContact(con);
}
//添加到通讯录
void AddContact(contact* con)
{
PeoInfo 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(con, info);
printf("插入成功\n");
}
//展示通讯录
void ShowContact(contact* con)
{
printf("%s %s %s %s %s\n", "姓名", "年龄", "性别", "电话", "地址");
for (int i = 0; i < con->size;i++)
{
printf("%s %d %s %s %s\n",
con->arr[i].name,
con->arr[i].age,
con->arr[i].gender,
con->arr[i].tel,
con->arr[i].addr);
}
}
//由于在删除 修改之前都要先看看这个数据是否存在,以姓名为查找依据
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;//找不到返回-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;
}
SLErase(con, pos);
printf("删除成功\n");
}
//添加某一元素到通讯录
void ModifyContact(contact* con)
{
char name[NAME_MAX];
printf("请输入要修改的用户的姓名:\n");
scanf("%s", name);
int pos = FindByName(con, name);
if (pos < 0)
{
printf("要修改的用户不存在,修改失败\n");
return;
}
PeoInfo info;
printf("请输入姓名:\n");
scanf("%s", &con->arr[pos].name);
printf("请输入年龄:\n");
scanf("%d", &con->arr[pos].age);
printf("请输入性别:\n");
scanf("%s", &con->arr[pos].gender);
printf("请输入电话:\n");
scanf("%s", &con->arr[pos].tel);
printf("请输入地址:\n");
scanf("%s", &con->arr[pos].addr);
printf("修改成功\n");
}
//查找某一元素
void FindContact(contact* con)
{
char name[NAME_MAX];
printf("请输入要查找的用户的姓名:\n");
scanf("%s", name);
int pos = FindByName(con, name);
if (pos < 0)
{
printf("要查找的用户不存在,查找失败\n");
return;
}
printf("查找成功\n");
printf("%s %d %s %s %s\n",
con->arr[pos].name,
con->arr[pos].age,
con->arr[pos].gender,
con->arr[pos].tel,
con->arr[pos].addr);
}
//在要销毁通讯录时保存数据
void SaveContact(contact* con)
{
FILE* pf = fopen("contact.txt", "wb");
if (pf == NULL)
{
perror("fopen error\n");
return;
}
//将通讯录数据写入文件
for (int i = 0; i < con->size; i++)
{
fwrite(con->arr + i, sizeof(PeoInfo), 1, pf);
}
printf("通讯录数据保存成功\n");
}
//销毁通讯录
void DestroyContact(contact* con)
{
SaveContact(con);
SLDestroy(con);
}
test.c
#include "SeqList.h"
#define _CRT_SECURE_NO_WARNINGS 1
void menu()
{
//通讯录初始化
contact con;
InitContact(&con);
int op = -1;
do
{
printf("**************通讯录*************\n");
printf("*****1.添加用户 2.删除用户*****\n");
printf("*****3.查找用户 4.修改用户*****\n");
printf("*****5.展示用户 0.退出*********\n");
printf("*********************************\n");
printf("请选择你的操作:\n");
scanf_s("%d", &op);
switch (op)
{
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;
case 0:
return;
default:
printf("非法输入,请重新输入\n");
break;
}
} while (op != 0);
//退出以后 销毁通讯录
DestroyContact(&con);
}
int main()
{
menu();
return 0;
}
简单的通讯录小项目就完成啦!感兴趣的小伙伴快去试试看吧~~