天天看點

利用list.h(系統封裝好的雙向循環連結清單的接口)頭檔案來編寫一個通訊錄小項目利用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;
        }
        }
    }
}
           

繼續閱讀