• 哈希表查找


    //有个例子,参考一下;
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>


    #define N 30
    #define L 38
    #define P 37


    typedef struct
    {
        char *name;
        char *py;
        int k;
    }old;
    old oldlist[N];

    typedef struct
    {
        char *name;
        char *py;
        int k; 
        int b; 
    }hx;
    hx hlist[L];

    void InputOldlist()

        int i,s=0,r;
        /*for(i=1;i<=N;i++){
            oldlist[i-1].name=(char*)malloc(sizeof(char)*20);
            printf("请输入人名:");
            gets(oldlist[i-1].name);
            oldlist[i-1].py=(char*)malloc(sizeof(char)*20);
            printf("请输入拼音:");
            gets(oldlist[i-1].py);
        }*/
        oldlist[0].name="栾雪峰";    oldlist[0].py="luan xue feng";
        oldlist[1].name="刘翔";        oldlist[1].py="liu xiang";
        oldlist[2].name="马三立";    oldlist[2].py="ma san li";
        oldlist[3].name="李开复";        oldlist[3].py="li kai fu";
        oldlist[4].name="李彦宏";    oldlist[4].py="li yan hong";
        oldlist[5].name="王冰杰";    oldlist[5].py="wang bin jie";
        oldlist[6].name="孟茂昌";    oldlist[6].py="meng mao chang";
        oldlist[7].name="蒋睿杰";    oldlist[7].py="jiang rui jie";
        oldlist[8].name="嫦娥";    oldlist[8].py="chang er";
        oldlist[9].name="孙悟空";    oldlist[9].py="sun wu kong";
        oldlist[10].name="猪八戒";    oldlist[10].py="zhu ba jie";
        oldlist[11].name="徐琛";    oldlist[11].py="xu chen";
        oldlist[12].name="唐僧";    oldlist[12].py="tang seng";
        oldlist[13].name="周星驰";    oldlist[13].py="zhou xing chi";
        oldlist[14].name="董寸瑞";    oldlist[14].py="dong cun rui";
        oldlist[15].name="黄继光";    oldlist[15].py="huang ji guang";
        oldlist[16].name="金庸";    oldlist[16].py="jin rong";
        oldlist[17].name="杨过";    oldlist[17].py="yang guo";
        oldlist[18].name="小龙女";    oldlist[18].py="xiao long nv";
        oldlist[19].name="郭大侠";    oldlist[19].py="guo da xia";
        oldlist[20].name="黄蓉";    oldlist[20].py="huang rong";
        oldlist[21].name="老师";    oldlist[21].py="lao shi";
        oldlist[22].name="学生";    oldlist[22].py="xue sheng";
        oldlist[23].name="小平同志";    oldlist[23].py="xiao ping tong zhi";
        oldlist[24].name="泽民";    oldlist[24].py="ze min";
        oldlist[25].name="圣诞老人";    oldlist[25].py="sheng dan lao ren";
        oldlist[26].name="朱德庸";    oldlist[26].py="zhu de yong";
        oldlist[27].name="阿扁";    oldlist[27].py="a bian";
        oldlist[28].name="连战";    oldlist[28].py="lian zhan";
        oldlist[29].name="幸福";    oldlist[29].py="xing fu";
        
        for (i=0;i<N;i++)
        {
           
            for(s=0,r=0;oldlist[i].py[r]!='\0';r++)
            {   
           
                s=toascii(oldlist[i].py[r])+s;
            }
            oldlist[i].k=s;
        }
    }

    void hash()
    {    int i,adr,d;
        float average,sum=0;
        for (i=0;i<L;i++) 
        {
            hlist[i].name="";
            hlist[i].py="";
            hlist[i].k=0;
            hlist[i].b=0;
        }
       
        for (i=0;i<N;i++) 
        {
       
            adr=(oldlist[i].k)%P;
            d=adr;
            if(hlist[adr].b==0)
            {
                hlist[adr].k=oldlist[i].k;
                hlist[adr].name=oldlist[i].name;
                hlist[adr].py=oldlist[i].py;
                hlist[adr].b=1;
                sum=sum+1;
            }
            else
            {
                do
                {   d=(d+adr%10+1)%P;
                    sum=sum+1;
                }while (hlist[d].b!=0);
                hlist[d].k=oldlist[i].k;
                hlist[d].name=oldlist[i].name;
                hlist[d].py=oldlist[i].py;
                hlist[d].b=1;
            }
        }
        average=sum/30;
        printf("平均查找长度为:%f\n",average);
        if(average>2)
        hash();
    }


    void findlist()  
    {
        char c[20];   
        int i=0,d,g,adr,s=0;
        printf("请输入你要查找的人的拼音:\n");
        getchar();
        gets(c);
       
        for(i=0;c[i]!='\0';i++)
        {

            s=toascii(c[i])+s;
        }
        adr=s%P;
        d=adr;
        if(strcmp(hlist[adr].py,c)==0)
        {
            printf("姓名:%s 拼音:%s 哈希表中存储位置:%d\n",hlist[adr].name,hlist[adr].py,adr);
        }
        else
             if(hlist[adr].b==0)
                {
                  printf("哈希表中无记录!\n");
                }
             else
             {
                 g=0;
                 for (i=0;g==0;i++)
                 {    d=(d+adr%10+1)%P;
                     if(hlist[d].b==0)
                     {
                         printf("哈希表中无记录!\n");
                         g=1;
                     }
                     if(strcmp(hlist[d].py,c)==0)
                     {
                         printf("姓名:%s 拼音:%s 哈希表中存储位置:%d\n",hlist[d].name,hlist[d].py,d);
                         g=1;
                     }
                 }
             }
    }

    void printlist()
    {
        int i;
        for(i=0;i<L;i++)
        printf("%s    %s    %d \n",hlist[i].name,hlist[i].py,hlist[i].k);
    }

    void main()
    {
        int ch;

        InputOldlist();

        hash();

        do
        {
            printf("1.查找  2.输出哈希表  3.退出程序\n");
            scanf("%d",&ch);
            switch(ch)
            {
            case 1:
                findlist();
                printf("Press any key to continue.\n");
                break;
            case 2:
                printlist();
                printf("Press any key to continue.\n");
                break;
            case 3:
                break;
            default: 
                printf("No this operation.\n");
                printf("Press any key to continue.\n");
            }
            getchar();
        }while (ch!=3);
    }

  • 相关阅读:
    【Unity编程】欧拉角与万向节死锁(图文版)
    关于ListView中getView被重复调用的问题
    svn 错误集锦续
    android 无法生成R文件的原因剖析
    SVN下错误集锦
    视频文件格式
    国内各视频网站android pad客户端支持分辨率情况初步统计
    android 下的网络图片加载
    遇到问题描述:Android Please ensure that adb is correctly located at问题解决
    android 时间控件概述
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2035152.html
Copyright © 2020-2023  润新知