简单的通讯录

来源:互联网 发布:五子棋人机对战算法 编辑:程序博客网 时间:2024/06/08 19:26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define T 1
#define F -1

typedef int Status;
typedef char Type_nam;
typedef char Type_num;
typedef char Type_add;

struct List
{
     Type_nam name[20];
     Type_num number[20];
     Type_add address[20];
     int count;
     struct List *next;

};


Type_nam *name_1[] = {"xiaowang","xiaoming","xiaofang","xiaochen"};
Type_num *number_1[] = {"13712345674","13712345672","13712345671","13712345673"};
Type_add *address_1[] = {"jiangning1","jiangning2","jiangning3","jiangning4"};


Type_nam *name_2[] = {"xiaocai"};
Type_num *number_2[] = {"13112345675"};
Type_add *address_2[] = {"liuhe"};

Status init (struct List** head);


Status insert(struct List *head, Type_nam *name_2, Type_num *number_2, Type_add *address_2, int count_2);


Status delete_cou(struct List *head, int count_2);
Status delete_num(struct List *head, Type_num *number_2);
Status delete_nam(struct List *head, Type_nam *name_2);




Status update_cou(struct List *head, Type_nam *name_3, Type_num *number_3, Type_add *address_3, int count_3);
Status update_nam(struct List *head, Type_nam *name_3, Type_num *number_3, Type_add *address_3, Type_nam *name_4);
Status update_num(struct List *head, Type_nam *name_3, Type_num *number_3, Type_add *address_3, Type_num *number_4);


Status query_cou(struct List *head, int count_5);
Status query_nam(struct List *head, Type_nam *name_5);
Status query_num(struct List *head, Type_num *number_5);


Status sort_num(struct List *head);
Status sort_nam(struct List *head);


void print(struct List *head);






int main()
{
    
    int ret = 0;


    struct List* head = NULL;
    ret = init(&head);
    if(F == ret)
    {
        printf("error init\n");
    }
    if(T == ret)
    {
        printf("init ok\n");
    
    }
    
    int i;
    for(i = 0; i < 4; i++)
    {
        insert(head, name_1[i],number_1[i],address_1[i], i + 1);
    }
     print(head);
     
     //delete_cou(head, 3); 
     //printf("****按编号删除******************************************\n");
     //print(head);


     //delete_num(head, number_1[1]);
     //printf("****按号码删除******************************************\n");
     //print(head);


     //delete_nam(head, name_1[2]);
     //printf("****按名字删除******************************************\n");
     //print(head);
     
    // update_cou(head, name_2[0], number_2[0], address_2[0], 3); 
    // printf("****按编号替换******************************************\n");
    // print(head);
    
    //update_nam(head, name_2[0], number_2[0], address_2[0], name_1[2]);
    //printf("****按名字替换******************************************\n");
    //print(head);


    //update_num(head, name_2[0], number_2[0], address_2[0], number_1[2]);
    //printf("****按号码替换******************************************\n");
    //print(head);
    
    //printf("****按编号查找******************************************\n");
    //query_cou(head, 3);


    //printf("****按名字查找******************************************\n");
    //query_nam(head, name_1[2]);


    //printf("****按号码查找******************************************\n");
    //query_num(head, number_1[2]);


    //printf("****按号码排序******************************************\n");
    //sort_num(head);
    //print(head);
    printf("****按名字排序******************************************\n");
    sort_nam(head);
    print(head);
}


Status init (struct List** head)
{
   
    struct List *newlist = (struct List*)malloc(sizeof(struct List));
    if(NULL == newlist)
    {
        return F;
    }
    newlist->next = NULL;
    *head = newlist;
    
    return T;


}


Status insert(struct List *head, Type_nam *name_2, Type_num *number_2, Type_add *address_2, int count_2)
{
    struct List *newlist = (struct List*)malloc(sizeof(struct List));
    if(NULL == newlist)
    {
        return F;
    }
    strcpy(newlist->name, name_2);
    strcpy(newlist->number,  number_2);
    strcpy(newlist->address, address_2);
    newlist->count = count_2;
    newlist->next = NULL;


    while(head->next != NULL)
    {
        head = head->next;
    }
    
    head->next = newlist;


    return T;


}
Status delete_cou(struct List *head, int count_2)
{
    while(count_2 != head->next->count)
    {
        head = head->next;
if(head->next == NULL)
{
    printf("error count!");
    return;
}
    }
    struct List *temp;
    temp = head->next->next;
    free(head->next);
    head->next = temp;
}
Status delete_num(struct List *head, Type_num *number_2)


{
    while(strcmp((number_2) ,(head->next->number)) != 0)
    {
        head = head->next; 
if(head->next == NULL)
{
   printf("error number!\n");
   return;

}
    }
    struct List *temp;
    temp = head->next->next;
    free(head->next);
    head->next = temp;
    
   


}


Status delete_nam(struct List *head, Type_nam *name_2)
{
    while(strcmp((head->next->name), (name_2)) != 0)
    {
        head = head->next;
if(NULL == head->next )
{
   printf("error name!\n");
   return;
}
    }
    struct List *temp;
    temp = head->next->next;
    free(head->next);
    head->next = temp;


}


Status update_cou(struct List *head, Type_nam *name_3, Type_num *number_3, Type_add *address_3, int count_3)
{
    while(count_3 != head->next->count)
    {
        head = head->next;
if(NULL == head->next)
{
   printf("error update count!");
   return;
}
    
    }
    strcpy(head->next->name, name_3);
    strcpy(head->next->number, number_3);
    strcpy(head->next->address, address_3);




}
Status update_nam(struct List *head, Type_nam *name_3, Type_num *number_3, Type_add *address_3, Type_nam *name_4)
{
    while(strcmp((name_4) , (head->next->name)) != 0)
    {
        head = head->next;
if(NULL == head->next)
{
   printf("error update name!");
   return;
}
    
    
    }
    strcpy(head->next->name, name_3);
    strcpy(head->next->number, number_3);
    strcpy(head->next->address, address_3);


}




Status update_num(struct List *head, Type_nam *name_3, Type_num *number_3, Type_add *address_3, Type_num *number_4)
{
    while(strcmp((number_4), (head->next->number)) != 0)
    {
        head = head->next;
if (NULL == head->next)
{
   printf("error update number!");
   return;
}
    
    }
    strcpy(head->next->name, name_3);
    strcpy(head->next->number, number_3);
    strcpy(head->next->address, address_3);


}


Status query_cou(struct List *head, int count_5)
{
    while(count_5 != head->next->count)
    {
        head = head->next;
if(NULL == head->next)
{
   printf("error query count!\n");
   return;
}
    }
     printf("%d姓名:%s\t电话:%s\t住址:%s\n",head->next->count, head->next->name, head->next->number, head->next->address);
}


Status query_nam(struct List *head, Type_nam *name_5)
{
    
    while(strcmp((name_5) ,( head->next->name)) != 0)
    {
        head = head->next;
if(NULL == head->next)
{
   printf("error query name!\n");
   return;
}
    }
     printf("%d姓名:%s\t电话:%s\t住址:%s\n",head->next->count, head->next->name, head->next->number, head->next->address);
}


Status query_num(struct List *head, Type_num *number_5)
{


    while(strcmp((number_5) ,( head->next->number)) != 0)
    {
        head = head->next;
if(NULL == head->next)
{
   printf("error query number!\n");
   return;
}
    }
     printf("%d姓名:%s\t电话:%s\t住址:%s\n",head->next->count, head->next->name, head->next->number, head->next->address);




}


Status sort_num(struct List *head)
{
    int i;
    int j;
    
    struct List *temp = (struct List*)malloc(sizeof(struct List));
    if(NULL == temp)
    {
        return F;
    }
    struct List *p;
    
    for(i = 0; NULL != head->next; i++)
    {    
     p = head;
for(j = 0;j < 4 - i; j++)
{    
            p = p->next;
   if(NULL == p->next)
   {
       continue;
   
   }


   if(strcmp((head->next->number) ,( p->next->number)) > 0 )
            {
                
              strcpy(temp->name, head->next->name);
     strcpy(temp->number, head->next->number);
     strcpy(temp->address,head->next->address); 
     strcpy(head->next->name, p->next->name);
     strcpy(head->next->number, p->next->number);
     strcpy(head->next->address, p->next->address);
              strcpy(p->next->name, temp->name);
              strcpy(p->next->number, temp->number);
              strcpy(p->next->address, temp->address);




      //temp = head->next;   /*段错误*/
      //head->next = p->next;
      //p->next = temp;
//strcpy(temp, (head->next));  /*不兼容指针类型*/
//strcpy((head->next), (head->next->next));
//strcpy((head->next), temp);


   }


        }
    
    head = head->next;
    } 


}


Status sort_nam(struct List *head)
{
    int i;
    int j;
    
    struct List *temp = (struct List*)malloc(sizeof(struct List));
    if(NULL == temp)
    {
        return F;
    }
    struct List *p;
    
    for(i = 0; NULL != head->next; i++)
    {    
     p = head;
for(j = 0;j < 4 - i; j++)
{    
            p = p->next;
   if(NULL == p->next)
   {
       continue;
   
   }


   if(strcmp((head->next->name) ,( p->next->name)) > 0 )
            {
                
              strcpy(temp->name, head->next->name);
     strcpy(temp->number, head->next->number);
     strcpy(temp->address,head->next->address); 
     strcpy(head->next->name, p->next->name);
     strcpy(head->next->number, p->next->number);
     strcpy(head->next->address, p->next->address);
              strcpy(p->next->name, temp->name);
              strcpy(p->next->number, temp->number);
              strcpy(p->next->address, temp->address);
             
   }
        }
    head = head->next;
    }
}
void print(struct List *head)
{
    while(NULL != head->next)
    {
        printf("%d姓名:%s\t电话:%s\t住址:%s\n",head->next->count, head->next->name, head->next->number, head->next->address);
head = head->next;
    }




}
原创粉丝点击