数据结构4:基于单链表的通讯录项目
文章目录
- 头文件
- SList.h
- Contact.h
- 实现文件
- SList.c
- Contact.c
- 测试代码
头文件
SList.h
#pragma once #include #include #include #include"Contact.h" //typedef int SLDataType; typedef PersonInfo SLDataType; //创建链表节点结构 typedef struct SListNode { SLDataType a; struct SListNode* next; }SListNode; //销毁 void SLDestory(SListNode** phead); //打印 void SLPrint(SListNode* phead); //尾插 void SLPushBack(SListNode** pphead, SLDataType x); //头插 void SLPushFront(SListNode** pphead, SLDataType x); //尾删 void SLPopBack(SListNode** pphead); //头删 void SLPopFront(SListNode** pphead); //查找 SListNode* SLFind(SListNode* phead, SLDataType x); //在指定数据之前插入数据 void SLInsertFront(SListNode** pphead, SLDataType pos, SLDataType x); //指定数据之后插入 void SLInsertBack(SListNode* phead, SLDataType pos, SLDataType x); //指定位置之前插入数据 void SLInsertFront_1(SListNode** pphead, SListNode* pos, SLDataType x); //指定位置之前插入数据 void SLInsertBack_1(SListNode* pos, SLDataType x); //删除pos结点 void SLErase(SListNode** pphead, SListNode* pos); //删除pos之后的结点 void SLEraseAfter(SListNode* pos);
Contact.h
#pragma once #define NAME_MAX 50 #define GENDER_MAX 4 #define TEL_MAX 11 #define ADDR_MAX 100 //将单链表重命名为通讯录 typedef struct SListNode Contact; //定义联系人数据结构 typedef struct PersonInfo { char name[NAME_MAX]; char gender[GENDER_MAX]; char tel[TEL_MAX]; char addr[ADDR_MAX]; }PersonInfo; //销毁通讯录 void ContactDestory(Contact** con); //添加通讯录数据 void ContactPushBack(Contact** con); //展示通讯录 void ContactPrint(Contact* con); //保存数据 void SaveContact(Contact* con); //读取文件 void LoadContact(Contact** con); //删除联系人 void ContactDele(Contact** con); //查找联系人 void ContactFind(Contact* con); //修改联系人信息 void ContactModify(Contact** con);
实现文件
SList.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"SList.h" //打印 //void SLPrint(SListNode* phead) //{ // SListNode* pcur = phead; // while (pcur) // { // printf("%d->", pcur->a); // pcur = pcur->next; // } // printf("NULL\n"); //} //创建新节点 SListNode* BuyNode(SLDataType x) { SListNode* newnode = (SListNode*)malloc(sizeof(SListNode)); if (newnode == NULL) { perror("malloc fail!\n"); exit(1); } newnode->a = x; newnode->next = NULL; return newnode; } //链表的销毁 void SLDestory(SListNode** pphead) { assert(pphead); SListNode* pcur = *pphead; while (pcur) { SListNode* tmp = pcur->next; free(pcur); pcur = tmp; } *pphead = NULL; } //尾插 void SLPushBack(SListNode** pphead, SLDataType x) { assert(pphead); SListNode* newnode = BuyNode(x); if (*pphead == NULL) { *pphead = newnode; } else { SListNode* ptail = *pphead; while (ptail->next != NULL) { ptail = ptail->next; } ptail->next = newnode; } } //头插 void SLPushFront(SListNode** pphead, SLDataType x) { assert(pphead); SListNode* newnode = BuyNode(x); newnode->next = *pphead; *pphead = newnode; } //尾删 void SLPopBack(SListNode** pphead) { assert(pphead && *pphead); if ((*pphead)->next == NULL) { SLDestory(pphead); return; } SListNode* pprev = *pphead; SListNode* ptail = *pphead; while (ptail->next != NULL) { pprev = ptail; ptail = ptail->next; } pprev->next = NULL; free(ptail); ptail = NULL; } //头删 void SLPopFront(SListNode** pphead) { assert(pphead && *pphead); SListNode* pcur = *pphead; *pphead = pcur->next; free(pcur); pcur = NULL; } //查找 //SListNode* SLFind(SListNode* phead, SLDataType x) //{ // assert(phead); // SListNode* pcur = phead; // while (pcur) // { // if (pcur->a == x) // { // return pcur; // } // pcur = pcur->next; // } // return NULL; //} //指定数据之前插入 //void SLInsertFront(SListNode** pphead, SLDataType pos, SLDataType x) //{ // assert(pphead && *pphead); // if ((*pphead)->a == pos) // { // SLPushFront(pphead, x); // return; // } // SListNode* newnode = BuyNode(x); // SListNode* pcur = *pphead; // while (pcur->next->a != pos) // { // pcur = pcur->next; // } // newnode->next = pcur->next; // pcur->next = newnode; //} //指定数据之后插入 //void SLInsertBack(SListNode* phead, SLDataType pos, SLDataType x) //{ // assert(phead); // SListNode* newnode = BuyNode(x); // SListNode* find = SLFind(phead, pos); // newnode->next = find->next; // find->next = newnode; //} //指定位置之前插入数据 //void SLInsertFront_1(SListNode** pphead, SListNode* pos, SLDataType x) //{ // assert(pphead && *pphead); // assert(pos); // if (*pphead == pos) // { // SLPushFront(pphead, x); // return; // } // SListNode* newnode = BuyNode(x); // SListNode* pcur = *pphead; // while (pcur->next != pos) // { // pcur = pcur->next; // } // newnode->next = pcur->next; // pcur->next = newnode; //} //指定位置之前插入数据 //void SLInsertBack_1(SListNode* pos, SLDataType x) //{ // assert(pos); // SListNode* newnode = BuyNode(x); // newnode->next = pos->next; // pos->next = newnode; //} //删除pos结点 void SLErase(SListNode** pphead, SListNode* pos) { assert(pphead && *pphead); assert(pos); if (*pphead == pos) { SLPopFront(pphead); return; } SListNode* prev = *pphead; while (prev->next != pos) { prev = prev->next; } prev->next = pos->next; free(pos); pos = NULL; } //删除pos之后的结点 void SLEraseAfter(SListNode* pos) { assert(pos && pos->next); SListNode* next = pos->next; pos->next = next->next; free(next); next = NULL; }
Contact.c
#define _CRT_SECURE_NO_WARNINGS 1 #include #include"Contact.h" #include"SList.h" //销毁通讯录 void ContactDestory(Contact** con) { SLDestory(con); } //添加通讯录数据 void ContactPushBack(Contact** con) { assert(con); Contact* phead = *con; PersonInfo tmp; printf("请输入联系人姓名:\n"); scanf("%s", tmp.name); printf("请输入联系人性别:\n"); scanf("%s", tmp.gender); printf("请输入联系人电话:\n"); scanf("%s", tmp.tel); printf("请输入联系人地址:\n"); scanf("%s", tmp.addr); SLPushBack(con, tmp); } //查找联系人信息 Contact* Findbyname(Contact* con,char name[NAME_MAX]) { assert(con); Contact* pcur = con; while (pcur) { if (strcmp(pcur->a.name, name) == 0) { return pcur; } pcur = pcur->next; } return NULL; } //删除联系人 void ContactDele(Contact** con) { assert(con); Contact* phead = *con; char name[NAME_MAX]; printf("请输入要删除的联系人姓名:\n"); scanf("%s", name); Contact* find = Findbyname(*con, name); if (find == NULL) { printf("无此联系人信息!\n"); return; } SLErase(con, find); } //查找联系人 void ContactFind(Contact* con) { assert(con); char name[NAME_MAX]; printf("请输入要查找的联系人姓名:\n"); scanf("%s", name); Contact* find = Findbyname(con, name); if (find == NULL) { printf("无此联系人信息!\n"); return; } printf("姓名 性别 电话 地址\n"); printf("%s %s %s %s\n", find->a.name , find->a.gender , find->a.tel , find->a.addr); } //修改联系人信息 void ContactModify(Contact** con) { assert(con); char name[NAME_MAX]; printf("请输入要修改的联系人姓名:\n"); scanf("%s", name); Contact* find = Findbyname(*con, name); if (find == NULL) { printf("无此联系人信息!\n"); return; } printf("请输入新的联系人姓名:\n"); scanf("%s", find->a.name); printf("请输入联系人性别:\n"); scanf("%s", find->a.gender); printf("请输入联系人电话:\n"); scanf("%s", find->a.tel); printf("请输入联系人地址:\n"); scanf("%s", find->a.addr); printf("修改成功!\n"); } //展示通讯录 void ContactPrint(Contact* con) { Contact* pcur = con; printf("姓名 性别 电话 地址\n"); while (pcur) { printf("%s %s %s %s\n", pcur->a.name , pcur->a.gender , pcur->a.tel , pcur->a.addr); pcur = pcur->next; } } //保存文件 void SaveContact(Contact* con) { FILE* pf = fopen("Contact.txt", "wb"); if (pf == NULL) { perror("fopen fail!\n"); return; } Contact* pcur = con; while (pcur) { fwrite(&(pcur->a), sizeof(PersonInfo), 1, pf); pcur = pcur->next; } fclose(pf); pf = NULL; printf("保存成功!\n"); return; } //读取文件 void LoadContact(Contact** con) { FILE* pf = fopen("Contact.txt", "rb"); if (pf == NULL) { perror("fopen fail!\n"); return; } PersonInfo a; PersonInfo* tmp = &a; while (fread(tmp,sizeof(PersonInfo),1,pf) ) { SLPushBack(con, *tmp); } printf("读取成功!\n"); }
测试代码
#define _CRT_SECURE_NO_WARNINGS 1 #include"SList.h" #include"Contact.h" //测试通讯录 void Test2(Contact* con) { //读取数据 LoadContact(&con); //执行操作 int input = 0; do { menu(); scanf("%d", &input); switch (input) { case 1: ContactPushBack(&con); break; case 2: ContactDele(&con); break; case 3: ContactModify(&con); break; case 4: ContactFind(con); break; case 5: ContactPrint(con); default: break; } } while (input); printf("正在退出...\n"); //保存数据 SaveContact(con); } int main() { Contact* con = NULL; Test2(con); //销毁 ContactDestory(&con); return 0; }
(图片来源网络,侵删)
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。