【数据结构】顺序表(实现+详解+源码+通讯录项目(静态+动态+文件保存))
目录
概念及结构
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
- 静态顺序表:使用定长数组储存元素。
- 动态顺序表:使用动态开辟的数组储存。
接口实现
- #include<stdio.h>
- #include<assert.h>
- #include<stdlib.h>
-
-
- typedef int SLDataType;
-
- //顺序表的静态储存
- //#define MAX 100 //静态通讯录总容量
- //typedef struct SeqList
- //{
- // SLDataType array[MAX];
- // size_t size;
- //}SeqList;
-
- // 顺序表的动态存储
- #define N 5 //第一次开辟容量大小
- typedef struct SeqList
- {
- SLDataType* array; // 指向动态开辟的数组
- size_t size ; // 有效数据个数
- size_t capicity ; // 容量空间的大小
- }SeqList;
-
-
- // 基本增删查改接口
- // 顺序表初始化
- void SeqListInit(SeqList* ps);
- // 检查空间,如果满了,进行增容
- void CheckCapacity(SeqList* ps);
- // 顺序表尾插
- void SeqListPushBack(SeqList* ps, SLDataType x);
- // 顺序表尾删
- void SeqListPopBack(SeqList* ps);
- // 顺序表头插
- void SeqListPushFront(SeqList* ps, SLDataType x);
- // 顺序表头删
- void SeqListPopFront(SeqList* ps);
- // 顺序表查找
- int SeqListFind(SeqList* ps, SLDataType x);
- // 顺序表在pos位置插入x
- void SeqListInsert(SeqList* ps, size_t pos, SLDataType x);
- // 顺序表删除pos位置的值
- void SeqListErase(SeqList* ps, size_t pos);
- // 顺序表销毁
- void SeqListDestory(SeqList* ps);
- // 顺序表打印
- void SeqListPrint(SeqList* ps);
顺序表初始化
- #include<stdio.h>
- #include<assert.h>
- #include<stdlib.h>
-
-
- typedef int SLDataType;
-
- //顺序表的静态储存
- //#define MAX 100 //静态通讯录总容量
- //typedef struct SeqList
- //{
- // SLDataType array[MAX];
- // size_t size;
- //}SeqList;
-
- // 顺序表的动态存储
- #define N 5 //第一次开辟容量大小
- typedef struct SeqList
- {
- SLDataType* array; // 指向动态开辟的数组
- size_t size ; // 有效数据个数
- size_t capicity ; // 容量空间的大小
- }SeqList;
-
-
- // 基本增删查改接口
- // 顺序表初始化
- void SeqListInit(SeqList* ps);
- // 检查空间,如果满了,进行增容
- void CheckCapacity(SeqList* ps);
- // 顺序表尾插
- void SeqListPushBack(SeqList* ps, SLDataType x);
- // 顺序表尾删
- void SeqListPopBack(SeqList* ps);
- // 顺序表头插
- void SeqListPushFront(SeqList* ps, SLDataType x);
- // 顺序表头删
- void SeqListPopFront(SeqList* ps);
- // 顺序表查找
- int SeqListFind(SeqList* ps, SLDataType x);
- // 顺序表在pos位置插入x
- void SeqListInsert(SeqList* ps, size_t pos, SLDataType x);
- // 顺序表删除pos位置的值
- void SeqListErase(SeqList* ps, size_t pos);
- // 顺序表销毁
- void SeqListDestory(SeqList* ps);
- // 顺序表打印
- void SeqListPrint(SeqList* ps);
静态版:因为在定义结构体时已经创建了固定容量的数组,所以在初始化时只需要将数组元素置为 0,再将有效元素个数置为0即可。
动态版:在创建结构体时结构体成员中只创建了指向顺序表的指针,并没有创建空间,所以在初始化时可以使用两种方法初始化:
- 初始化时创建一定容量的空间,当然在后面空间检查时会有一些变化。
- 初始化时不创建空间,将指针置为空。
这里我们使用第二种方法:
- //静态版
- void SeqListInit(SeqList* ps)
- {
- memset(ps->array,0,sizeof(ps->array[0])*N);//将数组所有元素置为0
- ps->size = 0;//数组中的有效元素置为0
- }
-
-
- //动态版
- void SeqListInit(SeqList* ps)
- {
- ps->a = NULL;//将指向数组的指针置为空
- ps->capacity = 0;//数组容量为0
- ps->size = 0;//数组中的有效元素为0
- }
检查空间(动态)
空间检查只会在动态版中有,静态顺序表的空间大小是固定的,不需要扩容,容量满了之后不能插入元素。
- void CheckCapacity(SeqList* ps)
- {
- if (ps->capacity == ps->size)//当空间容量和有效元素相等时才需要增容
- {
- if (ps->capacity == 0)//当空间容量为0时
- {
- ps->a = (SLDataType*)malloc(sizeof(SLDataType) * N);
- if (ps->a == NULL)
- {
- printf("malloc erro");//开辟失败
- exit(-1);
- }
- ps->capacity = N;
- }
- else
- {
- SLDataType* tmp = (SLDataType*)realloc(ps->a ,sizeof(SLDataType) * ps->capacity * 2);//空间容量不够时
- if (tmp == NULL)
- {
- printf("realloc erro");
- exit(-1);
- }
- ps->a = tmp;
- ps->capacity *= 2;//增容2倍
-
- }
- }
顺序表尾插
尾插即在顺序表的最后一个有效元素的后面一个位置插入。
- void SeqListPushBack(SeqList* ps, SLDataType x)
- {
- SeqListCheckCapacity(ps);//插入元素时需先检查容量是否足够
- ps->a[ps->size] = x;//直接在最后插入即可
- ps->size++;//有效元素增加1
- }
顺序表头插
头插即在顺序表的第一个位置插入元素,但第一个位置是有元素的,所以需要将顺序表中所有元素向后移动一个位置。
- void SeqListPushFront(SeqList* ps, SLDataType x)
- {
- SeqListCheckCapacity(ps);//空间检查
- //1.可直接使用memmove移动
- memmove(ps->a + 1, ps->a, sizeof(SLDataType) * ps->size);
- //2.依次移动
- int i = 0;
- for(i = ps->size; i>0; i--)//注意数组不要越界
- {
- ps->array[i] = ps->array[i-1];
- }
- ps->a[0] = x;//在第一个位置插入
- ps->size++;
- }
顺序表尾删
直接删除最后一个元素,并将有效元素个数减一即可。
- void SeqListPopBack(SeqList* ps)
- {
- assert(ps->size > 0);//删除必须保证数组中有有效元素
- ps->size --;//直接将有效元素个减1即可
- } //注:不要将该位置置为0,因为可能该位置处原来放的就是0
顺序表头删
删除第一个元素,再将剩余元素像左移动一个位置。
- void SeqListPopFront(SeqList* ps)
- {
- assert(ps->size > 0);//同样删除需保证数组中存在有效元素
- //1.通过memmove移动
- memmove(ps->a, ps->a + 1, sizeof(SLDataType) * (ps->size - 1));
- //2.依次移动
- int i = 0;
- for(i = 0; i < ps->szie-1; i++)
- {
- ps->array[i] = ps->array[i+1];
- }
- ps->size--;//元素个数减1
- }
顺序表查找
- int SeqListFind(SeqList* ps, SLDataType x)
- {
- int i = 0;
- for (i = 0; i < ps->size; i++)//因为不确定数组中的元素是否排序,所以只能依次查找
- {
- if (ps->a[i] == x)
- {
- return i;//找到返回该元素在数组中的下标
- //注:这里没有考虑重复元素的情况
- }
- }
-
- return -1;//未找到返回-1;
- }
顺序表在pos位置插入x
将指定位置处向右所有的元素向后移动一个位置,将x插入下标为pos位置处。
- void SeqListInsert(SeqList* ps, int pos, SLDataType x)
- {
- SeqListCheckCapacity(ps);//同样增加元素需先检查容量
-
- //保证输入的下标是有效的
- //1.温柔方法
- if (pos > ps->size || pos < 0)
- {
- printf("输入位置信息错误
");
- return;
- }
- //2.暴力方法
- //assert(pos <= ps->size && pos >= 0)
-
- //将该位置和该位置以后的元素向后移动一个空间
- int i = 0;
- for (i = ps->size - 1; i >= pos; i--)
- {
- ps->a[i + 1] = ps->a[i];
- }
- ps->a[pos] = x;
- ps->size++;
- }
顺序表删除pos位置的值
同理,将pos位置处右边所有元素向左移动一个位置,覆盖pos位置处的值。
- void SeqListErase(SeqList* ps, int pos)
- {
- assert(ps->size > 0 && pos >= 0 && pos < ps->size - 1);//保证数组中的有效元素不为0,且输入的下标有效
- int i = 0;
- for (i = pos + 1; i < ps->size; i++)//将该位置以后的元素向前移动一个位置,将该位置覆盖
- {
- ps->a[i - 1] = ps->a[i];
- }
- ps->size--;
- }
顺序表销毁(动态)
动态创建的空间在程序结束后必须将该空间释放,不然会导致内存泄漏。
- void SeqListDestory(SeqList* ps)
- {
- free(ps->a);//释放开辟的空间
- ps->a = NULL;//指针置为空
- ps->capacity = ps->size = 0;//空间容量和有效元素都置为0
- }
顺序表打印
- void SeqListPrint(SeqList* ps)
- {
- int i = 0;
- for (i = 0; i < ps->size; i++)
- {
- printf("%%%%d ", ps->a[i]);
- }
- printf("
");
- }
通讯录实现
Contact.h
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
-
- #define NAME_MAX 10
- #define ADDR_MAX 10
- #define TELE_MAX 12
- #define CONTACT_MAX 1000
- #define CAPACITY 3
-
-
-
- struct PeopleInfo
- {
- char name[NAME_MAX];
- int age;
- char addr[ADDR_MAX];
- char tele[TELE_MAX];
- };
-
- //静态
- //struct Contact
- //{
- // struct PeopleInfo data[CONTACT_MAX];
- // int size;
- //};
-
- //动态
- struct Contact
- {
- struct PeopleInfo* data;
- int size;//通讯录中有效元素个数
- int capacity;//通讯录容量
- };
-
-
- //初始化通讯录
- void InitContact(struct Contact* pc);
-
- //通讯录扩容
- void BuyContact(struct Contact* pc);
-
- //增加联系人
- void AddContact(struct Contact* pc);
-
- //删除联系人
- void DelContact(struct Contact* pc);
-
- //查找联系人
- void SearchContact(const struct Contact* pc);
-
- //修改联系人
- void ModifyContact(struct Contact* pc);
-
- //排序通讯录列表(姓名)
- void SortContact(struct Contact* pc);
-
- //显示通讯录
- void ShowContact(const struct Contact* pc);
-
- //销毁通讯录
- void DestoryContact(struct Contact* pc);
-
- //保存信息到通讯录中
- void SaveContact(struct Contact* pc);
-
-
- //加载文件中的信息到通讯录中
- void LoadContact(struct Contact* pc);
Contact.c
- #include"Contact.h"
-
-
- //通讯录扩容
- void BuyContact(struct Contact* pc)
- {
- if (pc->size == pc->capacity)
- {
- struct PeopleInfo* tmp = (struct PeopleInfo*)realloc(pc->data, sizeof(struct PeopleInfo) * (pc->capacity) * 2);
- if (tmp == NULL)
- {
- perror("BuyContact::fail");
- return;
- }
- pc->data = tmp;
- pc->capacity *= 2;
- printf("增容成功
");
- }
- }
-
-
- void LoadContact(struct Contact* pc)
- {
- //打开文件
- FILE* pf = fopen("Contact.txt", "rb");
- if (pf == NULL)
- {
- perror("LoadContact::fopen");
- return;
- }
-
- //读文件
- struct PeopleInfo tmp = { 0 };
- while (fread(&tmp, sizeof(struct PeopleInfo), 1, pf))
- {
- BuyContact(pc);
- pc->data[pc->size] = tmp;
- pc->size++;
- }
-
- //关闭文件
- fclose(pf);
- pf = NULL;
- }
-
-
- //初始化通讯录(静态)
- //void InitContact(struct Contact* pc)
- //{
- // memset(pc->data, 0, sizeof(struct PeopleInfo) * CONTACT_MAX);
- // pc->size = 0;
- //}
-
- //初始化通讯录(动态)
- void InitContact(struct Contact* pc)
- {
- pc->data = (struct PeopleInfo*)malloc(sizeof(struct PeopleInfo) * CAPACITY);
- pc->capacity = CAPACITY;
- pc->size = 0;
-
- //加载文件信息到通讯录
- LoadContact(pc);
- }
-
-
-
-
- //添加联系人
- void AddContact(struct Contact* pc)
- {
- //静态
- /*if (pc->size >= CONTACT_MAX)
- {
- printf("通讯录已满
");
- return;
- }*/
-
- //动态(通讯录满了需增容)
- BuyContact(pc);
-
- printf("请输入名字>");
- scanf("%%%%s", pc->data[pc->size].name);
- printf("请输入年龄>");
- scanf("%%%%d", &(pc->data[pc->size].age));
- printf("请输入住址>");
- scanf("%%%%s", pc->data[pc->size].addr);
- printf("请输入电话>");
- scanf("%%%%s", pc->data[pc->size].tele);
- printf("添加成功!
");
- pc->size++;
- }
-
-
- //显示通讯录列表
- void ShowContact(const struct Contact* pc)
- {
- if (pc->size == 0)
- {
- printf("通讯录为空
");
- exit(1);
- }
- printf(" %%%%-10s %%%%-3s %%%%-10s %%%%-12s
", "name", "age", "addr", "tele");
- int i = 0;
- for (i = 0; i < pc->size; i++)
- {
- printf(" %%%%-10s %%%%-3d %%%%-10s %%%%-12s
",
- pc->data[i].name,
- pc->data[i].age,
- pc->data[i].addr,
- pc->data[i].tele);
- }
- }
-
-
- int FindByName(const struct Contact* pc,const char*name)
- {
-
- int i = 0;
- for (i = 0; i < pc->size; i++)
- {
- if (strcmp(pc->data[i].name, name) == 0)
- {
- return i;
- }
- }
- return -1;
- }
-
- //删除联系人
- void DelContact(struct Contact* pc)
- {
- if (pc->size <= 0)
- {
- printf("通讯录为空
");
- return;
- }
- char name[NAME_MAX] = { 0 };
- printf("请输入需要删除的联系人>");
- scanf("%%%%s", name);
- int i = FindByName(pc,name);
- if (i != -1)
- {
- for (; i < pc->size - 1; i++)
- {
- pc->data[i] = pc->data[i + 1];
- }
- pc->size--;
- printf("删除成功
");
- }
- else
- {
- printf("无此联系人
");
- }
- }
-
-
- //查找联系人
- void SearchContact(const struct Contact* pc)
- {
- char name[NAME_MAX] = { 0 };
- printf("请输入需要查找的联系人>");
- scanf("%%%%s", name);
- int i = FindByName(pc, name);
- if(i!=-1)
- {
- printf(" %%%%-10s %%%%-3s %%%%-10s %%%%-12s
", "name", "age", "addr", "tele");
- printf(" %%%%-10s %%%%-3d %%%%-10s %%%%-12s
",
- pc->data[i].name,
- pc->data[i].age,
- pc->data[i].addr,
- pc->data[i].tele);
-
- }
- else
- {
- printf("无此联系人
");
- }
- }
-
-
- //修改联系人
- void ModifyContact(struct Contact* pc)
- {
- char name[NAME_MAX] = { 0 };
- printf("请输入需要修改的联系人>");
- scanf("%%%%s", name);
- int i = FindByName(pc, name);
- if (i != -1)
- {
- printf("请输入修改后的名字>");
- scanf("%%%%s", pc->data[i].name);
- printf("请输入修改后的年龄>");
- scanf("%%%%d", &(pc->data[i].age));
- printf("请输入修改后的住址>");
- scanf("%%%%s", pc->data[i].addr);
- printf("请输入修改后的电话>");
- scanf("%%%%s", pc->data[i].tele);
- }
- else
- {
- printf("无此联系人
");
- }
-
- }
-
-
- //排序通讯录列表(姓名)
-
- int cmp_name(const void* a, const void* b)
- {
- return strcmp(((struct PeopleInfo*)a)->name , ((struct PeopleInfo*)b)->name);
- }
-
- void SortContact(struct Contact* pc)
- {
- qsort(pc->data, pc->size, sizeof(struct PeopleInfo), cmp_name);
- printf("排序成功
");
- }
-
-
-
- //销毁通讯录
- void DestoryContact(struct Contact* pc)
- {
- free(pc->data);
- pc->data = NULL;
- pc->capacity = 0;
- pc->size = 0;
- }
-
- //保存信息到通讯录中
- void SaveContact(struct Contact* pc)
- {
- //打开文件
- FILE* pf = fopen("Contact.txt", "w");
- if (pf == NULL)
- {
- perror("SaveContact::fopen");
- exit(1);
- }
-
- //写文件
- int i = 0;
- for (i = 0; i < pc->size; i++)
- {
- fwrite(&(pc->data[i]), sizeof(struct PeopleInfo), 1, pf);
- }
- //关闭文件
- fclose(pf);
- pf = NULL;
- }
test.c
- #include"Contact.h"
-
- void menu()
- {
- printf("******************************
");
- printf("******* 1.add 2.del *******
");
- printf("******* 3.search 4.modify ****
");
- printf("******* 5.sort 6.show ******
");
- printf("********* 0.exit *************
");
- printf("******************************
");
-
- }
-
- enum contact
- {
- EXIT,
- ADD,
- DEL,
- SEARCH,
- MODIFY,
- SORT,
- SHOW
- };
-
- int main()
- {
- //创建通讯录
- struct Contact con;
- //初始化通讯录
- InitContact(&con);
- int input = 0;
- do
- {
- menu();
- printf("请选择功能>");
- scanf("%%%%d", &input);
- switch (input)
- {
- case EXIT:
- //保存信息
- SaveContact(&con);
- DestoryContact(&con);
- printf("退出通讯录
");
- break;
- case ADD:
- AddContact(&con);
- break;
- case DEL:
- DelContact(&con);
- break;
- case SEARCH:
- SearchContact(&con);
- break;
- case MODIFY:
- ModifyContact(&con);
- break;
- case SORT:
- SortContact(&con);
- break;
- case SHOW:
- ShowContact(&con);
- break;
- default:
- printf("选择错误,请重新输入
");
- break;
- }
- } while (input);
- return 0;
- }
顺序表的问题及思考
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
-
- #define NAME_MAX 10
- #define ADDR_MAX 10
- #define TELE_MAX 12
- #define CONTACT_MAX 1000
- #define CAPACITY 3
-
-
-
- struct PeopleInfo
- {
- char name[NAME_MAX];
- int age;
- char addr[ADDR_MAX];
- char tele[TELE_MAX];
- };
-
- //静态
- //struct Contact
- //{
- // struct PeopleInfo data[CONTACT_MAX];
- // int size;
- //};
-
- //动态
- struct Contact
- {
- struct PeopleInfo* data;
- int size;//通讯录中有效元素个数
- int capacity;//通讯录容量
- };
-
-
- //初始化通讯录
- void InitContact(struct Contact* pc);
-
- //通讯录扩容
- void BuyContact(struct Contact* pc);
-
- //增加联系人
- void AddContact(struct Contact* pc);
-
- //删除联系人
- void DelContact(struct Contact* pc);
-
- //查找联系人
- void SearchContact(const struct Contact* pc);
-
- //修改联系人
- void ModifyContact(struct Contact* pc);
-
- //排序通讯录列表(姓名)
- void SortContact(struct Contact* pc);
-
- //显示通讯录
- void ShowContact(const struct Contact* pc);
-
- //销毁通讯录
- void DestoryContact(struct Contact* pc);
-
- //保存信息到通讯录中
- void SaveContact(struct Contact* pc);
-
-
- //加载文件中的信息到通讯录中
- void LoadContact(struct Contact* pc);
- #include"Contact.h"
-
-
- //通讯录扩容
- void BuyContact(struct Contact* pc)
- {
- if (pc->size == pc->capacity)
- {
- struct PeopleInfo* tmp = (struct PeopleInfo*)realloc(pc->data, sizeof(struct PeopleInfo) * (pc->capacity) * 2);
- if (tmp == NULL)
- {
- perror("BuyContact::fail");
- return;
- }
- pc->data = tmp;
- pc->capacity *= 2;
- printf("增容成功
");
- }
- }
-
-
- void LoadContact(struct Contact* pc)
- {
- //打开文件
- FILE* pf = fopen("Contact.txt", "rb");
- if (pf == NULL)
- {
- perror("LoadContact::fopen");
- return;
- }
-
- //读文件
- struct PeopleInfo tmp = { 0 };
- while (fread(&tmp, sizeof(struct PeopleInfo), 1, pf))
- {
- BuyContact(pc);
- pc->data[pc->size] = tmp;
- pc->size++;
- }
-
- //关闭文件
- fclose(pf);
- pf = NULL;
- }
-
-
- //初始化通讯录(静态)
- //void InitContact(struct Contact* pc)
- //{
- // memset(pc->data, 0, sizeof(struct PeopleInfo) * CONTACT_MAX);
- // pc->size = 0;
- //}
-
- //初始化通讯录(动态)
- void InitContact(struct Contact* pc)
- {
- pc->data = (struct PeopleInfo*)malloc(sizeof(struct PeopleInfo) * CAPACITY);
- pc->capacity = CAPACITY;
- pc->size = 0;
-
- //加载文件信息到通讯录
- LoadContact(pc);
- }
-
-
-
-
- //添加联系人
- void AddContact(struct Contact* pc)
- {
- //静态
- /*if (pc->size >= CONTACT_MAX)
- {
- printf("通讯录已满
");
- return;
- }*/
-
- //动态(通讯录满了需增容)
- BuyContact(pc);
-
- printf("请输入名字>");
- scanf("%%%%s", pc->data[pc->size].name);
- printf("请输入年龄>");
- scanf("%%%%d", &(pc->data[pc->size].age));
- printf("请输入住址>");
- scanf("%%%%s", pc->data[pc->size].addr);
- printf("请输入电话>");
- scanf("%%%%s", pc->data[pc->size].tele);
- printf("添加成功!
");
- pc->size++;
- }
-
-
- //显示通讯录列表
- void ShowContact(const struct Contact* pc)
- {
- if (pc->size == 0)
- {
- printf("通讯录为空
");
- exit(1);
- }
- printf(" %%%%-10s %%%%-3s %%%%-10s %%%%-12s
", "name", "age", "addr", "tele");
- int i = 0;
- for (i = 0; i < pc->size; i++)
- {
- printf(" %%%%-10s %%%%-3d %%%%-10s %%%%-12s
",
- pc->data[i].name,
- pc->data[i].age,
- pc->data[i].addr,
- pc->data[i].tele);
- }
- }
-
-
- int FindByName(const struct Contact* pc,const char*name)
- {
-
- int i = 0;
- for (i = 0; i < pc->size; i++)
- {
- if (strcmp(pc->data[i].name, name) == 0)
- {
- return i;
- }
- }
- return -1;
- }
-
- //删除联系人
- void DelContact(struct Contact* pc)
- {
- if (pc->size <= 0)
- {
- printf("通讯录为空
");
- return;
- }
- char name[NAME_MAX] = { 0 };
- printf("请输入需要删除的联系人>");
- scanf("%%%%s", name);
- int i = FindByName(pc,name);
- if (i != -1)
- {
- for (; i < pc->size - 1; i++)
- {
- pc->data[i] = pc->data[i + 1];
- }
- pc->size--;
- printf("删除成功
");
- }
- else
- {
- printf("无此联系人
");
- }
- }
-
-
- //查找联系人
- void SearchContact(const struct Contact* pc)
- {
- char name[NAME_MAX] = { 0 };
- printf("请输入需要查找的联系人>");
- scanf("%%%%s", name);
- int i = FindByName(pc, name);
- if(i!=-1)
- {
- printf(" %%%%-10s %%%%-3s %%%%-10s %%%%-12s
", "name", "age", "addr", "tele");
- printf(" %%%%-10s %%%%-3d %%%%-10s %%%%-12s
",
- pc->data[i].name,
- pc->data[i].age,
- pc->data[i].addr,
- pc->data[i].tele);
-
- }
- else
- {
- printf("无此联系人
");
- }
- }
-
-
- //修改联系人
- void ModifyContact(struct Contact* pc)
- {
- char name[NAME_MAX] = { 0 };
- printf("请输入需要修改的联系人>");
- scanf("%%%%s", name);
- int i = FindByName(pc, name);
- if (i != -1)
- {
- printf("请输入修改后的名字>");
- scanf("%%%%s", pc->data[i].name);
- printf("请输入修改后的年龄>");
- scanf("%%%%d", &(pc->data[i].age));
- printf("请输入修改后的住址>");
- scanf("%%%%s", pc->data[i].addr);
- printf("请输入修改后的电话>");
- scanf("%%%%s", pc->data[i].tele);
- }
- else
- {
- printf("无此联系人
");
- }
-
- }
-
-
- //排序通讯录列表(姓名)
-
- int cmp_name(const void* a, const void* b)
- {
- return strcmp(((struct PeopleInfo*)a)->name , ((struct PeopleInfo*)b)->name);
- }
-
- void SortContact(struct Contact* pc)
- {
- qsort(pc->data, pc->size, sizeof(struct PeopleInfo), cmp_name);
- printf("排序成功
");
- }
-
-
-
- //销毁通讯录
- void DestoryContact(struct Contact* pc)
- {
- free(pc->data);
- pc->data = NULL;
- pc->capacity = 0;
- pc->size = 0;
- }
-
- //保存信息到通讯录中
- void SaveContact(struct Contact* pc)
- {
- //打开文件
- FILE* pf = fopen("Contact.txt", "w");
- if (pf == NULL)
- {
- perror("SaveContact::fopen");
- exit(1);
- }
-
- //写文件
- int i = 0;
- for (i = 0; i < pc->size; i++)
- {
- fwrite(&(pc->data[i]), sizeof(struct PeopleInfo), 1, pf);
- }
- //关闭文件
- fclose(pf);
- pf = NULL;
- }
- #include"Contact.h"
-
- void menu()
- {
- printf("******************************
");
- printf("******* 1.add 2.del *******
");
- printf("******* 3.search 4.modify ****
");
- printf("******* 5.sort 6.show ******
");
- printf("********* 0.exit *************
");
- printf("******************************
");
-
- }
-
- enum contact
- {
- EXIT,
- ADD,
- DEL,
- SEARCH,
- MODIFY,
- SORT,
- SHOW
- };
-
- int main()
- {
- //创建通讯录
- struct Contact con;
- //初始化通讯录
- InitContact(&con);
- int input = 0;
- do
- {
- menu();
- printf("请选择功能>");
- scanf("%%%%d", &input);
- switch (input)
- {
- case EXIT:
- //保存信息
- SaveContact(&con);
- DestoryContact(&con);
- printf("退出通讯录
");
- break;
- case ADD:
- AddContact(&con);
- break;
- case DEL:
- DelContact(&con);
- break;
- case SEARCH:
- SearchContact(&con);
- break;
- case MODIFY:
- ModifyContact(&con);
- break;
- case SORT:
- SortContact(&con);
- break;
- case SHOW:
- ShowContact(&con);
- break;
- default:
- printf("选择错误,请重新输入
");
- break;
- }
- } while (input);
- return 0;
- }
问题:
1. 中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
思考:如何解决以上问题呢?
我们下期再来解答~
推荐阅读