通讯录Linux的实现

产品底层思考:

  1. 人员如何存储 -> 链表 (增删改 但是排序不适合)
  2. 文件存储 -> 人员数据的格式 name:xxx,phone:xxx
  3. 人员信息 -> 姓名、电话 引出2
    name:xxx,phone:xxx,age:xxx,addr,xxx
    name:yyy,phone:yyy,age:yyy,addr,yyy

通讯录Linux的实现_第1张图片

实现通讯录

person结构体
通讯录 含 person结构体 person数量

宏定义

更占用空间但是更快 使用寄存器
失败情况:

#define EXCH(x,y){
    int tem = x;
    x = y;
    y = tem;
}

因为缺少续行符

** 续行符**

#define EXCH(x,y){\
    int tem = x;\
    x = y;\
    y = tem;\
}

续完行后宏定义可以使用
但是由于中间出现分号 有分号导致 if else无法链接
所以为了摆脱分号分开影响 使用do while(0);即可 do while(0);需要加一个分号!
do while(0)

#define EXCH(x,y) do{\  //续航符
    int tem = x;\
    x = y;\
    y = tem;\
}while(0)

#include
#define EXCH(x,y) do{\
    int tem = x;\
    x = y;\
    y = tem;\
}while(0)

int main()
{
    int x = 10,y = 20;

    if(x

person结构体

//结构体人包含联系人信息
struct person{
char name[NAME_LENGTH];
char phone[PHONE_LENGTH];

struct person* next;
struct person* prev;

};

通讯录结构体

//通讯录结构
struct contacts{
struct person* people;
int count;
};

宏实现链表的增删

insert

通讯录Linux的实现_第2张图片

#define LIST_INSERT(item,list) do{\
    item->next = list;\
    item->prev = NULL;\
    list->next->prev = item;\
    list = item;\
}while(0)

通讯录Linux的实现_第3张图片

move item

除了直接删除需注意头节点的删除需要设置list指针

#define LIST_REMOVE(item,list) do{\
    if(item->prev != NULL) item->prev->next = item->next;\
    if(item->next != NULL) item->next->prev = item->prev;\
    if(list == item) list = item->next;\
    item->prev = NULL;\
    item->next = NULL;\
}

通讯录Linux的实现_第4张图片

你可能感兴趣的:(零声cpp,linux,运维,服务器)