利用list.h(系统封装好的双向循环链表的接口)头文件来编写一个通讯录小项目

利用list.h(系统封装好的双向循环链表的接口)头文件来编写一个通讯录小项目

学习了链表,虽说系统已经帮我们码农封装好了各种接口,我们码农不需要自己码接口了,只需要做一个幸福的搬运工,但我们码农不会搬运,系统提供再多的接口也毛用哈!以下是小编小试牛刀,先学会怎么做一位幸福的搬运工先哈,运用系统封装好的双向循环链表的接口写的通讯录小项目,仅供参考哈!

/*
     	printf("\t0、显示已有的通讯录\n");
        printf("\t1、增加存储人物的名字与电话\n");
        printf("\t2、通过人物的名字可以删除联系人\n");
        printf("\t3、通过人物的名字来修改联系人的电话号码\n");
        printf("\t4、可以设置星级好友,一直置顶显示。\n");
        printf("\t5、将某位联系人拉入黑名单\n");
        printf("\t6、将某位联系人拉出黑名单!\n");
        printf("\t7、显示黑名单!\n");
        printf("\t8、通过名字查找某位联系人的联系方式\n");
        printf("\t9、退出通讯录。\n");
*/
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include <string.h>
struct node // 大结构体
{
    struct list_head list; // 小结构体
    char name[1024];
    char PhoneNumber[12];
};
// 初始化大结构体
struct node *init(char *name, char *PhoneNumber)
{
    struct node *xnew = malloc(sizeof(struct node));
    strcpy(xnew->name, name);
    strcpy(xnew->PhoneNumber, PhoneNumber);
    INIT_LIST_HEAD(&xnew->list);
    return xnew;
}
// 增    存储人物的名字与电话
void insert_node(struct list_head *head, struct list_head *xnew)
{
    list_add_tail(xnew, head);
    return;
}
// 删    通过人物的名字可以删除联系人
void del_node(struct list_head *head, char *name)
{
    struct list_head *pos = NULL;
    list_for_each(pos, head) // 遍历链表
    {
        struct node *value = (struct node *)pos; // 将struct list_head * 型的pos 强制转换成struct node * 型 ,以获取大结构体内的人物的名字
        if (strcmp(value->name, name) == 0)
        {
            list_del_init(pos); // 让pos从链表中脱离出来
            free(value); // 释放大结构体的空间
            printf("删除成功!\n");
            return;
        }
    }
    printf("通讯录中无此人,删除失败!\n");
    return;
}
// 拉入黑名单
struct node *Blacklist_node(struct list_head *head, char *name)
{
    struct list_head *pos = NULL;
    list_for_each(pos, head) // 遍历链表
    {
        struct node *value = (struct node *)pos; // 将struct list_head * 型的pos 强制转换成struct node * 型 ,以获取大结构体内的人物的名字
        if (strcmp(value->name, name) == 0)
        {
            list_del_init(pos); // 让pos从链表中脱离出来
            printf("成功拉入黑名单!\n");
            return value;
        }
    }
    printf("通讯录中无此人,操作失败!\n");
    return NULL;
}
// 拉出黑名单
struct node *out_Blacklist(struct list_head *head, char *name)
{
    struct list_head *pos = NULL;
    list_for_each(pos, head) // 遍历链表
    {
        struct node *value = (struct node *)pos; // 将struct list_head * 型的pos 强制转换成struct node * 型 ,以获取大结构体内的人物的名字
        if (strcmp(value->name, name) == 0)
        {
            list_del_init(pos); // 让pos从链表中脱离出来
            printf("成功拉出黑名单!\n");
            return value;
        }
    }
    printf("通讯录中无此人,操作失败!\n");
    return NULL;
}
// 查  可以通过人物的名字查找电话
void find_node(struct list_head *head, char *name)
{
    struct list_head *pos = NULL;
    list_for_each(pos, head) // 遍历链表
    {
        struct node *value = (struct node *)pos; // 将struct list_head * 型的pos 强制转换成struct node * 型 ,以获取大结构体内的人物的名字和电话
        if (strcmp(value->name, name) == 0)
        {
            printf("查找到的信息:    %s\t%s\n", value->name, value->PhoneNumber);
            return;
        }
    }
    printf("查无此人!\n");
    return;
}
// 4.可以设置星级好友,一直置顶显示。
void vip_node(struct list_head *head, char *name)
{
    struct list_head *pos = NULL;
    list_for_each(pos, head)
    {
        struct node *value = (struct node *)pos;
        if (strcmp(value->name, name) == 0)
        {
            list_del_init(pos);  // 让pos从链表中脱离出来
            list_add(pos, head); // 头插
            printf("设置星级好友成功!\n");
            return;
        }
    }
    printf("通讯录中无此人,请先增加此人,再设置星级好友\n");
    return;
}
// 显示
void show_node(struct list_head *head)
{
    struct list_head *pos = NULL;
    printf("联系人\t电话号码\n");
    list_for_each(pos, head)
    {
        struct node *value = (struct node *)pos;
        printf("%s\t%s\n", value->name, value->PhoneNumber);
    }
    return;
}
// 改
void change_node(struct list_head *head, char *name)
{
    struct list_head *pos = NULL;
    list_for_each(pos, head)
    {
        struct node *value = (struct node *)pos;
        if (strcmp(value->name, name) == 0)
        {
            printf("请输入修改后的电话号码\n");
            char PhoneNumber[1024];
            scanf("%s", PhoneNumber);
            strcpy(value->PhoneNumber, PhoneNumber);
            printf("修改成功!\n");
            return;
        }
    }
    printf("查无此人,不可以修改\n");
    return;
}

int main()
{
    struct list_head *head = malloc(sizeof(struct list_head));
    INIT_LIST_HEAD(head);
    struct list_head *Blacklist_head = malloc(sizeof(struct list_head));
    INIT_LIST_HEAD(Blacklist_head);
    insert_node(head, &init("朱晓明", "15219691242")->list);
    insert_node(head, &init("老王", "18919796542")->list);
    insert_node(head, &init("妈咪", "18515796442")->list);
    insert_node(head, &init("老爸", "19819796456")->list);
    insert_node(head, &init("哥哥", "18819792222")->list);
    insert_node(head, &init("姐姐", "18819711111")->list);
    insert_node(head, &init("陶晓敏", "1881944444")->list);
    int n;
    while (1)
    {
        printf("__________________________________________________\n");
        printf("\t0、显示已有的通讯录\n");
        printf("\t1、增加存储人物的名字与电话\n");
        printf("\t2、通过人物的名字可以删除联系人\n");
        printf("\t3、通过人物的名字来修改联系人的电话号码\n");
        printf("\t4、可以设置星级好友,一直置顶显示。\n");
        printf("\t5、将某位联系人拉入黑名单\n");
        printf("\t6、将某位联系人拉出黑名单!\n");
        printf("\t7、显示黑名单!\n");
        printf("\t8、通过名字查找某位联系人的联系方式\n");
        printf("\t9、退出通讯录。\n");
        printf("__________________________________________________\n");
        printf("请选择要执行的功能:");
        scanf("%d", &n);
        switch (n)
        {
        case 0:
        {
            show_node(head);
            break;
        }
        case 1:
        {
            printf("请输入要增加的人物的名字和电话\n");
            char name[1024];
            char PhoneNumber[1024];
            scanf("%s%s", name, PhoneNumber);
            insert_node(head, &init(name, PhoneNumber)->list);
            printf("增加成功!\n");
            break;
        }
        case 2:
        {
            printf("请输入要删除的人物的名字\n");
            char name[1024];
            scanf("%s", name);
            del_node(head, name);
            break;
        }
        case 3:
        {
            printf("请输入要修改联系人的名字\n");
            char name[1024];
            scanf("%s", name);
            change_node(head, name);
            break;
        }
        case 4:
        {
            printf("请输入要设置星级好友的人的名字\n");
            char name[1024];
            scanf("%s", name);
            vip_node(head, name);
            break;
        }
        case 5:
        {
            printf("请输入要拉入黑名单联系人的名字\n");
            char name[1024];
            scanf("%s", name);
            insert_node(Blacklist_head, &Blacklist_node(head, name)->list);
            break;
        }
        case 6:
        {
            printf("请输入要拉出黑名单联系人的名字\n");
            char name[1024];
            scanf("%s", name);
            struct node *pos=out_Blacklist(Blacklist_head, name);
            if(pos != NULL)
            {
                insert_node(head,&pos->list);
            }     
            break;
        }
        case 7:
        {
            printf("显示黑名单!\n");
            show_node(Blacklist_head);
            break;
        }
        case 8:
        {
            printf("请输入要查找的人的名字\n");
            char name[1024];
            scanf("%s", name);
            find_node(head, name);
            break;
        }
        case 9:
        {
            printf("退出通讯录。\n");
            return 0;
        }
        default:
        {
            printf("输入错误,请重新输入!\n");
            break;
        }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NOREAD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值