C语言学习总结--通讯录项目

在写通讯录前,基本没有什么思路,但是脑海中对于大概用到了什么功能还是有数的,就是无法有效的串联,当时请教了老师,老师给的建议是,把链表关于增删改查的操作,多敲几遍就会有所收获

本次用到的知识模块有

1、数据结构--单向链表

2、结构体

3、文件编程

4、排序

本程序实现了

(理论上本通讯录人数无上限)

1、添加联系人:
如:学号、姓名、电话、qq号、家庭住址等
2、查找联系人:根据内容查找到联系人并输出信息,显示符合条件联系人的个数等
3、显示所有联系人(按添加时间)
4、删除联系人:先按照求查找到相应的联系人,再根据输入要求(如删除查找到的哪一个联系人等)删除(没找到或输入错误退出删除)
5、修改联系人信息:先按照求查找到相应的联系人,再根据输入要求(如修改查找到的哪一个联系人、修改该联系人的哪一个信息等)修改(没找到或输入错误退出删除)。
修改时还要注意修改后的信息是否符合要求(如输入的学号全部为数字且不能为空等)。
6、排序显示(堆排序和快速排序):请选择排序方式(学号、姓名、电话、qq号、家庭住址等)(最难)
7、退出(将链表中的内容写入文件中)
8、程序开始运行后读取文件中原有联系人

缺点也很明显,排序是硬伤,只实现了最简单的排序,在后期学习中会逐步完善,快排有一些问题待调整

代码全部奉上,有问题的话删掉快排
#include 
#include 
#include 
#include 

#define F 0
#define T 1

typedef int Elementype;
typedef int Status;


typedef struct link
{
    char num[15];        //学号
    char name[30];       //姓名
    char telephone[12];  //电话
    char family_address[30];    //地址
    char QQ[11];         //QQ号
    struct link *next;
}Linker;

Status init(Linker **head);            //初始化函数
Status insert_tail(Linker *head);      //新建联系人
void print(Linker *head);              //输出函数
Status fscanf_file(Linker *head);      //读文件
void query(Linker *head);              //查找联系人
Status delete(Linker *head);           //删除联系人
Status update(Linker *head);           //修改联系人信息
Status fprint_file(Linker *head);      //输出到文件
void clear(Linker **head);             //清空缓存
void sort_show(Linker *head);          //排序函数
void swap(Linker* p);                  //交换函数
Status count(Linker* head);            //计数函数
void bubblen(Linker* head);            //冒泡学号排序
void bubblena(Linker* head);           //冒泡姓名排序
void bubbleQ(Linker* head);            //冒泡QQ排序
void bubblea(Linker* head);            //冒泡地址排序
void bubblet(Linker* head);            //冒泡电话排序
void quicksort(Linker* arr[1024],Linker* head);       //快速排序
Status pivotkey(Linker* arr[1024],int low,int high);
void selectsort(Linker* head);         //选择排序法(从大到小)

int main()
{
    struct tm *t;
    time_t tt;
    time(&tt);
    t = localtime(&tt);    //打印系统的当前时间
  
    Linker *head;
    init(&head);
    fscanf_file(head);
    int choose;

    while(1)
    {
        system("clear");
        printf("                *********************************************\n");
        printf("                *                    主菜单                 *\n");
        printf("                * 输入数字执行操作:                        *\n");
        printf("                *      1:查看联系人        4.更改联系人信息 *\n");
        printf("                *      2:新建联系人        5.删除联系人     *\n");
        printf("                *      3:查找联系人        0:退出           *\n");
        printf("                *                                           *\n");
        printf("                *     操作时间:%4d年%02d月%2d日%02d:%02d:%02d      *\n",
                                                t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
        printf("                *********************************************\n");
        printf("\n");
        printf("请输入您需要的操作:");
        scanf("%d",&choose);
      
        if(choose < 0 || choose > 5)
        {
            printf("您的输入有误!\n");
            return;
        }

        while(choose >= 0 && choose < 6)
        {
            switch(choose)
            {
                case 1:
                        sort_show(head);//排序并显示
                        print(head);
                        printf("******************************************************************************************\n");
                        break;
                case 2:
                        insert_tail(head);//插入并输出到文件
                        fprint_file(head);
                        printf("******************************************************************************************\n");
                        break;
                case 3:
                        query(head);
                        printf("******************************************************************************************\n");
                        break;
                case 4:
                        update(head);//更新信息并输出到文件
                        fprint_file(head);
                        printf("******************************************************************************************\n");
                        break;
                case 5:
                        delete(head);//删除联系人,并把新的链表信息写入文本
                        fprint_file(head);
                        printf("******************************************************************************************\n");
                        break;
                case 0:
                        clear(&head);
                        printf("******************************************************************************************\n");
                        break;
                default:
                        break;
            }

            if(choose == 0)
            {
                break;
            }
            
            printf("请再次输入您需要的操作(需要菜单提示请按y):");
            char a;
            scanf("%c",&a);
            if(a == 'y' || a == 'Y')
            {
                printf("\n---------------------------------------------------------------\n");
                printf("                ******************1111班通讯录*************\n");
                printf("                *                    主菜单                 *\n");
                printf("                * 输入数字执行操作:                        *\n");
                printf("                *      1:查看联系人        4.更改联系人信息 *\n");
                printf("                *      2:新建联系人        5.删除联系人     *\n");
                printf("                *      3:查找联系人        0:退出           *\n");
                printf("                *                                           *\n");
                printf("                *********************************************\n");
                printf("\n");

            }

            scanf("%d",&choose);
        }
        if(choose == 0)
        {
            printf("\n*****************欢迎再次使用!***************\n");
            break;
        }
    }

    return 0;
}


Status init(Linker **head)
{
    Linker *newnode = (Linker *)malloc(sizeof(struct link));
    if (newnode == NULL)
    {
        return F;
    }

    newnode->next = NULL;
    *head = newnode;

    return T;
}

Status fscanf_file(Linker *head)
{
    int flag;
    Linker *newnode,*p;
    p = head;
    
    FILE *file = fopen("/mnt/hgfs/share/addressbook.txt","r");
    if(NULL == file)
    {
        perror("fopen file");
        return F;
    }
    
    fscanf(file,"%d",&flag);
    
    while(flag != 0)
    {
        p = head;
        
        newnode = (Linker *)malloc(sizeof(struct link));
        if (newnode == NULL)
        {
            return F;
        }

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

        fscanf(file,"%s%s%s%s%s",newnode->num,newnode->name,newnode->telephone,newnode->QQ,
                                newnode->family_address);
        fscanf(file,"%d",&flag);

        newnode->next = NULL;
        p->next = newnode;

    }

    fclose(file);
    return T;
}

Status fprint_file(Linker *head)
{
    
    int k = 1;
    FILE *file2 = fopen("/mnt/hgfs/share/addressbook.txt","w+");//每次重新写入信息
    {
        if(file2 == NULL)
        {
            perror("fopen file2");
            exit(2);
        }
    }

    while(head->next != NULL)
    {
        fprintf(file2, "%d ",k++);   //为联系人编号
        fprintf(file2,"%-11s %-15s %-11s %-10s %-20s\r\n"
                ,head->next->num,head->next->name,head->next->telephone,head->next->QQ,head->next->family_address);
        head = head->next;
    }

    fprintf(file2, "%d\n",0);//标志位,如果只有0,没有联系人
    fclose(file2);

    return T;
}

Status insert_tail(Linker *head)
{
    Linker *p = head;
    Linker *q = head;
    int i;
    
    Linker *newnode = (Linker *)malloc(sizeof(struct link));
    if (newnode == NULL)
    {
        return F;
    }

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

    printf("\n请输入新建联系人的学号:\n");
    scanf("%s",newnode->num);
    while(strlen(newnode->num) != 11)
    {
        printf("请重新输入新建联系人的学号:\n");
        scanf("%s",newnode->num);
    }

    int flag = 0;
    while(q->next != NULL || flag == 1)
    {
        flag = 0;
        if (strcmp(q->next->num,newnode->num) == 0)
        {
            flag = 1;
            printf("已有该联系人:\n");
            printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n",q->next->num,
            q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);
            
        }

        printf("如需要再次添加请按1,如不再重复添加请按2\n");
        scanf("%d",&i);
        if(i == 1)
        {
            flag = 0;
            break;
        }

        if(flag == 1)
        {
            printf("请重新输入新建联系人的学号:\n");
            scanf("%s",newnode->num);           
            q = head;    
        }
        
        q = q->next;
        
        if(flag == 0 && q->next == NULL)
        {
            break;
        }   
    }

    q = head; 

    printf("\n请输入新建联系人的姓名:\n");
    scanf("%s",newnode->name);
    flag = 0;
    while(q->next != NULL || flag == 1)
    {
        flag = 0;
        if (strcmp(q->next->name,newnode->name) == 0)
        {
            flag = 1;
            printf("已有该联系人:\n");
            printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n",q->next->num,
            q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);
            
        }

        printf("如需要再次添加请按1,如不再重复添加请按2\n");
        scanf("%d",&i);
        if(i == 1)
        {
            flag = 0;
            break;
        }

        if(flag == 1)
        {
            printf("请重新输入新建联系人的姓名:\n");
            scanf("%s",newnode->name);
            
            q = head;
 
        }
        
        q = q->next;

        if(flag == 0 && q->next == NULL)
        {
            break;
        }
    }

    q = head; 


    printf("\n请输入新建联系人的电话号码:\n");
    scanf("%s",newnode->telephone);
    
    while(strlen(newnode->telephone) != 11)
    {
        printf("请重新输入新建联系人的电话号码:\n");
        scanf("%s",newnode->telephone);
    }
    flag = 0;
    
    while(q->next != NULL || flag == 1)
    {
        flag = 0;
        if (strcmp(q->next->telephone,newnode->telephone) == 0)
        {
            flag = 1;
            printf("已有该联系人:\n");
            printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n",q->next->num,
            q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);
            
        }

        printf("如需要再次添加请按1,如不再重复添加请按2\n");
        scanf("%d",&i);
        if(i == 1)
        {
            flag = 0;
            break;
        }

        if(flag == 1)
        {
            printf("请重新输入新建联系人的电话号码:\n");
            scanf("%s",newnode->telephone);
            
            q = head;

        }

        q = q->next;

        if(flag == 0 && q->next == NULL)
        {
            break;
        }
    }

    q = head; 
    
    printf("\n请输入新建联系人的地址:\n");
    scanf("%s",newnode->family_address);
    flag = 0;
    while(q->next != NULL || flag == 1)
    {
        flag = 0;
        if (strcmp(q->next->family_address,newnode->family_address) == 0)
        {
            flag = 1;
            printf("已有该联系人:\n");
            printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n",q->next->num,
            q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);          
        }

        printf("如需要再次添加请按1,如不再重复添加请按2\n");
        scanf("%d",&i);
        if(i == 1)
        {
            flag = 0;
            break;
        }

        if(flag == 1)
        {
            printf("请重新输入新建联系人的地址:\n");
            scanf("%s",newnode->family_address);
            
            q = head;
        }

        q = q->next;

        if(flag == 0 && q->next == NULL)
        {
            break;
        }
    }

    q = head;
    
    printf("\n请输入新建联系人的QQ:\n");
    scanf("%s",newnode->QQ);
    while(strlen(newnode->QQ) > 10)
    {
        printf("\n请重新输入新建联系人的QQ:\n");
        scanf("%s",newnode->QQ);
    }
    flag = 0;
    while(q->next != NULL || flag == 1)
    {
        flag = 0;
        if (strcmp(q->next->QQ,newnode->QQ) == 0)
        {
            flag = 1;
            printf("已有该联系人:\n");
            printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n",q->next->num,
            q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);          
        }

        printf("如需要再次添加请按1,如不再重复添加请按2\n");
        scanf("%d",&i);
        if(i == 1)
        {
            flag = 0;
            break;
        }

        if(flag == 1)
        {
            printf("请重新输入新建联系人的QQ:\n");
            scanf("%s",newnode->QQ);
            
            q = head;

        }

        q = q->next;

        if(flag == 0 && q->next == NULL)
        {
            break;
        }
    }

    q = head;
    
    newnode->next = NULL;
    p->next = newnode;

    if(flag == 0)
    {
        printf("\n*****************添加联系人成功!********************\n");
    }


    return T;
}

void query(Linker *head)
{
    int choose,flag = 0;
    int i;
    Linker *q = head;
    char array[50];

    while(1)
    {
        printf("                **********************************************\n");
        printf("                *   请选择查找方式:                         *\n");
        printf("                *      1:根据姓名查找       2.根据地址查找   *\n");
        printf("                *      3:根据号码查找       4.根据QQ查找     *\n");
        printf("                *      5:根据学号查找       0:返回上一级菜单 *\n");  
        printf("                **********************************************\n");

        printf("请输入选择的查找方式的序号:");
        scanf("%d",&choose);

        switch(choose)
        {
            case 1:
                    flag = 0;

                    printf("请输入要查找联系人的姓名:");
                    scanf("%s",array);
                    while(q->next != NULL && flag == 0)
                    {
                        flag = 0;
                        for(i = 0;i < count(head);i++)
                        {
                            if(strcmp(q->next->name,array) == 0)
                            {
                                flag = 1;
                                printf("找到该联系人:");
                                printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n\n\n",q->next->num,
                                q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);
                            }
                            q = q->next;
                        } 

                        if(flag == 0)
                        {
                            printf("查无此人,请重新输入联系人姓名(退出按exit):");
                            scanf("%s",array);
                            q = head;
                        }

                        if(flag == 1)
                        {
                            break;
                        }
                        if(strcmp(array,"exit") == 0)
                        {
                            break;
                        }
                    }

                    q = head;
                    break;
            case 2:
                   flag = 0;

                    printf("请输入要查找联系人的地址:");
                    scanf("%s",array);
                    while(q->next != NULL || flag == 0)
                    {
                        flag = 0;
                        for(i = 0;i < count(head);i++)
                        {
                            if(strcmp(q->next->family_address,array) == 0)
                            {
                                flag = 1;
                                printf("找到该联系人:");
                                printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n\n\n",q->next->num,
                                q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);    
                            }
                            q = q->next;
                        } 

                        if(flag == 0)
                        {
                            printf("查无此人,请重新输入联系人地址(退出按exit):");
                            scanf("%s",array);
                            q = head;
                        }

                        if(flag == 1)
                        {
                            break;
                        }
                        if(strcmp(array,"exit") == 0)
                        {
                            break;
                        }
                    }

                    q = head;
                    break;                


            case 3:
                                        flag = 0;

                    printf("请输入要查找联系人的号码:");
                    scanf("%s",array);
                    while(q->next != NULL || flag == 0)
                    {
                        flag = 0;
                        for(i = 0;i < count(head);i++)
                        {
                            if(strcmp(q->next->telephone,array) == 0)
                            {
                                flag = 1;
                                printf("找到该联系人:");
                                printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n\n\n",q->next->num,
                                q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);
                                
                            }
                            q = q->next;
                        } 

                        if(flag == 0)
                        {
                            printf("查无此人,请重新输入联系人号码(退出按exit):");
                            scanf("%s",array);
                            q = head;
                        }

                        if(flag == 1)
                        {
                            break;
                        }
                        if(strcmp(array,"exit") == 0)
                        {
                            break;
                        }
                    }

                    q = head;
                    break;

 

            case 4:
                    flag = 0;

                    printf("请输入要查找联系人的QQ:");
                    scanf("%s",array);
                    while(q->next != NULL || flag == 0)
                    {
                        flag = 0;
                        for(i = 0;i < count(head);i++)
                        {
                            if(strcmp(q->next->QQ,array) == 0)
                            {
                                flag = 1;
                                printf("找到该联系人:");
                                printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n\n\n",q->next->num,
                                q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);
                                
                            }
                            q = q->next;
                        } 

                        if(flag == 0)
                        {
                            printf("查无此人,请重新输入联系人QQ(退出按exit):");
                            scanf("%s",array);
                            q = head;
                        }

                        if(flag == 1)
                        {
                            break;
                        }
                        if(strcmp(array,"exit") == 0)
                        {
                            break;
                        }
                    }

                    q = head;
                    break;

            case 5:
                    flag = 0;

                    printf("请输入要查找联系人的学号:");
                    scanf("%s",array);
                    while(q->next != NULL || flag == 0)
                    {
                        flag = 0;
                        for(i = 0;i < count(head);i++)
                        {
                            if(strcmp(q->next->num,array) == 0)
                            {
                                flag = 1;
                                printf("找到该联系人:");
                                printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n\n\n",q->next->num,
                                q->next->name,q->next->telephone,q->next->QQ,q->next->family_address);
                                
                            }
                            q = q->next;
                        } 

                        if(flag == 0)
                        {
                            printf("查无此人,请重新输入联系人学号(退出按exit):");
                            scanf("%s",array);
                            q = head;
                        }

                        if(flag == 1)
                        {
                            break;
                        }
                        if(strcmp(array,"exit") == 0)
                        {
                            break;
                        }
                    }

                    q = head;
                    break;
            case 0:
                    break;
            default:
                    printf("**********输入错误,重新输入!**********\n");
                    break;

        }
        if (choose == 0)
        {
            break;
        }
        getchar();
   }

}

void print(Linker *head)
{
    int i = 0;
    if (head->next == NULL)
    {
        printf("无联系人信息!\n");
    }
    else
    {
        printf("\n\n");
        printf("联系人信息:\n\n");
        while(head->next != NULL)
        {
            printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n"
                ,head->next->num,head->next->name,head->next->telephone,head->next->QQ,head->next->family_address);
            head = head->next;
            i++;
        }
        printf("\n共有%d人\n",i);
    }
}

Status update(Linker *head)
{
    int flag = 0;
    int information;
    Linker *q = head;
    Linker *p = (Linker *) malloc (sizeof(struct link));
    if (p == NULL)
    {
        return F;
    }

    printf("请输入要修改联系人的姓名:  ");
    scanf("%s",p->name);
    while(q->next != NULL)
    {
        if (strcmp(q->next->name,p->name) == 0)
        {
            flag = 1;           
            printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n"
                ,head->next->num,head->next->name,head->next->telephone,head->next->QQ,head->next->family_address);
            
            printf("                **********************************************\n");
            printf("                *   请选择查找方式:                         *\n");
            printf("                *      1:修改姓名       2.修改地址           *\n");
            printf("                *      3:修改号码       4.修改QQ             *\n");
            printf("                *      5:修改学号       0:返回上一级菜单     *\n");  
            printf("                **********************************************\n");


            printf("输入需要修改此联系人的具体信息:");
            scanf("%d",&information);
            switch(information)
            {
                case 1:
                    printf("\n请输入新的联系人的姓名:");
                    scanf("%s",q->next->name);
                    break;

                case 2:
                    printf("\n请输入新的联系人的地址:");
                    scanf("%s",q->next->family_address);
                    break;

                case 3:
                    printf("\n请输入新的联系人的号码:");
                    scanf("%s",q->next->telephone);
                    break;

                case 4:
                    printf("\n请输入新的联系人的QQ:");
                    scanf("%s",q->next->QQ);
                    break;

                case 5:
                    printf("\n请输入新的联系人的学号:");
                    scanf("%s",q->next->num);
                    break;
                case 0:
                    break;

                default:
                    break;
            }
            printf("修改成功!\n");
        }
        q = q->next;
    }

    if (flag == 0)
    {
        printf("查无此人!\n");

        return F;
    }

    free(p);
    return T;
}

Status delete(Linker *head)
{
    int flag = 0;//标志位
    Linker *p = head;
    char name[50];
    printf("请输入要删除联系人的姓名:");
    scanf("%s",name);
    while(p->next != NULL)                              //计算符合删除条件的个数
    {
        if (strcmp(p->next->name,name) == 0)
        {
            flag++;
        }
        p = p->next;
    }
    if (flag == 0)
    {
        printf("查无此人!\n");
    }
    if (flag == 1)
    {
        p = head;
        while(p->next != NULL)
        {
            if (strcmp(p->next->name,name) == 0)
            {
                Linker *temp = p->next;                 //链表的删除指针操作
                p->next = temp->next;
                free(temp);
                printf("删除成功!\n");
            }
            else
            {
                p = p->next;
            }
        }
    }

    if (flag > 1)
    {
        int i;
        p = head;
        while(p->next != NULL)
        {
            if (strcmp(p->next->name,name) == 0)
            {
                printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n"
                    ,p->next->num,p->next->name,p->next->telephone,p->next->QQ,p->next->family_address);
            }
            p = p->next;
        }
        printf("多位联系人符合条件,按1全部删除,按2增加删除条件学号\n");
        scanf("%d",&i);
        
        if(i == 1)
        {
            p = head;
            while(p->next != NULL)
            {
                if (strcmp(p->next->name,name) == 0)
                {
                    printf("学号:%-11s 姓名:%-15s 号码:%-11s QQ:%-10s 地址:%-20s\n"
                    ,p->next->num,p->next->name,p->next->telephone,p->next->QQ,p->next->family_address);
                    
                    Linker *temp = p->next;
                    p->next = temp->next;
                    free(temp);                    
                }
                else
                {
                    p = p->next;
                }
            }            
        }

        if(i == 2)
        {
            
            char num[20];
            printf("\n请添加删除条件学号:\n");
            scanf("%s",num);
            p = head;
            
            while(p->next != NULL)
            {
                if (strcmp(p->next->num,num) == 0 && strcmp(p->next->name,name) == 0)
                {
                    Linker *temp = p->next;
                    p->next = temp->next;
                    free(temp);
                    printf("删除成功!\n");
                    break;
                }
                else
                {
                    p = p->next;
                }
            }
        }

    }

    return T;
}

void clear(Linker **head)
{
    Linker *temp;
    while((*head)->next != NULL)
    {
        temp = (*head)->next;
        (*head)->next = (*head)->next->next;
        free(temp);
    }

    (*head)->next = NULL;

    printf("\n*****************清理完毕!******************\n\n");
}

void sort_show(Linker *head)
{
    printf("                **********************************************\n");
    printf("                *   请选择排序方式:                         *\n");
    printf("                *      1:按学号排序         2:按地址排序    *\n");
    printf("                *      3:按姓名排序         4:按QQ排序      *\n");
    printf("                *      5:按电话号码排序     0:返回上一级菜单 *\n");  
    printf("                **********************************************\n");

    Linker* arr[1024];
    
    Linker* pp = head;
    int i;
    for(i = 0;i < count(head);i++)
    {
        arr[i] = pp->next;
        pp = pp->next;
    }
    
    int array;
    Linker* p;
    Linker *q;
    Linker *head2 = head->next->next; 

    if (count(head) == 0)//只有一个或没有联系人不用排
    {
        printf("没有联系人!\n");
        return;
    }

    if (count(head) == 1)//只有一个或没有联系人不用排
    {
        printf("\n仅有一个联系人\n");
        return;
    }

    printf("请输入想要的排序方式:\n");
    scanf("%d",&array);

    while(array < 0 || array > 5)
    {
        printf("您的输入有误,请重新输入\n");
        scanf("%d",&array);
        if(array >= 0 && array < 6)
        {
            break;
        }
    }

    while(array >= 0 && array < 6)
    {
        switch(array)
        {
            case 1:
                    quicksort(arr,head);
                    break;
            case 2:
                    head->next->next = NULL;

                    while(head2 != NULL)
                    {
                        p = head;
                        q = head2;
                        head2 = head2->next;

                        while(p->next != NULL)
                        {
                            if (strcmp(q->family_address,p->next->family_address) > 0)//以第一次为例,如果不进入if做头插
                            {
                                p = p->next;
                            }
                            else
                                break;
                        }

                        q->next = p->next;
                        p->next = q;
                    }
                    break;
            case 3:
                    bubblena(head);
                    break;
            case 4:
                    bubbleQ(head);
                    break;
            case 5:
                    bubblet(head);
                    break;
            case 0:
                    break;
            default:
                    break;
        }
        break;
    }
}

Status count(Linker* head)
{
    int i = 0;
    while(head->next != NULL)
    {
        head = head->next;
        i++;
    }
    return i;
}

void bubblen(Linker* head)
{
    Linker* p = head;
    int i,j;
    for(i = 0;i < count(head)-1;i++)
    {
        for(j = 0;j < count(head)-1-i;i++)
        {
            if(strcmp(p->next->num,p->next->next->num) > 0)
            {
                swap(p);
            }
            p = p->next;
        }
        p = head;
    }
}

void bubblena(Linker* head)
{
    Linker* p = head;
    int i,j;
    for(i = 0;i < count(head)-1;i++)
    {
        for(j = 0;j < count(head)-1-i;i++)
        {
            if(strcmp(p->next->name,p->next->next->name) > 0)
            {
                swap(p);
            }
            p = p->next;
        }
        p = head;
    }
}

void bubbleQ(Linker* head)
{
    Linker* p = head;
    int i,j;
    for(i = 0;i < count(head)-1;i++)
    {
        for(j = 0;j < count(head)-1-i;i++)
        {
            if(strcmp(p->next->QQ,p->next->next->QQ) > 0)
            {
                swap(p);
            }
            p = p->next;
        }
        p = head;
    }
}

void bubblea(Linker* head)
{
    Linker* p = head;
    int i,j;
    for(i = 0;i < count(head)-1;i++)
    {
        for(j = 0;j < count(head)-1-i;i++)
        {
            if(strcmp(p->next->family_address,p->next->next->family_address) > 0)
            {
                swap(p);
            }
            p = p->next;
        }
        p = head;
    }
}

void bubblet(Linker* head)
{
    Linker* p = head;
    int i,j;
    for(i = 0;i < count(head)-1;i++)
    {
        for(j = 0;j < count(head)-1-i;i++)
        {
            if(strcmp(p->next->telephone,p->next->next->telephone) > 0)
            {
                swap(p);
            }
            p = p->next;
        }
        p = head;
    }
}

void swap(Linker* p)        //交换函数
{
    Linker* temp = p->next->next->next;
    Linker* temp1 = p->next;
    p->next = p->next->next;
    p->next->next = temp1;
    temp1->next = temp;
}

/*void selectsort(Linker* head) //选择排序法(从大到小)
{
    Linker *p,*other,*maxNode;
    Linker* temp;
    if(head == NULL)
        exit(2);  //空链表
 
    for(p = head; p->next != NULL; p = p->next)
    {
        maxNode = p;
        for(other = p->next; other != NULL; other = other->next)
        {
            if(strcmp(other->num,maxNode->num) > 0)
            {
                maxNode = other;
            }
        }
        if(maxNode != p)
        {
            strcpy(temp->num,p->num);
            strcpy(temp->name,p->name);
            strcpy(temp->QQ,p->QQ);
            strcpy(temp->family_address,p->family_address);
            strcpy(temp->telephone,p->telephone);

            strcpy(p->name,maxNode->name);
            strcpy(p->num,maxNode->num);
            strcpy(p->family_address,maxNode->family_address);
            strcpy(p->QQ,maxNode->QQ);
            strcpy(p->telephone,maxNode->telephone);
            
            strcpy(maxNode->num,temp->num);
            strcpy(maxNode->name,temp->name);
            strcpy(maxNode->family_address,temp->family_address);
            strcpy(maxNode->QQ,temp->QQ);
            strcpy(maxNode->telephone,temp->telephone);

        }
    }

}*/

void quicksort(Linker* arr[1024],Linker* head)
{
    int low = 0;
    int high = count(head);
    if(low < high)
    {
        int pivot1 = pivotkey(arr,low,high);
        quicksort(arr,low,pivot1-1);
        quicksort(arr,pivot1+1,high);
    }
}

Status pivotkey(Linker* arr[1024],int low,int high)
{
    Linker pivot = arr[low];
    while(low < high)
    {
        while(low < high && strcmp(arr[low].num,pivot.name) > 0)
        {
            high--;
        }
        arr[low] = arr[high];
        while(low < high && strcmp(arr[low].num,pivot.name) < 0)
        {
            low++;
        }
        arr[high] = arr[low];
    }
    arr[low] = pivot;
    return low;
}

 

你可能感兴趣的:(C语言学习总结--通讯录项目)