利用 双向循环链表 实现通讯录的功能

该通讯录利用了双向循环链表, 通讯录能记录id, name, number. 它的功能有:输入数据(尾插法),根据id, name, number实现删除, 查找,更新数据,排序,输出数据的功能。 其中排序用了两种方法, 对name进行从小到大的冒泡排序法  ,和对number进行从小到大的快速排序法。当输入指令为end时,就结束对通讯录的访问和修改。





#include <stdio.h>

#include <string.h>
#include <stdlib.h>


#define T 1
#define F -1


typedef int el;
typedef int bo;
typedef char ch;
typedef struct Address* address;


struct Address
{
    el id;
    ch name[10];
    ch number[12];
    address next;
    address prior;
};


address unit_index(address head, int index);
bo init(address* head);
void rank_id(address head);
void rank_name(address head);
void rank_number(address head, int l, int r);
bo insert_tail(address head, ch *name, ch *number);
bo delete_id(address head, int id);
bo delete_name(address head, ch *name);
bo delete_number(address head, ch *number);
bo query_id(address head, el id);
void query_name(address head, ch *name);
void query_number(address head, ch *number);
void update_name(address head, ch *name, char *name1, char *number);
void update_number(address head, ch *number, char *name1, char *number1);
bo update_id(address head, el id, char *name1, char *number);
bo length(address head);
void print(address head);
bo main()
{
    int id, k, j;
    char name1[10];
    char num[12];
    char name2[10];
    char num1[12];
    char v[10];
    int x = 1;
    char* m = (char *)malloc(sizeof(char)*20);
    char n[10];
    address head = NULL;
    init(&head);
    printf("**************************************you have six orders:insert, delete, query, print, end, rank************************************\n");
    while(1 == x)
    {
        printf("please input a order:");
        scanf("%s", n);
        strcpy(m, n);
        if(strcmp(m, "rank") == 0)
        {
            printf("rank according to name or number?");
            scanf("%s", v);
            if(strcmp(v, "name") == 0)
            {
                rank_name(head);
                print(head);
            }
            if(strcmp(v, "number") == 0)
            {
                rank_number(head, 0, length(head));
                print(head);
            }
        }
        if(strcmp(m, "insert") == 0)
        {
            printf("how many address do you want to insert?");
            scanf("%d", &j);
            for(k = 1; k <= j; k++)
            {
                printf("please input name and number:");
                scanf("%s %s", name1, num);
                insert_tail(head, name1, num);
            }
        }
        if(strcmp(m, "print") == 0)
        {
            print(head);
        }
        if(strcmp(m, "delete") ==0) 
        { 
            printf("which do you want to delete id, name or number:");
            scanf("%s", v);
            if(strcmp(v, "id") == 0)
            {
                printf("please input id:");
                scanf("%d", &id);
                delete_id(head, id);
            }
            if(strcmp(v, "name") == 0)
            {
                printf("please input name:");
                scanf("%s", name1);
       delete_name(head, name1);
            }
            if(strcmp(v, "number") == 0)
            {
                printf("please input number:");
                scanf("%s", num);
       delete_number(head, num);
            }
            print(head);
        }   
        if(strcmp(m, "query") ==0) 
        { 
            printf("which do you want to query id, name or number:");
            scanf("%s", v);
            if(strcmp(v, "id") == 0)
            {
                printf("please input id:");
                scanf("%d", &id);
                query_id(head, id);
            }
            if(strcmp(v, "name") == 0)
            {
                printf("please input name:");
                scanf("%s", name1);
       query_name(head, name1);
            }
            if(strcmp(v, "number") == 0)
            {
                printf("please input number:");
                scanf("%s", num);
       query_number(head, num);
            }
        }   
        if(strcmp(m, "update") ==0) 
        { 
            printf("which do you want to update id, name or number:");
            scanf("%s", v);
            if(strcmp(v, "id") == 0)
            {
                printf("please input id:");
                scanf("%d", &id);
                printf("update it to:");
                scanf("%s %s", name1, num);
                update_id(head, id, name1, num);
            }
            if(strcmp(v, "name") == 0)
            {
                printf("please input name:");
                scanf("%s", name1);
                printf("update it to:");
                scanf("%s %s", name2, num1);
                update_name(head, name1, name2, num1);
            }
            if(strcmp(v, "number") == 0)
            {
                printf("please input number:");
                scanf("%s", num);
                printf("update it to:");
                scanf("%s %s", name2, num1);
                update_number(head, num, name2, num1);
            }
            print(head);
        }
        if(strcmp(m, "end") == 0)
        {
            printf("**************************************************************thank you**************************************************************\n");
            x = 0;
        }   


    }
    return 0;
}


void rank_number(address head, int l, int r)
{
    int i = l, j = r, m;
    char name[20];
    char num[20];
    strcpy(name, unit_index(head, l)->name);
    strcpy(num, unit_index(head, l)->number);
    while(i < j)
    {
        while(i < j && strcmp(unit_index(head, j)->number, num) >= 0)
        {
            j--;
        }
        if(i < j)
        {
            strcpy(unit_index(head, i)->name, unit_index(head, j)->name);
            strcpy(unit_index(head, i)->number, unit_index(head, j)->number);
        }
        while(i < j && strcmp(unit_index(head, i)->number, num) <= 0)
        {
            i++;
        }
        if(i < j)
        {
            strcpy(unit_index(head, j)->name, unit_index(head, i)->name);
            strcpy(unit_index(head, j)->number, unit_index(head, i)->number);
        }
        strcpy(unit_index(head, i)->number, num);
        strcpy(unit_index(head, i)->name, name);
        rank_number(head, l, i - 1);
        rank_number(head, i + 1, r);
    }
    
}
address unit_index(address head, int index)
{
    int i;
    address temp = head;
    for(i = 0; i < index; i++)
    {
        temp = temp->next;
    }
    return(temp);
}


void rank_name(address head)
{
    int i, j;
    int len = length(head);
    char *num = (char*)malloc(sizeof(char)*20);
    char *nam = (char*)malloc(sizeof(char)*20);
    address temp = head;
    for(i = 0; i < len - 1; i++)
    {
        head = temp;
        for(j = i; j < len; j++)
        {
            if(strcmp(temp->next->name, head->next->name) > 0)
            {
                strcpy(num, head->next->number);
                strcpy(head->next->number, temp->next->number);
                strcpy(temp->next->number, num);
                strcpy(nam, head->next->name);
                strcpy(head->next->name, temp->next->name);
                strcpy(temp->next->name, nam);
            }
            head = head->next;
        }
        temp = temp->next;
    }
}
void update_name(address head, ch *name, char *name1, char *number)
{
    int i;
    int len = length(head);
    for(i = 0; i < len; i++)
    {
        if(strcmp(head->next->name, name) == 0)
        {
            strcpy(head->next->name, name1);
            strcpy(head->next->number, number);
        }
        head = head->next;
    }
}
    
bo update_id(address head, el id, char *name1, char *number)
{
    int i;
    if(id <= 0 || id > length(head))
    {
        return F;
    }


    for(i = 0; i < id-1; i++)
    {
        head = head->next;
    }
  
    strcpy(head->next->name, name1);
    strcpy(head->next->number, number);
    return T;
    
}


void update_number(address head, ch *number, char *name1, char *number1)
{
    int i;
    int len = length(head);
    for(i = 0; i < len; i++)
    {
        if(strcmp(head->next->number, number) == 0)
        {
            strcpy(head->next->name, name1);
            strcpy(head->next->number, number1);
        }
        head = head->next;
    }
}
bo query_id(address head, el id)
{
    int i;
    if(id <= 0 || id > length(head))
    {
        return F;
    }


    for(i = 0; i < id - 1; i++)
    {
        head = head->next;
    }
  
    printf("%d. %s  %s\n", head->next->id, head->next->name, head->next->number);
    return T;
}


void query_name(address head, ch* name)
{
    int i;
    int len = length(head);
    for(i = 0; i < len; i++)
    {
        if(strcmp(head->next->name, name) == 0)
        {
            printf("%d. %s  %s\n", head->next->id, head->next->name, head->next->number);
        }
        head = head->next;
    }
}


void query_number(address head, ch* number)
{
    int i;
    int len = length(head);
    for(i = 0; i < len; i++)
    {
        if(strcmp(head->next->number, number) == 0)
        {
            printf("%d. %s  %s\n", head->next->id, head->next->name, head->next->number);
        }
        head = head->next;
    }
}


bo delete_id(address head, int id)
{
    int i;
    address temp1 = head;
    if(id <= 0 || id > length(head))
    {
        return F;
    }
    
    for(i = 0; i < id - 1; i++)
    {
        head = head->next;
    }


    address temp = head->next->next;
    free(head->next);
    head->next = temp;
    temp->prior = head;
    rank_id(temp1);


    return T;


}


bo delete_name(address head, ch *name)
{
    int i;
    address temp = head;
    while(temp->next != head)
    {
        if(strcmp(temp->next->name, name) == 0)
        {
             address sen = temp->next->next;
             free(temp->next);
             temp->next = sen;
             sen->prior = temp;
        }
        else
        {
            temp = temp->next;
        }
    }
    rank_id(head);
}


bo delete_number(address head, ch *number)
{
    int i;
    address temp = head;
    while(temp->next != head)
    {
        if(strcmp(temp->next->number, number) == 0)
        {
             address sen = temp->next->next;
             free(temp->next);
             temp->next = sen;
             sen->prior = temp;
        }
        else
        {
            temp = temp->next;
        }
    }
    rank_id(head);
}


bo length(address head)
{
    int count = 0;
    address temp = head;


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


bo init(address* head)
{
    address newnode = (address)malloc(sizeof(struct Address));
    if(NULL == newnode)
    {
        return F;
    }
   
    newnode->next = newnode;
    newnode->prior = newnode;
    *head = newnode;
}
bo insert_tail(address head, ch *name, ch *number)
{
    static int id = 1;
    address temp = head;
    address newnode = (address)malloc(sizeof(struct Address));
    if(NULL == newnode)
    {
        return F;
    }


    strcpy(newnode->name, name);
    strcpy(newnode->number, number);
    newnode->id = id;
    id++;
    
    head->prior->next = newnode;
    newnode->next = head;
    newnode->prior = head->prior;
    head->prior = newnode;
    rank_id(head);


    return T;
}


void rank_id(address head)
{
    int i;
    int count = 1;
    address temp = head;
    for(i = 0; i < length(head); i++)
    {
        temp->next->id = count;
        count++;
        temp = temp->next;
    }
}


void print(address head)
{
    address temp = head;


    while(temp->next != head)
    {
        printf("%d. %s   %s\n", temp->next->id, temp->next->name, temp->next->number);
        temp = temp->next;
    }
}
第一个模块——主函数main()的功能是:根据选单的选项调用各函数,并完成相应的功能。 
   第二个模块——Menu()的功能是:显示提示选单。 
   第三个模块——Quit()的功能是:退出选单。 
   第四个模块——Create()的功能是:创建新的数据记录。 
   第五个模块——Add()的功能是:增加新的数据记录,并返回选单。 
   第六个模块——Find()的功能是:按要求查询相关的信息,如果找到了,则显示该信息,如果未找到,则提示文件中没有该信息,并返回选单。 
   第七个模块——Alter()[的功能是:修改某条记录的信息,如果未找到要修改的记录,则提示系统中无此记录,并返回选单。 
   第八个模块——Delete()的功能是:删除某条记录,如果未找到要删除的记录,则提示通讯录中没有,并返回选单。 
   第九个模块——List()的功能是:显示所有记录。 一、用链表或者顺序表实现以下系统,完成线性表的建立(至少包括10个结点),以及线性表中信息(结点)的插入、查找、删除、修改、输出等操作,具体的模块要求见上方的“总的模块要求”。建议用“文件”存储数据。 1.通讯录管理系统的设计与实现 (1)通讯者信息包括:编号(char num[10])、姓名(char name[10])、性别(char sex[10])、电话(char phone[20]) (2)除了总的模块要求外,还需统计通讯录中男性人数及女性人数,并求出通讯录中的第一个模块——主函数main()的功能是:根据选单的选项调用各函数,并完成相应的功能。 
   第二个模块——Menu()的功能是:显示提示选单。 
   第三个模块——Quit()的功能是:退出选单。 
   第四个模块——Create()的功能是:创建新的数据记录。 
   第五个模块——Add()的功能是:增加新的数据记录,并返回选单。 
   第六个模块——Find()的功能是:按要求查询相关的信息,如果找到了,则显示该信息,如果未找到,则提示文件中没有该信息,并返回选单。 
   第七个模块——Alter()[的功能是:修改某条记录的信息,如果未找到要修改的记录,则提示系统中无此记录,并返回选单。 
   第八个模块——Delete()的功能是:删除某条记录,如果未找到要删除的记录,则提示通讯录中没有,并返回选单。 
   第九个模块——List()的功能是:显示所有记录。 一、用链表或者顺序表实现以下系统,完成线性表的建立(至少包括10个结点),以及线性表中信息(结点)的插入、查找、删除、修改、输出等操作,具体的模块要求见上方的“总的模块要求”。建议用“文件”存储数据。 1.通讯录管理系统的设计与实现 (1)通讯者信息包括:编号(char num[10])、姓名(char name[10])、性别(char sex[10])、电话(char phone[20]) (2)除了总的模块要求外,还需统计通讯录中男性人数及女性人数,并求出通讯录中的男女比例。 男女比例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值