单链表--通讯录 代码

2021.1.13

简易通讯录 代码 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct person
{
    char tele[20];
    char name[20];
    struct person *next;
}   PN;

void addperson(PN**);
void findperson(PN*);
void changeperson(PN**);
void delperson(PN**);
void display(PN*);
void quit(PN*);
void getiniput(PN*);
int main(void)
{
    PN* library=NULL;
    int mode;
    printf("选择模式\n");
    printf("\t1为添加,2为查找,3为更改,4为删除,0为展示,-1为退出:");
    scanf("%d",&mode);
    while(mode<-1||mode>4)
    {
        printf("选择模式\n");
        printf("\t1为添加,2为查找,3为更改,4为删除,0为展示,-1为退出:");
        scanf("%d",&mode);
    }
    while(mode!=-1)
    {
        if(mode==0)
        {
            display(library);
        }
        if(mode==1)
        {
            addperson(&library);

        }
        if(mode==2)
        {
            findperson(library);
        }
        if(mode==3)
        {
            changeperson(&library);
        }
        if(mode==4)
        {
            delperson(&library);
        }
        printf("1为添加,2为查找,3为更改,4为删除,0为展示,-1为退出:");
        scanf("%d",&mode);
        while(mode<-1||mode>4)
        {
            printf("1为添加,2为查找,3为更改,4为删除,0为展示,-1为退出:");
            scanf("%d",&mode);
        }
    }
    quit(library);
}
void getinput(PN*person)
{
    printf("请输入名字:");
    scanf("%s",person->name);
    printf("请输入电话号码:");
    scanf("%s",person->tele) ;

}
void delperson(PN**library)
{
    PN*previous=NULL,*current;
    char person[20];
    current=*library;
    printf("请输入名字:");
    scanf("%s",person);
    while(current!=NULL&&strcmp(person,current->name))
    {
        previous=current;
        current=current->next;
    }
    if(previous==NULL)
    {
        *library=current->next;free(current);
    }
    else if(current==NULL)
    {
        printf("\nthere is no the person\n");

    }
    else
    {
        previous->next=current->next;free(current);
    }
    

}

void changeperson(PN**library)
{
    PN *end;
    char person[20];
    end=*library;
    printf("请输入名字:");
    scanf("%s",person);
    while(end!=NULL&&strcmp(person,end->name))
    {
        end=end->next;
    }
    printf("\n名字:%s电话号码:%s\n",end->name,end->tele);
    getinput(end);
    printf("修改成功:名字:%s\n电话号码:%s\n",end->name,end->tele);

}

void findperson(PN*library)
{
    PN* end;
    char person[20];
    end=library;
    printf("请输入名字:");
    scanf("%s",person);
    while(end!=NULL&&strcmp(person,end->name))
    {
        end=end->next;
    }
    printf("\n名字:%s\n电话号码:%s\n\n",end->name,end->tele);

扫描二维码关注公众号,回复: 12899129 查看本文章

}


void addperson(PN**library)
{
    PN* person,*temp;
    person=(PN*)malloc(sizeof(PN));
    getinput(person);
    if(*library==NULL)
    {
        *library=person;
        person->next=NULL;
    }
    else
    {
        temp=*library;
        *library=person;
        person->next=temp;
    }

}

void  quit(PN*library)
{
    PN*temp;
    while(library!=NULL)
    {
        temp=library;
        library=library->next;
        free(temp);
    }
    printf("delect over\n");
}

void display(PN*library)
{
    while(library!=NULL)
    {
        printf("\n名字:%s\n电话号码:%s\n",library->name,library->tele);
        library=library->next;
    }
    printf("display over\n\n");
}

将这几天学习的基础单链表知识进行整合的一个练习。


 

猜你喜欢

转载自blog.csdn.net/m0_52521883/article/details/112590720
今日推荐