数据结构_基于链表的通讯录
顺序表的源代码需要略作修改,如下 将数据类型改为通讯录的结构体。注释掉打印,查找的函数。
SList.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h> #include<stdlib.h> #include<assert.h> #include"Contact.h"
typedef PeoInfo SList_Datatype;
typedef struct SList_Node { SList_Datatype Data; struct SList_Node* Next; }STL_Node;
void SLTPushBack(STL_Node** pphead, SList_Datatype x);
void SLTPushFront(STL_Node** pphead, SList_Datatype x);
void SLTPopBack(STL_Node** pphead);
void SLTPopFront(STL_Node** pphead);
void SLTInsert(STL_Node** pphead, STL_Node* pos, SList_Datatype x);
void SLTInsertAfter(STL_Node* pos, SList_Datatype x);
void SLTErase(STL_Node** pphead, STL_Node* pos);
void SLTEraseAfter(STL_Node* pos);
void SListDesTroy(STL_Node** pphead);
|
新建立一个结构体,存储联系人的数据。将链表重新给一个名字contact。
实现以下几个接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #pragma once
#define NAME_MAX 20
typedef struct SList_Node contact;
typedef struct PersonInfo { char name[NAME_MAX]; char tel[11]; }PeoInfo;
void InitContact(contact** con);
void AddContact(contact** con);
void DelContact(contact** con);
void ShowContact(contact* con);
contact* FindContact(contact* con, char* find);
void ModifyContact(contact** con);
void DestroyContact(contact** con);
|
因为我写过一遍通讯录(基于顺序表)所以再基于链表写一个的难度不是很大。
这里我要特别提醒自己和在看的各位:if(xxx == xxx) 中间一定记得用双等号,不要手滑搞成了 ‘=’(因为本人因为这个错误找了半小时bug,知道是这个原因的时候直接碎掉)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #define _CRT_SECURE_NO_WARNINGS 1 #include"Contact.h" #include"SList.h" #include<stdio.h> #include<string.h>
void InitContact(contact** con) { contact* phead = NULL; *con = phead; }
void AddContact(contact** con) { PeoInfo newperson; printf("姓名:"); scanf("%s", newperson.name); printf("电话:"); scanf("%s",newperson.tel); SLTPushBack(con,newperson); }
void DelContact(contact** con) { char del[NAME_MAX]; printf("要删除的联系人的姓名:"); scanf("%s",del); contact* find = FindContact(*con, del); if (find == NULL) { printf("要删除的联系人不存在"); return; } SLTErase(con, find); }
void ShowContact(contact* con) { assert(con); while (con) { printf("姓名:%s\n", con->Data.name); printf("电话:%s\n", con->Data.tel); con = con->Next; } }
contact* FindContact(contact* con,char* find) { contact* pcur = con; while (pcur) { if (strcmp(find, pcur->Data.name) == 0) { return pcur; } pcur = pcur->Next; } return NULL; }
void ModifyContact(contact** con) { printf("请输入要修改联系人的名字:"); char mod[NAME_MAX]; scanf("%s",mod); contact* find = FindContact(*con, mod); if (find == NULL) { printf("要修改的联系人不存在!"); return; } PeoInfo newperson; printf("请输入修改后的名字、电话:"); scanf("%s%s",newperson.name,newperson.tel); SLTInsertAfter(find,newperson); SLTErase(con,find); }
void DestroyContact(contact** con) { SListDesTroy(con); }
|
本博客旨在记录学习过程,以后忘了随时来看。