| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 380 人关注过本帖
标题:个人电话本
只看楼主 加入收藏
black1234
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-6-17
结帖率:100%
收藏
 问题点数:0 回复次数:2 
个人电话本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum
{
    TypeFind,
    TypeAdd,
    TypeUpdate_Name,
    TypeUpdate_Tel,
    TypeDelete,
};

struct telbook
{
    char name[20];
    int tel;
    struct telbook *next;
};
typedef struct telbook Tel,*pTel;

void show(pTel head)
{
    while(head)
    {
        printf("%s: %d\n",head->name,head->tel);
        head=head->next;   
    }
}
void empty(pTel *head)
{
    pTel pt;
    while(*head)
    {
        pt=(*head)->next;
        free(*head);
        *head=pt;
    }
}
void input(char *name,int *tel,int type)
{
    switch(type)
    {
        case TypeAdd:
            printf("[Add] input telbook name: ");
            break;
        case TypeFind:
            printf("[Find] input telbook name: ");
            break;
        case TypeUpdate_Name:
            printf("[Update] input telbook name: ");
            break;
        case TypeUpdate_Tel:
            printf("[Update] input telbook tel: ");
            break;
        case TypeDelete:
            printf("[Delete] input telbook name: ");
            break;
        default:
            break;
    }
    if(name!=NULL)
    {
        scanf("%s",name);
    }
    if(tel!=NULL)
    {
        if(type!=TypeFind )
        {
            scanf("%d",tel);
        }
    }
}
void find(pTel head,char *name)
{
    input(name,NULL,TypeFind);
    while(head)
    {
        if(strcmp(head->name,name)==0)
        {
            printf("%s: %d\n",name,head->tel);
            return;
        }
        head=head->next;
    }
    printf("error,no %s tel record!\n",name);
}
void update(pTel head,char *name,int *tel)
{
    input(name,NULL,TypeUpdate_Name);
    while(head)
    {
        if(strcmp(head->name,name)==0)
        {
            input(NULL,tel,TypeUpdate_Tel);
            head->tel=*tel;
            return;
        }
        head=head->next;
    }
    printf("error,no %s tel record!\n",name);
}
void delete(pTel *head,char *name)
{
    input(name,NULL,TypeDelete);
    pTel p=*head;
    pTel q=NULL;
    int flag=0;
    while(p)
    {
        if(strcmp(p->name,name)==0)
        {
            if(p==*head)
            {
                *head=p->next;
            }
            else
            {
                q->next=p->next;
            }
            free(p);
            flag=1;
        }
        q=p;
        p=p->next;
    }
    if(flag==0)
        printf("error,no this %s tel record!\n",name);
}


void sort(pTel head)
{
    Tel st=*head;;
    pTel p,q;
    p=head;
    while(p)
    {
        q=p->next;
        while(q)
        {
            if(strcmp(p->name,q->name)>0)
            {
                st=*p;
                strcpy(p->name,p->next->name);
                p->tel=q->tel;
                strcpy(q->name,st.name);
                q->tel=st.tel;
            }
            q=q->next;
        }
        p=p->next;
    }
   
   
}


//头插法
void push_front(pTel *head,char *name,int *tel)
{
    pTel pt=NULL;
    input(name,tel,TypeAdd);
    pt=malloc(sizeof(Tel));
    strcpy(pt->name,name);
    pt->tel=*tel;
    pt->next=*head;
    *head=pt;
}

void push_back(pTel *head,char *name,int *tel)
{
    pTel q=*head;
    pTel pt=NULL;
    input(name,tel,TypeAdd);
    pt=malloc(sizeof(Tel));
    strcpy(pt->name,name);
    pt->tel=*tel;
    pt->next=NULL;
    if(*head==NULL)
    {
        *head=pt;
    }
    else
    {
        while(q->next)q=q->next;
        q->next=pt;
    }
    return;
}

int main(int argc,char *argv[])
{
    pTel head=NULL;
    char name[20];
    int tel;
    push_front(&head,name,&tel);
    push_front(&head,name,&tel);
    push_back(&head,name,&tel);
    push_back(&head,name,&tel);
    show(head);
    find(head,name);
    update(head,name,&tel);
    show(head);
    delete(&head,name);
    show(head);
    sort(head);
    //empty(&head);
    //printf("******empty!...\n");
    printf("after sort...\n");
    show(head);
    return 0;




    /*
    pTel pt=NULL;
    pt=malloc(sizeof(Tel));
    strcpy(pt->name,"zhangshan");
    pt->tel=1111;
    pt->next=NULL;
    head=pt;

    pt=malloc(sizeof(Tel));
    strcpy(pt->name,"lisi");
    pt->tel=2222;
    pt->next=NULL;
    head->next=pt;   

    pt=malloc(sizeof(Tel));
    strcpy(pt->name,"wangwu");
    pt->tel=3333;
    pt->next=NULL;
    head->next->next=pt;
    */
}
搜索更多相关主题的帖子: include 电话本 
2014-06-23 11:06
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
个人电话本

DO IT YOURSELF !
2014-06-23 13:56
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:0 
这是要干嘛?

我不是砖家,要努力成为砖家。
2014-06-23 14:18
快速回复:个人电话本
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017337 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved