{"id":52,"date":"2013-12-14T14:19:09","date_gmt":"2013-12-14T14:19:09","guid":{"rendered":""},"modified":"2013-12-14T14:19:09","modified_gmt":"2013-12-14T14:19:09","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=52","title":{"rendered":"HashTable"},"content":{"rendered":"<pre class=\"brush:cpp; toolbar: true; auto-links: true;\">\/\/\u62c9\u94fe\u6cd5\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;conio.h&gt;\r\n\r\n#define DATATYPE int\r\n#define FORM \"%d\"\r\n#define MAX 17\r\n\r\ntypedef struct TableNode\r\n{\r\n    DATATYPE data;\r\n    struct TableNode *next;\r\n} TNode;\r\n\r\nint InsertNode(TNode **T,DATATYPE elem)\r\n{\r\n    TNode *temp=NULL;\r\n    if(*T==NULL)\r\n    {\r\n        *T=(TNode *)malloc(sizeof(TNode));\r\n        if(*T==NULL) return 0;\r\n        (*T)-&gt;data=elem;\r\n        (*T)-&gt;next=NULL;\r\n        return 1;\r\n    }\r\n    temp=*T;\r\n    while(temp-&gt;next) temp=temp-&gt;next;\r\n    if((temp-&gt;next=(TNode *)malloc(sizeof(TNode)))==NULL) return 0;\r\n    temp-&gt;next-&gt;data=elem;\r\n    temp-&gt;next-&gt;next=NULL;\r\n\r\n    return 1;\r\n}\r\n\r\nint search(TNode *T,DATATYPE elem)\r\n{\r\n    if(!T) return 0;\r\n\r\n    while(T)\r\n    {\r\n        if(T-&gt;data==elem)\r\n            return 1;\r\n        T=T-&gt;next;\r\n    }\r\n    return 0;\r\n}\r\n\r\nint main()\r\n{\r\n    int i=0;\r\n    DATATYPE elem;\r\n    TNode *HashTable[MAX];\r\n\r\n    memset(HashTable,NULL,sizeof(HashTable));\r\n    printf(\"\u5f00\u59cb\u6dfb\u52a0\u5143\u7d20...\\n\");\r\n    while(i&lt;100000)\r\n    {\r\n        \/\/if(scanf(FORM,&amp;elem)!=1) break;\r\n        i++;\r\n        srand(i);\r\n        elem=rand();\r\n        if(!InsertNode(&amp;HashTable[elem%MAX],elem))\r\n        {\r\n            printf(\"\u5143\u7d20\u6dfb\u52a0\u5931\u8d25!\\n\");\r\n            break;\r\n        }\r\n        printf(\"\u5df2\u6dfb\u52a0%d\u4e2a\u5143\u7d20\u3002\\r\",i);\r\n    }\r\n    while(1)\r\n    {\r\n        printf(\"\u8bf7\u8f93\u5165\u8981\u67e5\u8be2\u7684\u5143\u7d20:\");\r\n        fflush(stdin);\r\n        if(scanf(FORM,&amp;elem)!=1)\r\n        {\r\n            printf(\"\u8f93\u5165\u6709\u8bef,\");\r\n            continue;\r\n        }\r\n        if(search(HashTable[elem%MAX],elem))\r\n        {\r\n            printf(\"\u5728\u7b2c%d\u4e2a\u8868\u5185\u627e\u5230\u5143\u7d20!\\n\",elem%MAX+1);\r\n            getch();\r\n        }\r\n        else\r\n        {\r\n            printf(\"\u6ca1\u627e\u5230\u6b64\u5143\u7d20\u3002\\n\");\r\n            if(!InsertNode(&amp;HashTable[elem%MAX],elem))\r\n                printf(\"\u6dfb\u52a0\u5931\u8d25\u3002\\n\");\r\n            else\r\n                printf(\"\u5df2\u6dfb\u52a0\u3002\\n\");\r\n            getch();\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}\r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n\/\/\u7ebf\u6027\u63a2\u6d4b\u518d\u6563\u5217\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;conio.h&gt;\r\n\r\n#define MAX_LENGTH 11\r\n#define DATATYPE int\r\n\r\nconst char *NAME[]= {\"aa\",\"bb\",\"cc\",\"dd\",\"ee\",\"ff\",\"gg\",\"hh\",\"ii\",\"j\",\"kk\"};\r\nconst char *BIRTHDAY[]= {\"1993.5.9\",\"1993.12.30\",\"1993.1.7\",\"1991.10.21\",\"1992.11.12\",\"1994.9.2\",\"1990.3.2\",\\\r\n                         \"1992.11.2\",\"1993.1.12\",\"1993.11.2\",\"1993.12.2\",\r\n                        };\r\n\r\ntypedef struct\r\n{\r\n    char name[10];\r\n    char birthday[15];\r\n} Friend;\r\n\r\nvoid importInfo(Friend *table)\r\n{\r\n    int i,j,n;\r\n    Friend temp;\r\n\r\n    for(i=0; i&lt;MAX_LENGTH; i++)\r\n    {\r\n        memset(&amp;temp,NULL,sizeof(Friend));\r\n        memcpy(temp.name,NAME[i],strlen(NAME[i]));\r\n        memcpy(temp.birthday,BIRTHDAY[i],strlen(BIRTHDAY[i]));\r\n        for(j=n=0; j&lt;strlen(temp.name); j++)\r\n            n+=temp.name[j];\r\n        if(*(char *)&amp;table[n%MAX_LENGTH]==NULL)\r\n        {\r\n            printf(\"\u65e0\u51b2\u7a81:%s\\n\",temp.name);\r\n            \/\/\u5f53\u524d\u5143\u7d20\u503c\u4e3a\u7a7a\r\n            table[n%MAX_LENGTH]=temp;\r\n        }\r\n        else\r\n        {\r\n            printf(\"\u6709\u51b2\u7a81:%s\\n\",temp.name);\r\n            \/\/\u7ebf\u6027\u63a2\u6d4b\u518d\u6563\u5217\r\n            j=n%MAX_LENGTH;\r\n            do\r\n            {\r\n                j++;\r\n                if(j==MAX_LENGTH)\r\n                    j=0;\r\n                if(*(char *)&amp;table[j]==NULL)\r\n                {\r\n                    table[j]=temp;\r\n                    break;\r\n                }\r\n            }\r\n            while(j!=n%MAX_LENGTH);\r\n        }\r\n    }\r\n    return;\r\n}\r\n\r\nint CreateHashTable(Friend **table)\r\n{\r\n    *table=(Friend *)malloc(sizeof(Friend)*MAX_LENGTH);\r\n    if(!*table) return 0;\r\n    memset(*table,NULL,sizeof(Friend)*MAX_LENGTH);\r\n    importInfo(*table);\r\n\r\n    return 1;\r\n}\r\n\r\nFriend *search(Friend *table,char *name)\r\n{\r\n    int i,n;\r\n\r\n    for(i=n=0; i&lt;strlen(name); i++)\r\n        n+=name[i];\r\n\r\n    if(!strcmp(table[n%MAX_LENGTH].name,name))\r\n        return &amp;table[n%MAX_LENGTH];\r\n\r\n    i=n%MAX_LENGTH;\r\n    do\r\n    {\r\n        i++;\r\n        if(i==MAX_LENGTH)\r\n            i=0;\r\n        if(!strcmp(table[i].name,name))\r\n            return &amp;table[i];\r\n    }\r\n    while(i!=n%MAX_LENGTH);\r\n\r\n    return NULL;\r\n}\r\n\r\nint main()\r\n{\r\n    Friend *HashTable=NULL,*find=NULL;\r\n    char str[20];\r\n\r\n    CreateHashTable(&amp;HashTable);\r\n    int i;\r\n    for(i=0; i&lt;11; i++)\r\n        printf(\"%s\\n\",HashTable[i].name);\r\n    printf(\"\u8bf7\u8f93\u5165\u67e5\u627e\u4eba\u7684\u59d3\u540d:\");\r\n    fflush(stdin);\r\n    gets(str);\r\n    if(find=search(HashTable,str))\r\n        printf(\"\u5728Hash\u8868\u4e2d\u627e\u5230\uff0c\u751f\u65e5\u662f:%s\\n\",find-&gt;birthday);\r\n    else\r\n        printf(\"\u5728Hash\u8868\u4e2d\u4e0d\u5b58\u5728\u3002\\n\");\r\n\r\n    return 0;\r\n}\r\n\r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n <\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\n\t\/\/\u62c9\u94fe\u6cd5\n<\/p>\n<p>\n\t#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;conio.h&gt;\n<\/p>\n<p>\n\t#define DATATYPE int<br \/>\n#define FORM &#8220;%d&#8221;<br \/>\n#define MAX 17\n<\/p>\n<p>\n\ttypedef struct TableNode<br \/>\n{<br \/>\n&nbsp;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[326],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>HashTable - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=52\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HashTable - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"\/\/\u62c9\u94fe\u6cd5    #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;conio.h&gt;    #define DATATYPE int #define FORM &quot;%d&quot; #define MAX 17    typedef struct TableNode { &nbsp;...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=52\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2013-12-14T14:19:09+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=52#webpage\",\"url\":\"http:\/\/weizn.net\/?p=52\",\"name\":\"HashTable - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"datePublished\":\"2013-12-14T14:19:09+00:00\",\"dateModified\":\"2013-12-14T14:19:09+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=52#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=52\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=52#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HashTable\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=52#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=52#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"HashTable\",\"datePublished\":\"2013-12-14T14:19:09+00:00\",\"dateModified\":\"2013-12-14T14:19:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=52#webpage\"},\"wordCount\":1,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"articleSection\":[\"\\u6570\\u636e\\u7ed3\\u6784\\u53ca\\u7b97\\u6cd5\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=52#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HashTable - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=52","og_locale":"zh_CN","og_type":"article","og_title":"HashTable - Wayne&#039;s Blog","og_description":"\/\/\u62c9\u94fe\u6cd5    #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;conio.h&gt;    #define DATATYPE int #define FORM \"%d\" #define MAX 17    typedef struct TableNode { &nbsp;...","og_url":"http:\/\/weizn.net\/?p=52","og_site_name":"Wayne&#039;s Blog","article_published_time":"2013-12-14T14:19:09+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=52#webpage","url":"http:\/\/weizn.net\/?p=52","name":"HashTable - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"datePublished":"2013-12-14T14:19:09+00:00","dateModified":"2013-12-14T14:19:09+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=52#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=52"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=52#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"HashTable"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=52#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=52#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"HashTable","datePublished":"2013-12-14T14:19:09+00:00","dateModified":"2013-12-14T14:19:09+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=52#webpage"},"wordCount":1,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"articleSection":["\u6570\u636e\u7ed3\u6784\u53ca\u7b97\u6cd5"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=52#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=52"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/52\/revisions"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}