通讯录 改

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

struct node
{
    char name[15];
    char address[20];
    char tel[11];
    struct node *next ;
};

void creat_link(struct node **head)
{
    *head = (struct node *)malloc(sizeof(struct node));
    (*head)->next = NULL;
}

void insert_node(struct node *head)
{
    struct node *new_node ;
    new_node = (struct node *)malloc(sizeof(struct node));
    if( new_node == NULL )
    {
        printf ("malloc error\n");
        exit(-1) ;
    }
    printf("姓名:");
    gets(new_node->name);
    getchar();
    printf("住址:");
    gets(new_node->address);
    getchar();
    printf("电话:");
    gets(new_node->tel);
    getchar();
    printf("2");
    if(head->next==NULL)
    {
        head->next = new_node;
        new_node->next = NULL; 
    }
    else
    {
        new_node->next = head->next ;
        head->next = new_node;
    }
}
 
void search_node(struct node *head)
{
    char str1[20];
    struct node *p ;
    p = head->next ;
    printf("请输入查询目标:");
    gets(str1);
    if (p == NULL)     
    {
        printf("通讯录为空\n");
    }
    else
    {
        while( p != NULL)
        {
            if(strcmp(str1,p->name)==0)
            { 
                printf("姓名 :  %s\n" , p->name);
                printf("地址 :  %s\n" , p->address);
                printf("电话 :  %s\n" , p->tel);
            }
            if(strcmp(str1,p->address)==0)
            { 
                printf("姓名 :  %s\n" , p->name);
                printf("地址 :  %s\n" , p->address);
                printf("电话 :  %s\n" , p->tel);
            }
            if(strcmp(str1,p->tel)==0)
            { 
                printf("姓名 :  %s\n" , p->name);
                printf("地址 :  %s\n" , p->address);
                printf("电话 :  %s\n" , p->tel);
            }
            p = p->next ;
        }
        if (p == NULL)
        {
            printf("不在通讯录中\n");
        }
    }
}

void change_node(struct node *head)
{
    char str3[20];
    struct node *p , *q ;
    q = head ;
    p = head->next ;
    printf("请输入更改对象:");
    gets(str3);
    if(p == NULL)
    {
        printf("通讯录为空\n");
    }
    else
    {
        while(q != NULL )
        {
            if(strcmp(str3,p->name) == 0)
            {
                printf("姓名 :%s\n" , p->name);
                printf("地址 :%s\n" , p->address);
                printf("电话 :%s\n\n" , p->tel);
                printf("请输入新姓名:");
                gets(p->name);
                printf("请输入新地址:");
                gets(p->address);
                printf("请输入新电话:");
                gets(p->tel);
                printf("已修改\n");
                continue;
            }
            if(strcmp(str3,p->address) == 0)
            {
                printf("姓名 :%s\n" , p->name);
                printf("地址 :%s\n" , p->address);
                printf("电话 :%s\n\n" , p->tel);
                printf("请输入新姓名:");
                gets(p->name);
                printf("请输入新地址:");
                gets(p->address);
                printf("请输入新电话:");
                gets(p->tel);
                printf("已修改\n");
                continue;
            }
            if(strcmp(str3,p->tel) == 0)
            {
                printf("姓名 :%s\n" , p->name);
                printf("地址 :%s\n" , p->address);
                printf("电话 :%s\n\n" , p->tel);
                printf("请输入新姓名:");
                gets(p->name);
                printf("请输入新地址:");
                gets(p->address);
                printf("请输入新电话:");
                gets(p->tel);
                printf("已修改\n");
                continue;
            }
            q = p ;
            p = p->next;
        }
        if(p == NULL)
        {
            printf("无法找到\n");
        }
    }
}

void delate_node(struct node *head) 
{
    int a;
    char str2[20] ;
    struct node *p, *q;
    q = head;
    p = head->next;
    printf("请输入删除目标:");
    gets(str2);
    printf("是否删除 %s ? 是'1'  否'2'\n",str2);
    scanf("%d",a);
    while(a == 1)
    {
        if("p == NULL")
        {
            printf("通讯录不存在\n");
        }
        else
        {
            while(q != NULL)
            {
                if(strcmp(str2,p->name))
                {
                    q->next = p->next ;
                    free(p);
                    p = q->next;
                    printf("已删除\n");
                    continue;
                }
                if(strcmp(str2,p->address))
                {
                    q->next = p->next ;
                    free(p);
                    p = q->next;
                    printf("已删除\n");
                    continue;
                }
                if(strcmp(str2,p->tel))
                {
                    q->next = p->next ;
                    free(p);
                    p = q->next;
                    printf("已删除\n");
                    continue;
                }
                q = p ;
                p = p->next ;
            }
            if(q == NULL);
            {
                printf("目标不存在\n");
            }
        }           
    }
}

void display_node(struct node *head)
{
    struct node *p ;
    p = head->next ;
    if (p ==NULL)
    {
        printf ("link is empty\n");
    }
    else
    {
        while(p != NULL)
        {
            printf("姓名 :  %s\n" , p->name);
            printf("地址 :  %s\n" , p->address);
            printf("电话 :  %s\n\n" , p->tel);
            p = p->next;
        }
    }
}

void release_link(struct node **head )
{
    struct node *p, *q ;
    q = (*head);
    p = (*head)->next ;
    if (p == NULL)
    {
        free(head);
        printf("link is empty\n");
    }
    else 
    {
        while ( p != NULL)
        {
            q = p ;
            p = p->next ;
            free(q);
        }
        if(p == NULL)
        {      
            printf("link has been relesed\n");
        }
    }
}

void face_1()
{
    printf("***************************************\n");
    printf("******    1.新建联系人    2.修改联系人    3.删除联系人    *******\n");
    printf("******    4.查询联系人    5.显示联系人    6.退出通讯录    *******\n");
    printf("***************************************\n");
}

int main()
{
    struct node *head = NULL;
    int  a ;
    
    while(1)
    {
        face_1() ;

        scanf("%d" , &a) ;

        switch(a)
        {
            case   1  : insert_node( head );
                        printf("************************************************************************\n");
                        break;                        
            case   2  : change_node( head );
                        printf("************************************************************************\n");
                        break;
            case   3  : delate_node( head );
                        printf("************************************************************************\n");
                        break;
            case   4  : search_node( head );
                        printf("************************************************************************\n");
                        break;
            case   5  : display_node( head );
                        printf("************************************************************************\n");
                        break;
            case   6  : release_link( &head );
                        exit(0);
            default   : printf("请重新输入\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值