基于 链表 实现通讯录的功能

简介

最近, 我编写了一个程序,用于实现手机通讯录的功能。
该程序基于 单向循环链表 , 实现了 新建联系人、(按编号、姓名、号码)删除联系人、(按编号、姓名、号码)修改联系人、(按编号、姓名、号码)查询联系人、(按姓名、号码排序)遍历显示联系人的功能, 并且在主程序中为其加入了操作界面的设计。

程序:

#include 
#include 
#include 

#define T 1
#define F -1

typedef int Boolean;
typedef struct Node* node;

void id_order(node head);
Boolean init(node* head);
int length(node head);
void print(node head);
Boolean insert_tail(node head);
Boolean delete_id(node head);
void delete_name(node head);
void delete_num(node head);
Boolean update_id(node head);
Boolean update_name(node head);
Boolean update_num(node head);
Boolean query_id(node head);
void query_name(node head);
void query_num(node head);

void bubblesort_num(node head);
void quicksort_name(node head, int left, int right);
node unit_index(node head,int index);

struct Node
{
    int id;
    char name[20];
    char num[12];
    char addr[20];

    node next;
};


int main()
{
    int flag;
    node head;
    init(&head);


    while (1)
    {
    printf("******************************\n");
    printf("--MENU--\n");
    printf("1.INSERT\n2.DELETE\n3.UPDATE\n4.QUERY\n5.PRINT\n");
    int key;
    scanf("%d", &key);
    switch(key)
    {
        case 1:
            insert_tail(head);
            break;
        case 2:
            flag = 1;
            while (flag)
            {
            printf("******************************\n");
            printf("--DELETE--\n");
            printf("1.by ID\n2.by_NAME\n3.by_NUMBER\n0.BACK\n");
            scanf("%d", &key);
            switch(key)
            {
                case 0:
                    flag = 0;
                    break;
                case 1:
                    delete_id(head);
                    break;
                case 2:
                    delete_name(head);
                    break;
                case 3:
                    delete_num(head);
                    break;
                default:
                    printf("(ERROR)\n");
                    break;
            }
            }
            break;
        case 3:
            flag = 1;
            while (flag)
            {
            printf("******************************\n");
            printf("--UPDATE--\n");
            printf("1.by ID\n2.by_NAME\n3.by_NUMBER\n0.BACK\n");
            scanf("%d", &key);
            switch(key)
            {
                case 0:
                    flag = 0;
                    break;
                case 1:
                    update_id(head);
                    break;
                case 2:
                    update_name(head);
                    break;
                case 3:
                    update_num(head);
                    break;
                default:
                    printf("(ERROR)\n");
                    break;
            }
            }
            break;
        case 4:
            flag = 1;
            while (flag)
            {
            printf("******************************\n");
            printf("--QUERY--\n");
            printf("1.by ID\n2.by_NAME\n3.by_NUMBER\n0.BACK\n");
            scanf("%d", &key);
            switch(key)
            {
                case 0:
                    flag = 0;
                    break;
                case 1:
                    query_id(head);
                    break;
                case 2:
                    query_name(head);
                    break;
                case 3:
                    query_num(head);
                    break;
                default:
                    printf("(ERROR)\n");
                    break;
            }
            }
            break;
        case 5:
            flag = 1;
            while (flag)
            {
            printf("******************************\n");
            printf("--PRINT--\n");
            printf("1.by_NAME\n2.by_NUMBER\n0.BACK\n");
            scanf("%d", &key);
            switch(key)
            {
                case 0:
                    flag = 0;
                    break;
                case 1:
                    printf("******************************\n");
                    printf("--PRINT_by_NAME--\n");
                    quicksort_name(head, 1, length(head));
                    print(head);
                    break;
                case 2:
                    bubblesort_num(head);
                    break;
                default:
                    printf("(ERROR)\n");
                    break;
            }
            }
            break;
        default:
            printf("(ERROR)\n");
            break;
    }
    }

    return 0;
}

Boolean insert_tail(node head)
{
    printf("******************************\n");
    printf("--CREAT NEW CONTATOR--\n");
    char name[20];
    char num[12];
    char addr[20];
    printf("NAME: ");
    scanf("%s", &name);
    printf("NUMBER: ");
    scanf("%s", &num);
    printf("ADDRESS: ");
    scanf("%s", &addr);

    node newnode = (node)malloc(sizeof(struct Node));
    if (NULL == newnode)
    {
        return F;
    }
    strcpy(newnode->name, name);
    strcpy(newnode->num, num);
    strcpy(newnode->addr, addr);

    node temp = head;
    while (temp->next != head)
    {
        temp = temp->next;
    }
    temp->next = newnode;
    newnode->next = head;

    id_order(head);
    printf("(SAVE SUCCESS!)\n");
    return T;
}

Boolean delete_id(node head)
{
    printf("******************************\n");
    printf("--DELETE_ID--\n");
    int id;
    printf("ID: ");
    scanf("%d", &id);

    if (id <= 0 || id > length(head))
    {
        printf("(NOT FOUND ID: %d)\n", id);
        return F;
    }

    int i;
    node temp = head;
    for (i = 1; i < id; i++)
    {
        temp = temp->next;
    }
    node temp2 = temp->next;
    temp->next = temp->next->next;
    free(temp2);

    id_order(head);
    printf("(DELETE SUCCESS!)\n");
    return T;
}

void delete_name(node head)
{
    printf("******************************\n");
    printf("--DELETE_NAME--\n");
    char name[20];
    printf("NAME: ");
    scanf("%s", &name);

    int count = 0;
    node temp = head;
    while (temp->next != head)
    {
        if (!(strcmp(temp->next->name, name)))
        {
            count++;
            node temp2 = temp->next;
            temp->next = temp->next->next;
            free(temp2);
        }
        else
        {
            temp = temp->next;
        }
    }

    if (count)
    {
        id_order(head);
        printf("(DELETE SUCCESS!)\n");
    }
    else
    {
        printf("(NOT FOUND NAME: %s)\n", name);
    }
}

void delete_num(node head)
{
    printf("******************************\n");
    printf("--DELETE_NUMBER--\n");
    char num[12];
    printf("NUMBER: ");
    scanf("%s", &num);

    int count = 0;
    node temp = head;
    while (temp->next != head)
    {
        if (!(strcmp(temp->next->num, num)))
        {
            count++;
            node temp2 = temp->next;
            temp->next = temp->next->next;
            free(temp2);
        }
        else
        {
            temp = temp->next;
        }
    }

    if (count)
    {
        id_order(head);
        printf("(DELETE SUCCESS!)\n");
    }
    else
    {
        printf("(NOT FOUND NUMBER: %s)\n", num);
    }

}

Boolean update_id(node head)
{
    printf("******************************\n");
    printf("--UPDATE_ID--\n");
    int id;
    printf("ID: ");
    scanf("%d", &id);

    if (id <= 0 || id > length(head))
    {
        printf("(NOT FOUND ID: %d)\n", id);
        return F;
    }

    printf("PLEASE INPUT NEW INFOMATION:\n");
    char name[20];
    char num[12];
    char addr[20];
    printf("NAME: ");
    scanf("%s", &name);
    printf("NUMBER: ");
    scanf("%s", &num);
    printf("ADDRESS: ");
    scanf("%s", &addr);

    int i;
    for (i = 1; i <= id; i++)
    {
        head = head->next;
    }
    strcpy(head->name, name);
    strcpy(head->num, num);
    strcpy(head->addr, addr);

    printf("(UPDATE SUCCESS!)\n");
    return T;
}

Boolean update_name(node head)
{
    printf("******************************\n");
    printf("--UPDATE_NAME--\n");
    char old_name[20];
    printf("OLD_NAME: ");
    scanf("%s", &old_name);

    node temp = head;
    int count = 0;
    while (temp->next != head)
    {
        if (!(strcmp(temp->next->name, old_name)))
        {
            count++;
        }
        temp = temp->next;
    }
    if (0 == count)
    {
        printf("(NOT FOUND THIS NAME.)\n");
        return F;
    }

    printf("PLEASE INPUT NEW INFOMATION:\n");
    char name[20];
    char num[12];
    char addr[20];
    printf("NAME: ");
    scanf("%s", &name);
    printf("NUMBER: ");
    scanf("%s", &num);
    printf("ADDRESS: ");
    scanf("%s", &addr);

    temp = head;
    while (temp->next != head)
    {
        if (!(strcmp(temp->next->name, old_name)))
        {
            strcpy(temp->next->name, name);
            strcpy(temp->next->num, num);
            strcpy(temp->next->addr, addr);
        }
        temp = temp->next;
    }

    printf("(UPDATE SUCCESS!)\n");
    return T;
}

Boolean update_num(node head)
{
    printf("******************************\n");
    printf("--UPDATE_NUMBER--\n");
    char old_num[20];
    printf("OLD_NUMBER: ");
    scanf("%s", &old_num);

    node temp = head;
    int count = 0;
    while (temp->next != head)
    {
        if (!(strcmp(temp->next->num, old_num)))
        {
            count++;
        }
        temp = temp->next;
    }
    if (0 == count)
    {
        printf("(NOT FOUND THIS NUMBER.)\n");
        return F;
    }

    printf("PLEASE INPUT NEW INFOMATION:\n");
    char name[20];
    char num[12];
    char addr[20];
    printf("NAME: ");
    scanf("%s", &name);
    printf("NUMBER: ");
    scanf("%s", &num);
    printf("ADDRESS: ");
    scanf("%s", &addr);

    temp = head;
    while (temp->next != head)
    {
        if (!(strcmp(temp->next->num, old_num)))
        {
            strcpy(temp->next->name, name);
            strcpy(temp->next->num, num);
            strcpy(temp->next->addr, addr);
        }
        temp = temp->next;
    }

    printf("(UPDATE SUCCESS!)\n");
    return T;
}

Boolean query_id(node head)
{
    printf("******************************\n");
    printf("--QUERY_ID--\n");
    int id;
    printf("ID: ");
    scanf("%d", &id);

    if (id <= 0 || id > length(head))
    {
        printf("(NOT FOUND ID: %d)\n", id);
        return F;
    }

    int i;
    for (i = 1; i <= id; i++)
    {
        head = head->next;
    }
    printf("No.%-5dNAME: %-15sNUMBER: %-15sADDRESS: %-15s\n", head->id, head->name, head->num, head->addr);

    return T;
}

void query_name(node head)
{
    printf("******************************\n");
    printf("--QUERY_NAME--\n");
    char name[20];
    printf("NAME: ");
    scanf("%s", &name);

    int count = 0;
    node temp = head;
    while (temp->next != head)
    {
        if (!(strcmp(temp->next->name, name)))
        {
            count++;
            printf("No.%-5dNAME: %-15sNUMBER: %-15sADDRESS: %-15s\n", temp->next->id, temp->next->name, temp->next->num, temp->next->addr);
        }
        temp = temp->next;
    }

    if (0 == count)
    {
        printf("(NOT FOUND NAME: %s)\n", name);
    }

}

void query_num(node head)
{
    printf("******************************\n");
    printf("--QUERY_NUMBER--\n");
    char num[12];
    printf("NUMBER: ");
    scanf("%s", &num);

    int count = 0;
    node temp = head;
    while (temp->next != head)
    {
        if (!(strcmp(temp->next->num, num)))
        {
            count++;
            printf("No.%-5dNAME: %-15sNUMBER: %-15sADDRESS: %-15s\n", temp->next->id, temp->next->name, temp->next->num, temp->next->addr);
        }
        temp = temp->next;
    }

    if (0 == count)
    {
        printf("(NOT FOUND NUMBER: %s)\n", num);
    }

}

Boolean init(node* head)
{
    node newnode = (node)malloc(sizeof(struct Node));
    if (NULL == newnode)
    {
        return F;
    }
    newnode->id = 0;
    strcpy(newnode->name, "0");
    strcpy(newnode->num, "0");
    strcpy(newnode->addr, "0");

    newnode->next = newnode;

    *head = newnode;

    return T;
}

void print(node head)
{
    node temp = head;
    while (temp->next != head)
    {
        printf("No.%-5dNAME: %-15sNUMBER: %-15sADDRESS: %-15s\n", temp->next->id, temp->next->name, temp->next->num, temp->next->addr);
        temp = temp->next;
    }

}

int length(node head)
{
    int count = 0;
    node temp = head;
    while(temp->next != head)
    {
        count++;
        temp = temp->next;
    }
    return count;
}

void id_order(node head)
{
    int count = 0;

    node temp = head;
    while (temp->next != head)
    {
        temp->next->id = ++count;
        temp = temp->next;
    }
}

void bubblesort_num(node head)
{
    printf("******************************\n");
    printf("--PRINT_by_NUM--\n");

    int i, j, m;
    for (i = 1; i < length(head); i++)
    {
        for (j = length(head); j > i; j--)
        {
            node temp1 = head;
            for (m = 0; m < j - 2; m++)
            {
                temp1 = temp1->next;
            }

            node temp2 = temp1->next;
            if (strcmp(temp2->next->num, temp2->num) < 0)
            {
                temp1->next = temp2->next;
                temp2->next = temp1->next->next;
                temp1->next->next = temp2;
            }
        }
    }

    id_order(head);
    print(head);
}

void quicksort_name(node head, int left, int right)
{
    if (left >= right)
    {
        return;
    }

    int i = left;
    int j = right;
    char key_name[20];
    char key_num[12];
    char key_addr[20];
    strcpy(key_name, unit_index(head, left)->name);
    strcpy(key_num, unit_index(head, left)->num);
    strcpy(key_addr, unit_index(head, left)->addr);

    while (i < j)
    {
        while (i < j && (strcmp(unit_index(head, j)->name, key_name) >= 0))
        {
            j--;
        }
        strcpy(unit_index(head, i)->name, unit_index(head, j)->name);
        strcpy(unit_index(head, i)->num, unit_index(head, j)->num);
        strcpy(unit_index(head, i)->addr, unit_index(head, j)->addr);

        while (i < j && (strcmp(unit_index(head, i)->name, key_name) <= 0))
        {
            i++;
        }
        strcpy(unit_index(head, j)->name, unit_index(head, i)->name);
        strcpy(unit_index(head, j)->num, unit_index(head, i)->num);
        strcpy(unit_index(head, j)->addr, unit_index(head, i)->addr);
    }

    strcpy(unit_index(head, i)->name, key_name);
    strcpy(unit_index(head, i)->num, key_num);
    strcpy(unit_index(head, i)->addr, key_addr);
    quicksort_name(head, left, i - 1);
    quicksort_name(head, i + 1, right);
}

node unit_index(node head, int index)
{
    int i;
    node temp = head;
    for (i = 0; i < index; i++)
    {
        temp = temp->next;
    }
    return (temp);
}

运行界面

基于 链表 实现通讯录的功能_第1张图片
基于 链表 实现通讯录的功能_第2张图片
基于 链表 实现通讯录的功能_第3张图片
基于 链表 实现通讯录的功能_第4张图片
基于 链表 实现通讯录的功能_第5张图片
基于 链表 实现通讯录的功能_第6张图片
基于 链表 实现通讯录的功能_第7张图片

心得

在写通讯录程序的过程中, 我获益匪浅, 在此进行记录。
首先, 我对链表的认识更加人深刻了, 对链表和指针的使用也更加熟悉了;在写遍历联系人(print)的子程序时, 按姓名顺序遍历我采用了快速排序法, 按号码顺序遍历我采用了冒泡排序法, 对两种排序算法有了更进一步的理解; 联系人的姓名、号码、地址均采用字符串类型, 在处理(输入、排序、输出)其的过程中, 帮助我复习了字符串类型数据的输入、输出和常用处理字符串的函数的相关知识; 新建、删除联系人的过程中, 需要申请空间和释放空间, 这部分帮助我强化了内存管理相关函数的使用; 该程序由17个子函数和1个main函数组成, 在编写程序的过程中, 帮助我巩固了巩固了子函数的调用,、参数的传递、函数返回值以及数组作为参数传递的过程中弱化为指针的相关知识; 在main函数中, 我对整个程序运行中的界面进行了一定的设计, 锻炼了我人机交互界面的设计能力。

你可能感兴趣的:(C语言,通讯录,链表,排序算法)