栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言通讯录版本一:使用链表完成

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言通讯录版本一:使用链表完成

myhead.h

#ifndef MYHEAD_H_
#define MYHEAD_H_

#include 
#include 
#include 

#define SUCCESS 1
#define FAILURE 0

struct hnode
{
    char name[20];
    char  age[20];
    char number[20];
    char telephone[20];
    struct hnode * next;
};

typedef struct hnode Hnode;
typedef Hnode * Hlink;

//enum reslt_val{RET_OK =1,RET_NO};

int is_malloc_ok( Hlink new_node);
int create_node( Hlink*  new_node);
void create_link( Hlink * head);
int add(Hlink head);
int delete(Hlink head);
int see(Hlink head);
int find(Hlink head);
int insert_node(Hlink head);
int change_node(Hlink head);
int signout(Hlink * head);
int length(Hlink head);

void menu();  
void welcome();
#endif



 

function.c

#include "../../include/myhead.h"

void welcome()
{
    printf("*********************************************************************n");
    printf("*******************                               *******************n");
    printf("*******************          Welocome             *******************n");
    printf("*******************                               *******************n");
    printf("*********************************************************************n");
    printf("nnn");
    
}

void menu()
{
    printf("***************     welcome to use  *******************n");  
    printf("**1 添加通讯录成员                   2 显示通讯录成员**n");

	printf("**3 删除通讯录成员                   4 查询通讯录成员**n");

    printf("**5 插入通讯录成员                   6 修改通讯录成员**n");
    printf("*****************    7 退出系统      ******************n");
	printf("*******************************************************n");
}

int is_malloc_ok(Hlink new_node)
{
    if(new_node == NULL)
    {
        return FAILURE;
    }
    else
    {
        return SUCCESS;
    }
}

int create_node(Hlink * new_node)
{
    *new_node = (Hlink)malloc((unsigned)sizeof(Hnode));
    if(FAILURE == is_malloc_ok(*new_node))
    {
        return FAILURE;
    }
    else
    {
        return SUCCESS;
    }
}

void create_link(Hlink * head)
{
    Hlink new_node = NULL;

    new_node = (Hlink)malloc(sizeof(Hnode));

    if(NULL == new_node)
    {
        printf("malloc errorn");
    }

    new_node->next = NULL;
    (*head) = new_node;
}

int add(Hlink head)
{

    Hlink new_node;
    
    while (1)
    {
        new_node = (Hlink)malloc(sizeof( Hnode));

        if (NULL == new_node)
        {
            return FAILURE;
        }

        printf("please input the namen");
        getchar();
        scanf("%s", new_node->name);

        printf("please input the agen");
        getchar();
        scanf("%s", new_node->age);

        printf("please input the numbern");
        getchar();
        scanf("%s", new_node->number);

        printf("please input the telephonen");
        getchar();
        scanf("%s", new_node->telephone);
        
        new_node->next = NULL;

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

        head->next = new_node;
        
        
        break;
    }
    return SUCCESS;

}

int delete(Hlink head)
{
    char name[20];
    Hlink temp;

    printf("please input the name you want to deleten");
    getchar();
    scanf("%s", name);

    while (head != NULL)
    {
        if (strcmp(head->next->name, name) == 0)
        {
            temp = head->next;
            head->next = head->next->next;
            free(temp); return SUCCESS;
        }
        else
        {
            head = head->next;
        }
 
    }
    head->next = NULL;
    return FAILURE;


}

int see(Hlink head)
{
    int count = 0;
    //printf("1111111");

    while (head->next != NULL)
    {
        count++;
        printf("第%d位:t", count);
        printf("姓名:%st",head->next->name);
        printf("年龄:%st",head->next->age);
        printf("学号:%st",head->next->number);
        printf("电话:%sn",head->next->telephone);
        head = head->next;
        
    }

    while(head->next == NULL)
    {
        return FAILURE;
    }
    
    return SUCCESS;
    
    
    

}

int find(Hlink head)
{
    char name[20];
    int count = 0;

    printf("please input the name you want to findn");
    scanf("%s", name);

    while (head->next != NULL)
    {
        if (strcmp(head->next->name, name) == 0)
        {
            count++;
            printf("名字:%st", head->next->name);
            printf("年龄:%st", head->next->age);
            printf("学号:%st", head->next->number);
            printf("电话:%sn", head->next->telephone);
        }

        head = head->next;
    }

    if (count == 0)
    {
        return FAILURE;
    }

 return SUCCESS;
}


int insert_node(Hlink head)
{
    
    char name[20];
    Hlink new_node;
    Hlink p = NULL;
    Hlink q = NULL;

    p = head->next;
    q = head;

    printf("Please input the name you want to insert frontn");
    getchar();
    scanf("%s",name);

    new_node = (Hlink)malloc(sizeof(Hnode));

    if (NULL == new_node)
    {
        return FAILURE;
    }

    while(p != NULL && strcmp(head->next->name, name) != 0)
    {
        q = p;
        p = p->next;
    }

    printf("please input the namen");
    getchar();
    scanf("%s", new_node->name);

    printf("please input the agen");
    getchar();
    scanf("%s", new_node->age);

    printf("please input the numbern");
    getchar();
    scanf("%s", new_node->number);

    printf("please input the telephonen");
    getchar();
    scanf("%s", new_node->telephone);
    if(p == NULL)
    {
        new_node->next = p;
        q->next = new_node;
    }
    else
    {
    
    
    q->next = new_node;
    new_node->next = p;
    }
    if(p == NULL)
    {
        return FAILURE;
    }
    return SUCCESS;
}

int change_node(Hlink head)
{
    char name[20];
    Hlink new_node;
    new_node = (Hlink)malloc(sizeof( Hnode));

    if (NULL == new_node)
    {
        return FAILURE;
    }

    printf("please input the name you want to findn");
    scanf("%s", name);
    while (head->next != NULL)
    {
        if (strcmp(head->next->name, name) == 0)
        {
            printf("please input the namen");
            getchar();
            scanf("%s", new_node->name);

            printf("please input the agen");
            getchar();
            scanf("%s", new_node->age);

            printf("please input the numbern");
            getchar();
            scanf("%s", new_node->number);

            printf("please input the telephonen");
            getchar();
            scanf("%s", new_node->telephone);
         
            new_node->next = head->next->next;

            head->next = new_node;break;

        }

       head = head->next;
         
    }

    if(head->next == NULL)
    {
        return FAILURE;
    }
    return SUCCESS;
}

int signout(Hlink * head)
{
    Hlink p;
    p = *head;
    while(*head != NULL)
    {
        p = *head;
        *head = (*head)->next;
        free(p);
    }
    *head = NULL;
    return SUCCESS;
}

main.c

#include "../../include/myhead.h"



int main()
{
    int num;
    int result ;
    
    Hlink   head = NULL;
  

    create_link( &head);
    welcome();
    while (1)
    {
        
        menu();
        printf("Please select an actionn");
        scanf("%d",&num);
        

        switch(num)
        {
            case 1:
                    result = add(head);
				    if (result == SUCCESS )
				    {
					    printf("add success!n");
				    }
				    else
				    {
					    printf("add failure!n");
				    }
                    break;
            
            case 2:
                    result = see(head);
				    if (result == SUCCESS )
				    {
					    printf(" success!n");
				    }
				    else
				    {
					    printf("Address book is empty !n");
				    }
                    break;

            case 3:

                    result = delete(head);
				    if (result == SUCCESS )
				    {
					    printf("deldete success!n");
				    }
				    else
				    {
					    printf("delete failure!n");
				    }
                    break;

            case 4:
                    result = find(head);
				    if (result == SUCCESS )
				    {
					    printf("find success!n");
				    }
				    else
				    {
					    printf("no find!n");
				    }
                    break;

            case 5:
                    result = insert_node(head);
				    if (result == SUCCESS )
				    {
					    printf("insert success!n");
				    }
				    else
				    {
					    printf("insert failure!n");
				    }
                    break;

                case 6:
                    result = change_node( head);
				    if (result == SUCCESS )
				    {
					    printf("insert success!n");
				    }
				    else
				    {
					    printf("insert failure!n");
				    }
                    break;

            case 7:
                    result = signout(&head);
				    if (result == SUCCESS )
				    {
					    printf("Exit successful!n");
                        exit(1);
				    }
				      
        }
                
    }
	return 0;
    
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/297524.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号