码迷,mamicode.com
首页 > 编程语言 > 详细

C语言学习之用链表实现通讯录

时间:2015-02-05 09:30:23      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:c语言   通讯录   链表   

本程序主要功能是对联系人信息进行,添加、删除、查找、插入、显示功能

说明:调用的链表操作接口请参考:http://blog.csdn.net/qlx846852708/article/details/43482497

这里面有我实现的链表操作的接口的详细实现过程,并进行过测试的哦!!!



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "addressBookList.h"


/* 显示链表所有信息*/
void chainlist_all(chainListType *head)
{
    chainListType *phead;
    DATATYPE_T data;
    phead = head;

    while(phead)
    {
        data = phead->data;
        printf("name:%s,address:%s,telephone:%s\n",data.key,data.add,data.telephone);
        phead = phead->next;
    }
    return;
}

/*添加联系人*/
chainListType *add_contact(chainListType *head)
{
    DATATYPE_T  contactInfo;

    printf("please input contact information\n");
    scanf("%s %s %s",contactInfo.key,contactInfo.add,contactInfo.telephone);
   
    return chainlist_add_end(head,contactInfo);
}

/*按照关键字查找联系人*/
int find_contact(chainListType *head)
{
    char key[15];
    chainListType *node = NULL;
    printf("please input find key\n");
    scanf("%s",key);
    node = chainlist_find(head,key);
    if(node!=NULL)
    {
        printf("find info name:%s,address:%s,telephone:%s\n",node->data.key,node->data.add,node->data.telephone);
    }
    else
    {
        printf("the key can‘t find!!!\n");
    }
    return 0;
}

/*按照关键字删除联系人*/
chainListType *delete_contact(chainListType *head)
{
    char key[15];
    chainListType *phead = NULL;
    printf("please input delete key\n");
    scanf("%s",key);
    phead = chainlist_delete(head,key);
    if(phead == NULL)
    {
        printf("delete after the list is NULL!!!\n");
        return NULL;
    }
    return phead;
}

/*插入联系人信息*/
chainListType *insert_contact(chainListType *head)
{
    char key[15];
    DATATYPE_T insertData;
    chainListType *phead = NULL;
    printf("please input insert key\n");
    scanf("%s",key);
    printf("please input insert contact information\n");
    scanf("%s %s %s",insertData.key,insertData.add,insertData.telephone);
   
    phead = chainlist_insert(head,key,insertData);
    return phead;
}

/*显示所有联系人信息*/
int show_contact(chainListType *head)
{
    if(head==NULL)
    {
        printf("the list is NULL\n");
        return -1;
    }
   
    chainlist_all(head);
    return 0;
}

int menu()
{
    printf("********************\n");
    printf("1.add a contact\n");
    printf("2.find a contact\n");
    printf("3.delete a contact\n");
    printf("4.insert a contact\n");
    printf("5.show a contact\n");
    printf("0.quit ");
    printf("\n");
    printf("********************\n");
}

int main()
{
    int opt = 0;
    chainListType *head=NULL;
    do
    {
        printf("\n");
        printf("please select option!\n");
        menu();
        scanf("%d",&opt);
        printf("you select for %d\n",opt);

        switch(opt)
        {
            case 1:
                head = add_contact(head);
                break;
            case 2:
                find_contact(head);
                break;       
            case 3:
                head = delete_contact(head);
                break;
            case 4:
                head = insert_contact(head);
                break;
            case 5:
                show_contact(head);
                break;
            case 0:
                return 0;
            default:
                printf("unknow select\n");
                break;
        }
    }while(opt!=0);
   
    return 0;
}




C语言学习之用链表实现通讯录

标签:c语言   通讯录   链表   

原文地址:http://blog.csdn.net/qlx846852708/article/details/43523463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!