手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2015-02-09] 来源:Linux社区  作者:qlx846852708 [字体: ]

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

说明:调用的链表操作接口请参考:http://www.linuxidc.com/Linux/2015-02/113142.htm

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

#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++ 隐式类类型转化 Implicit Class-Type Conversions http://www.linuxidc.com/Linux/2013-01/78071.htm

C语言变长数组之剖析 http://www.linuxidc.com/Linux/2013-07/86997.htm

C语言需要注意的问题 http://www.linuxidc.com/Linux/2013-05/84301.htm

C语言位域的使用及其注意点 http://www.linuxidc.com/Linux/2013-07/87027.htm

C语言中简单的for循环和浮点型变量 http://www.linuxidc.com/Linux/2013-08/88514.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-02/113143.htm

linux
相关资讯       C语言学习  C语言链表 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评���即表明您已经阅读并接受上述条款