通过链表实现的简易通讯录

学习内容:链表通讯录

#ifndef _LINKLIST_H
#define _LINKLIST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1000

#define FAILURE    10000
#define SUCCESS    10001
#define TRUE       10002
#define FALSE      10003

struct node
{
    char name[100];
    int age;
    struct node *next;
};
typedef struct node Node;

#endif

int LinkInit(Node **l)      //初始化
{
    *l=(Node *)malloc(sizeof(Node)*1);
    if(NULL == *l)
    {
        return FAILURE;
    }
    (*l)->next=NULL;
    return SUCCESS;
}

int LinkInsert(Node *l)
{
    Node *p=l;
    char flag = 'y';
    if(NULL == l)
    {
        return FAILURE;
    }
    while(flag == 'y')
    {
        Node *q=(Node *)malloc(sizeof(Node)*1);  //要插入的
        printf("Please input name and age:");
        scanf("%s%d",q->name,&q->age);

        q->next=NULL;
        p->next=q;
        p=q;
        getchar();
        printf("finish?(y/n)");
        scanf("%c",&flag);
    }

}

int LinkShow(Node *l)
{
    if(NULL == l)
    {
        return FAILURE;
    }
    Node *q = l;
    while(q->next)
    {
        q=q->next;
        printf("name:%s age:%d\n",q->name,q->age);
    }
    getchar();
    getchar();
    return SUCCESS;
}


int LinkFind(Node *l)
{
    if(NULL == l )
    {
        return FAILURE;
    }
    char *p;
    p =(char *)malloc(sizeof(char)*32);
    printf("Please input the name you want to find:");
    scanf("%s",p);
    Node *q = l;
    int i;
    while(q != NULL)
    {
        if(strcmp(q->name,p) == 0)
        {
            printf("name:%s age:%d",q->name,q->age);
            break;
        }
        else
        {
            q=q->next;
        }
    }
    if(!q)
    {
        return FAILURE;
    }
    return SUCCESS;
}


int LinkDelete(Node *l)
{
    if(NULL == l)
    {
        return FAILURE;
    }
    Node *q = l;
    Node *m = l;
    Node *t;
    int n=0;
    int k=1;
    char *p;
    p = (char*)malloc(sizeof(char)*32);
    printf("Please input the name you want to delete:");
    scanf("%s",p);
    while(l->next != NULL)
    {
        q = q->next;
        n++;
        if(strcmp(q->name,p) == 0)
        {
            t = q;
            break;
        }
    }
    while (k < n && m != NULL)
    {
        m = m->next;
        k++;
    }
    m->next=t->next;
    free(p);
}


int LinkCorrect(Node *l)
{
    if(NULL == l )
    {
        return FAILURE;
    }

    char *p;
    p =(char *)malloc(sizeof(char)*32);
    printf("Please input the name you want to change:");
    scanf("%s",p);
    Node *q = l;
    while(q != NULL)
    {
        if(strcmp(q->name,p) == 0)
        {
            printf("Please input the name after change:");
            scanf("%s",q->name);
            printf("Please input the age after change:");
            scanf("%d",&q->age);
            break;
        }
        else
        {
            q=q->next;
        }
    }
    if(!q)
    {
        return FAILURE;
    }
    return SUCCESS;
}





void welcome()
{
    printf("\t\t\033[46m*************************\n");
    printf("\t\t*********Welcome!********\n");
    printf("\t\t*************************\n");
}
void menu()
{
    system("clear");
    printf("\n");
    printf("\t\t************************\n");
    printf("\t\t1.添加            2.查看 \n");
    printf("\t\t3.查找            4.删除 \n");
    printf("\t\t5.修改            6.返回 \n");
    printf("\t\t************************\n");
}


int main()
{
    Node *first = NULL;
    int choice;
    welcome();
    LinkInit(&first);
    while(1)
    {
        menu();
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:LinkInsert(first);
                   break;
            case 2:LinkShow(first);
                   break;
            case 3:LinkFind(first);
                   break;
            case 4:LinkDelete(first);
                   break;
            case 5:LinkCorrect(first);
                   break;
            case 6:exit(0);
                   break;

        }
    }
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值