通讯录

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0

typedef int Status;
typedef int ElemType;

//联系人
typedef struct contact
{
    char name[50];
    char number[20];
    char address[50];
    struct contact  *next;
}Contact;
typedef Contact *LinkList;

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

char *menu[]={"*********************",\
             "**1.insert a contact**",\
             "**2.delete a contact**",\
             "**3.check a contact **",\
             "**4.alter a contact **",\
             "**5.traver all contact **",\
             "**0.quit           **",\
             "*********************"};
//菜单
char display_menu(char **menu,int len)
{
    int i;
    char sel;
    char temp;
    while(1)
    {
    for(i=0;i<len;i++)
    {
        printf("%s\n",*(menu+i));
    }
    printf("please input your choice:");
    scanf(" %c",&sel);
    while(scanf("%c",&temp)!=EOF)
    {
        if(temp=='\n') break;
    }
    if (sel >='0' && sel < len-2+'0')
    {
        return sel;
    }
    else
    {
        printf("input error\n");
    }
    }
}


//初始化链表
Status InitList(LinkList *L)
{
    (*L) = (LinkList)malloc(sizeof(Contact));
    if ((*L) == NULL)
    {
        printf("malloc error!\n");
        return ERROR;
    }
    (*L)->next = NULL;
    return OK;
}

//
Status insert(LinkList L)
{
    LinkList p,s;
    p=L;
    s=(LinkList)malloc(sizeof(Contact));
    if(s!=NULL)
    {
        strcpy(s->name,"songxingxing");
        strcpy(s->number,"123456");
        strcpy(s->address,"anhui");
        s->next = p->next;
        p->next = s;
    }
    s=(LinkList)malloc(sizeof(Contact));
    if(s!=NULL)
    {
        strcpy(s->name,"tangchao");
        strcpy(s->number,"789456");
        strcpy(s->address,"shanghai");
        s->next = p->next;
        p->next = s;
    }
    return OK;


}
//求链表长度
int ListLength(LinkList L)
{
    int i=0;
    LinkList p = L->next;
    while(p != NULL)
    {
        p = p->next;
        i++;
    }
    return i;
}


//添加联系人信息
Status ListInsert(LinkList L,int i)
{
    LinkList p,s;
    int j;
    if (L == NULL)
    {
        printf("Linklist is not init\n");
        return ERROR;
    }
    if (i<1 || i>ListLength(L)+1)
    {
        printf("insert position error\n");
        return ERROR;
    }
    p = L;
    j = 1;
    while(p != NULL && j<i)
    {
        p = p->next;
        j++;
    }
    s = (LinkList)malloc(sizeof(Contact));
    if (s == NULL)
    {
        printf("malloc error!\n");
        return ERROR;
    }
    printf("请输入联系人信息:\n");
    printf("姓名:"); scanf("%s",s->name);
    printf("电话:"); scanf("%s",s->number);
    printf("住址:"); scanf("%s",s->address);

    s->next = p->next;
    p->next = s;
    printf("添加成功!\n");
    return OK;
}

//判断是否为空
Status ListEmpty(LinkList L)
{
    if (L->next == NULL)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

//删除指定联系人
Status ListDelete(LinkList L)
{
    LinkList p,q;
    char temp;
    char a[50];
    char m;
//    int j;
    if (L == NULL)
    {
        printf("Linklist is not init\n");
        return ERROR;
    }
    /*if (i<1 || i>ListLength(*L))
    {
        printf("delete position error\n");
        return ERROR;
    }*/
    printf("Please input the name you want to delete:");
    scanf("%s",a);
    p = L;
    while(p->next!= NULL)
    {
        if(strcmp(p->next->name,a)!=0)
            p=p->next;
        else break;
    
    } 
    if(p->next==NULL)
    {    printf("NOT FOUND\n");
         return OK;
    }
    else
    {
       printf("SURE?(Y/N):\n");
       scanf(" %c",&m);
       while(scanf("%c",&temp)!=EOF)
       {
        if(temp=='\n') break;
       }
       if(m=='Y')
       {
          q = p->next;
          p->next = q->next;
          free(q);
          printf("delete OK!\n");
       }
    }
    
    //else 
    //    printf("NOTFOUND!\n");
    return OK;
}

//查看指定联系人信息
Status Listcheck(LinkList L)
{
    char a[50];
    char m,temp;
    LinkList p;
    printf("Please input the name of contact:");
    scanf("%s",a);
    p = L->next;
    while(p != NULL)
    {
        if(strcmp(p->name,a)!=0)
            p=p->next;
        else break;
    
    }
    if(p==NULL)
    {
        printf("NOT FOUND\n");
        return OK;
    }
    else
    {
         printf("NAME:%s\n",p->name);
         printf("NUMBER:%s\n",p->number);
         printf("ADDRESS:%s\n",p->address);
         printf("\n");
        
    }
    printf("IF YOU NEED TO ALTER THE MESSAGE OF THE CONTACT?(Y/N):\n");
    scanf(" %c",&m);
    while(scanf("%c",&temp)!=EOF)
    {
        if(temp=='\n') break;
    }
    if(m=='Y')
     {
            printf("Please input the message of the contact:\n");
            printf("NAME:"); scanf("%s",p->name);
            printf("NUMBER:"); scanf("%s",p->number);
             printf("ADDRESS:"); scanf("%s",p->address);
            printf("alter OK!\n");
    
     }
    //else if(m=='N')
    return OK;
}

//修改联系人信息
Status ListAlter(LinkList L)
{
    char a[50];
    char temp;
    LinkList p;
    printf("Please input the name you want to alter:");
    scanf("%s",a);
    p=L->next;
    char m;
        while(p != NULL)
    {
        if(strcmp(p->name,a)!=0)
            p=p->next;
        else break;
    
    }
    if(p==NULL)
    {
        printf("NOT FOUND\n");
        return OK;
    }
    else
    {
         printf("NAME:%s\n",p->name);
         printf("NUMBER:%s\n",p->number);
         printf("ADDRESS:%s\n",p->address);
         printf("\n");
        
    }
    
    printf("IF YOU NEED TO ALTER THE MESSAGE OF THE CONTACT?(Y/N):\n");
    scanf(" %c",&m);
    while(scanf("%c",&temp)!=EOF)
    {
        if(temp=='\n') break;
    }
    if(m=='Y')
     {
            printf("Please input the message of the contact:\n");
            printf("NAME:"); scanf("%s",p->name);
            printf("NUMBER:"); scanf("%s",p->number);
             printf("ADDRESS:"); scanf("%s",p->address);
            printf("alter OK!\n");
    
     }
    //else if(m=='N')
    
     return 0;
}


//遍历联系人信息
Status ListTraver(LinkList L)
{
     LinkList p=L->next;
     while(p!=NULL)
     {
         printf("NAME:%s\n",p->name);
         printf("NUMBER:%s\n",p->number);
         printf("ADDRESS:%s\n",p->address);
         printf("\n");
         p=p->next;
     }
     return OK;
 }


int main()
{
        LinkList L;
        char sel;

    if(ERROR == InitList(&L))  //查看是否初始化
    {
        exit(1);
    }
    insert(L);

    while(1)
    {
    sel = display_menu(menu,8);
    switch(sel)
    {
        case '0':printf("thanks\n");exit(0);break;
        case '1':ListInsert(L,1);break;
        case '2':ListDelete(L);break;
        case '3':Listcheck(L);break;
        case '4':ListAlter(L);break;
        case '5':ListTraver(L);break;
        default:break;
    }
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值