
本节内容讲解顺序表以及使用顺序表实现通讯录一 顺序表1.顺序表的简介顺序表是线性表的一种其逻辑结构和物理结构都为线性底层逻辑为数组。顺序表可以分为静态顺序表和动态顺序表实际更多使用到动态顺序表。typedef peoInfo SLDataType; //顺序表的定义 typedef struct SeqList { SLDataType* arr; int size; int capacity; }SL;2.顺序表的功能顺序表可以实现增删查改等功能以及对顺序表初始化和销毁1在头文件中声明//顺序表的初始化与销毁 void SLInit(SL* ps); void SLDestory(SL* ps); //顺序表的插入 void SLCheckCapacity(SL* ps); void SLPushBack(SL* ps, SLDataType x); void SLPushFront(SL* ps, SLDataType x); //顺序表的删除 void SLPopBack(SL* ps); void SLPopFront(SL* ps); //在指定位置插入 void SLInsert(SL* ps, int pos, SLDataType x); //删除指定位置数据 void SLErase(SL* ps, int pos); //顺序表的打印 void SLPrint(SL s); //查找 int SLFind(SL* ps,SLDataType x);2在.c文件实现#include SeqList.h //顺序表初始化与销毁 void SLInit(SL *ps) { ps-arr NULL; ps-size ps-capacity 0; } void SLDestory(SL* ps) { if (ps-arr) { free(ps-arr); } ps-arr NULL; ps-size ps-capacity 0; } //顺序表的增容 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 fail!); exit(1); } ps-arr tmp; ps-capacity newCapacity; } } //顺序表的头插尾插 void SLPushBack(SL* ps, SLDataType x) { assert(ps); SLCheckCapacity(ps); ps-arr[ps-size] x; ps-size; } 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 SLPopBack(SL* ps) { assert(ps); assert(ps-size); 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 SLInsert(SL* ps, int pos, SLDataType x) { assert(ps); assert(pos 0 pos ps-size); SLCheckCapacity(ps); for (int i ps-size;ipos; i--) { 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 SLPrint(SL s) //{ // for (int i 0; i s.size; i) // { // printf(%d , s.arr[i]); // } // printf(\n); //}二 通讯录的实现通过对顺序表功能的包装和修改可以实现通讯录1在头文件声明#pragma once #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); //联系人的查找 void ContactFind(Contact* con); //展示通讯录 void ContactShow(Contact* con);2在.c文件实现#include Contact.h #include SeqList.h //通讯录的初始化与销毁 void ContactInit(Contact* con) { SLInit(con); } void ContactDestory(Contact* con) { SLDestory(con); } //联系人的增加 void ContactAdd(Contact* con) { peoInfo info; printf(请输入要添加的联系人姓名\n); scanf(%s, info.name); printf(请输入要添加的联系人性别\n); scanf(%s, info.gender); 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); } else { SLErase(con, find); } } //联系人的修改 void ContactModify(Contact* con) { char name[NAME_MAX]; printf(请输入你要修改的联系人姓名\n); scanf(%s, name); int find Findbyname(con, name); if (find 0) { printf(要修改的联系人不存在\n); } else { printf(请输入新的联系人姓名\n); scanf(%s, con-arr[find].name); printf(请输入新的联系人性别\n); scanf(%s, con-arr[find].gender); 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); } else { printf(姓名 性别 年龄 电话 地址\n); printf(%s %s %d %s %s\n, con-arr[find].name, con-arr[find].gender, con-arr[find].age, con-arr[find].tel, con-arr[find].addr); } } //展示通讯录 void ContactShow(Contact* con) { printf(姓名 性别 年龄 电话 地址\n); 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在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() { Contact con; ContactInit(con); int input; do { menu(); printf(请选择你的操作\n); scanf(%d, input); switch (input) { 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; default: break; } } while (input); ContactDestory(con); return 0; }