c语言基础 通讯录 (链表版)

目录

一.main.c文件

二.tongxunlu.h 文件

三.tongxunlu.c文件 所有功能性函数这

3.1 my_strncmp

3.2 mystrcmp

3.3 创建一个空的链表

3.4 菜单

3.5 switch  框架 调用函数

 3.6 判断链表是否为空

3.7 添加联系人 并按首字母大小排序

 3.8 查看所有联系人

3.9 按照名字或者地址查找练习人

3.10 按照名字删除联系人

3.11 修改联系人信息

3.12 结束整个进程  

三. 注意所有.c文件必需包含tongxulu.h文件


一.main.c文件

#include "tongxunlu.h"


int main(int argc, char const *argv[])
{
    tongxunlist *head = tongxunlistcreate();
        while (1)//这个循环可以实现 功能结束返回菜单
    {
        menu(); //菜单  
        myswitch(head); //这里在tongxunlu.c封装好了也可直接在main.c里写
    }
    return 0;
}

二.tongxunlu.h 文件

#ifndef _TONGXUNLU_h
#define _TONGXUNLU_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef int datatype;

typedef struct tongxunlu
{
    char name[32];//姓名

    datatype age; //年龄

    char adress[32];//住址

    char tel[32];//电话

    struct tongxunlu *next;  //指针域
    
}tongxunlist;

tongxunlist* tongxunlistcreate();
//mymenu 菜单
void menu();

void myswitch();

//mystrncmp  比较字符串首字母大小用
int my_strncmp(const char *str,const char *str1,int n);

//判断链表是否为空
int tongxunempty(tongxunlist *head);

//my_strcmp   比较字符串是否相同 name
int my_strcmp(char *str,char *str1);

//直接插入排序
void linklist_straight_insertion(tongxunlist *head);

//单链表的遍历
void linklistprintf(tongxunlist *head);

//查找联系人
void Find_person(tongxunlist *head);

//删除联系人
void  del_person(tongxunlist *head);

//修改联系人信息
void  tongxunlistdata_data(tongxunlist *head);

//结束进程
void endp();

#endif

三.tongxunlu.c文件 所有功能性函数这

3.1 my_strncmp

//mystrncmp  比较字符串 前n个字符的大小 使用strncmp(目的字符串,源字符串,前n个字符)
int my_strncmp(const char *str,const char *str1,int n)
{
    char *p = (char *)str;
    char *q = (char *)str1;
    while(n--)
    {
        if(*p == *q)
        {
            p++;
            q++;
        }
        else
        {
            return (*p - *q);
        }
    }
    return 0;
}

3.2 mystrcmp

//mystrcmp  比较字符串大小  相等返回值为0
int my_strcmp(char *str,char *str1)
{
    char *p = (char *)str;
    char *q = (char *)str1;
    while(*p!='\0'&&*p==*q)
    {
            p++;
            q++;
    }
    if (*p > *q)
	{
		return 1;
	}
	else if (*p < *q)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

3.3 创建一个空的链表

tongxunlist* tongxunlistcreate()
{
    tongxunlist *head = (tongxunlist *)malloc(sizeof(tongxunlist));
    head->next = NULL;
    return head;
}

3.4 菜单

//mymenu 菜单
void menu()
{   
    printf("**************欢迎使用通讯录*****************\n");
    printf("*********************************************\n");
    printf("**  1.添加联系人              2.查看联系人 **\n");
    printf("**  3.搜索联系人              4.删除联系人 **\n");
    printf("**  5.修改用户信息            6.退出通讯录 **\n");
    printf("*********************************************\n");
    printf("*********************************************\n");

}

3.5 switch  框架 调用函数

void myswitch(tongxunlist *head)
{   
    int num;//定义整形 数字
    scanf("%d",&num); //输入数字 调用对应功能
    getchar();  //吞噬垃圾字符 防止出现错误
    while(1) //此处用循环可以为下面连续使用对应功能 
    {   
        switch (num)
        {             
            case 1:
            printf("正在添加联系人.....\n");
            putchar(10);//没什么作用为了让终端显示的不那么拥挤
            linklist_straight_insertion(head);//添加联系人函数调用
            break;
            case 2:
                printf("正在查看联系人.....\n");
                putchar(10);
                linklistprintf(head);//查看联系人函数调用
                break;
            case 3:
                printf("正在搜索联系人.....\n");
                Find_person(head);//搜索联系人函数调用
                putchar(10);
                break;
            case 4:
                printf("正在删除联系人.....\n");
                del_person(head);//删除联系人函数调用
                putchar(10);
                break;
            case 5:
                printf("正在修改联系人.....\n");
                putchar(10);
                tongxunlistdata_data(head);//修改联系人函数调用
                break;
            case 6:
                printf("正在退出通讯录.....\n");
                endp();//退出函数调用
                break;      
            default:
                printf("对不起您输入有误,请重新输入\n");
                return;
        }
        break;  
    }
}

 3.6 判断链表是否为空

int tongxunempty(tongxunlist *head)
{
    if (head->next==NULL)
    {
    return  1 ;
    }
    else 
    {
    return 0;
    }
}

3.7 添加联系人 并按首字母大小排序

//添加联系人
void linklist_straight_insertion(tongxunlist *head)
{
    tongxunlist *tmp=(tongxunlist *)malloc(sizeof(tongxunlist));
    tmp->next=NULL;

    printf("请输入姓名:\n");//字符串定义为一个数组  代表的就是字符串首地址
    scanf("%s",tmp->name); 

    printf("请输入年龄:\n");//年龄整形需要取地址
    scanf("%d",&tmp->age);
    
    printf("请输入住址:\n");
    scanf("%s",tmp->adress);

    printf("请输入电话号码:\n");

    scanf("%s",tmp->tel);
    putchar(10);

    
    tongxunlist *p =head;//定义一个指针为表头

    if(1==tongxunempty(head))
    {
        tmp->next=head->next;//令新插入的结点指针指向下一个结点
    //tmp 新插入结点的地址
        head->next=tmp;
    }
    else
    {
        while(p->next!=NULL&&my_strncmp(p->next->name , tmp->name,1)<0)//当目的name小于源name
        {
            p=p->next;
        }
        //当p->next指向的数据大于或者等于需要插入的数据是跳出循环
        //此事p->next  向的是第一个大于value的值
        tmp->next=p->next;
        p->next=tmp;  //然后将其插入大于value的值之前
        printf("*****联系人添加完成*****\n");
        putchar(10);
    }
    int num = 0;  
    printf("*请选择是否继续添加联系人*\n");

    printf("*1.继续添加*    2.返回菜单*\n");
    scanf("%d",&num);
    getchar();
    if(num == 1)
    {
        linklist_straight_insertion(head);//继续添加就在此调用函数
    }  
    else
    {
        return;       //不添加就结束 把程序流程从被调函数转向主调函数    main.c在此循环进入菜单              
    } 
}

 3.8 查看所有联系人

//查看所有联系人
void linklistprintf(tongxunlist *head)
{
    //定义一个指针  遍历保存第一个有数据的结点
    tongxunlist *p = head->next;
    if(1==tongxunempty(head))
    {
        printf("该通讯录为空请添加联系人\n");
        printf("请进行添加联系人\n");
        return;
    }
    //循环遍历单链表,直到指针指为空结束循环 因为最后一个结点的指针域指向NULL
    while(p->next !=NULL)
    {
        printf("╔════════════════════════════════════════════════════════╗\n");
        printf("*姓名%s__年龄:%d___住址:%s__电话%s*\n",p->name,p->age,p->adress,p->tel);
        printf("╚════════════════════════════════════════════════════════╝\n");
    //p循环中指向下一个结点 
        p=p->next;
    }
        printf("╔════════════════════════════════════════════════════════╗\n");
        printf("*姓名%s__年龄:%d___住址:%s__电话%s*\n",p->name,p->age,p->adress,p->tel);
        printf("╚════════════════════════════════════════════════════════╝\n");
    putchar(10);
}

3.9 按照名字或者地址查找练习人

//查找联系人

void Find_person(tongxunlist *head)
{
    if(1==tongxunempty(head))
    {
        printf("该通讯录为空请添加联系人\n");
        printf("请进行添加联系人\n");
        return;
    }
    char s1[32]={0};//定义一个字符串数组 用于保存你输入想要查找联系人的名字
        printf("********************************\n");
        printf("请输入您所查找联系人的姓名或者住址\n");
        printf("********************************\n");
        int i;
        scanf("%s",s1);
        tongxunlist *p =head;
        while(p!=NULL)//如果用p->next!=NULL  当p在最后一个结点是 p->next=NULL 无法进入循环
        {   
            if(my_strcmp(s1,p->name) == 0 ) 
            {
                printf("╔═══════════════════════════════════════════════════════════╗\n");
                printf("* 姓名%s__年龄:%d___住址:%s__电话%s *\n",p->name,p->age,p->adress,p->tel);
                printf("╚═══════════════════════════════════════════════════════════╝\n");
                break;
            }
            else if(my_strcmp(s1,p->adress) == 0 )  //这个是按地址查询 道理与上面相同
            {
                printf("╔═══════════════════════════════════════════════════════════╗\n");
                printf("*姓名%s__年龄:%d___住址:%s__电话%s*\n",p->name,p->age,p->adress,p->tel);
                printf("╚═══════════════════════════════════════════════════════════╝\n");
                break;
            }
            p=p->next;
        }
            if(p==NULL)
            {
                printf("**查无此人**\n");
                putchar(10);
            }
            printf("请问是否要继续查询联系人\n");
            printf("**1.继续查找**      **2.返回菜单**\n");
            putchar(10);
            int num = 0;
            getchar();//吞噬垃圾字符
            scanf("%d",&num);
            if(num==1)
            {
                Find_person(head);//强行执行下次循环继续查找
            }
            else 
            {
                return;  //如果输入不为1 退出  进入主函数  在次循环进出菜单
            }
    
}

3.10 按照名字删除联系人

//删除联系人
void  del_person(tongxunlist *head)
{
    if(1==tongxunempty(head))
    {
        printf("该通讯录为空请添加联系人\n");
        printf("请进行添加联系人\n");
        return;
    }
    printf("**请输入你想删除联系人的名字**\n");
    char s2[32] = {0};
    scanf("%s",s2);
    tongxunlist *p = head;

    tongxunlist *q;

    while(p->next!=NULL)
    {
        if(my_strcmp(s2,p->next->name)==0)
        {
            q=p->next;
            p->next=p->next->next;
            free(q);
            q=NULL;
            printf("删除成功\n");
            return;
        }
        p=p->next;
    }
}

3.11 修改联系人信息

//修改联系人信息
void  tongxunlistdata_data(tongxunlist *head)
{
    if(1==tongxunempty(head))
    {
        printf("该通讯录为空请添加联系人\n");
        printf("请进行添加联系人\n");
        return;
    }
    
    printf("**请输入您所要修改信息的人名**\n");
    char s3[32] = {0};
    scanf("%s",s3);
    tongxunlist *p=head;//定义一个指针指向头结点
    p=p->next;//头结点无数据  直接跳过
    while(p!=NULL)
    {
        if(my_strcmp(s3,p->name) == 0 ) 
        {
            
            printf("请输入新的姓名\n");//下面直接进行覆盖就行了
            scanf("%s",p->name);    //把新的信息覆盖旧的信息

            printf("请输入新的年龄\n");
            scanf("%d",&p->age);

            printf("请输入新的住址\n");
            scanf("%s",p->adress);

            printf("请输入新的电话\n");
            scanf("%s",p->tel);

            printf("修改成功\n");
            return;  //修改完  返回主函数 循环进入菜单
        }
        p=p->next;
    }
    if(p==NULL)
    {
        printf("**不存在该联系人**\n");
        putchar(10);
    }

}

3.12 结束整个进程  

//结束整个进程
void endp()

{
    exit(0);
}

三. 注意所有.c文件必需包含tongxulu.h文件

  • 7
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是个小趴菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值