{"id":28,"date":"2017-01-17T11:30:34","date_gmt":"2017-01-17T02:30:34","guid":{"rendered":"http:\/\/kpc2017.satoshis.jp\/?p=28"},"modified":"2021-09-23T17:27:41","modified_gmt":"2021-09-23T08:27:41","slug":"%e3%83%8f%e3%83%83%e3%82%b7%e3%83%a5%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kpc2017.satoshis.jp\/?p=28","title":{"rendered":"\u30cf\u30c3\u30b7\u30e5\u6cd5"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8p.112\u306e\u30cf\u30c3\u30b7\u30e5\u6cd5\u306e\u5b9f\u88c5\u3092\u884c\u3046\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>ChainHash.h<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#ifndef __ChainHash\r\n#define __ChainHash\r\n\r\n#include &quot;Member.h&quot;\r\n\r\ntypedef struct __node {\r\n\tMember  data;\r\n\tstruct __node *next;\r\n} Node;\r\n\r\ntypedef struct {\r\n\tint size;\r\n\tNode **table;\r\n} ChainHash;\r\n\r\nint Initialize(ChainHash *h, int size);\r\n\r\nNode *Search(const ChainHash *h, const Member *x);\r\n\r\nint Add(ChainHash *h, const Member *x);\r\n\r\nint Remove(ChainHash *h, const Member *x);\r\n\r\nvoid Dump(const ChainHash *h);\r\n\r\nvoid Clear(ChainHash *h);\r\n\r\nvoid Terminate(ChainHash *h);\r\n\r\n#endif\r\n<\/pre>\n<p>ChainHash.c<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &quot;Member.h&quot;\r\n#include &quot;ChainHash.h&quot;\r\n\r\nstatic int hash(int key, int size) \r\n{\r\n\treturn key % size;\r\n}\r\n\r\nstatic void SetNode(Node *n, const Member *x, const Node *next)\r\n{\r\n\tn-&gt;data = *x;\r\n\tn-&gt;next = next;\r\n}\r\n\r\nint Initialize(ChainHash *h, int size)\r\n{\r\n\tint i;\r\n\r\n\tif((h-&gt;table = calloc(size, sizeof(Node *))) == NULL) {\r\n\t\th-&gt;size = 0;\r\n\t\treturn 0;\r\n\t}\r\n\th-&gt;size = size;\r\n\tfor (i = 0; i &lt; size; i++)\r\n\t\th-&gt;table[i] = NULL;\r\n\treturn 1;\r\n}\r\n\r\nNode *Search(const ChainHash *h, const Member *x)\r\n{\r\n\tint key = hash(x-&gt;no, h-&gt;size);\r\n\tNode *p = h-&gt;table[key];\r\n\r\n\twhile (p != NULL) {\r\n\t\tif (p-&gt;data.no == x-&gt;no)\r\n\t\t\treturn p;\r\n\t\tp = p-&gt;next;\r\n\t}\r\n\treturn NULL;\r\n}\r\n\r\nint Add(ChainHash *h, const Member *x)\r\n{\r\n\tint key = hash(x-&gt;no, h-&gt;size);\r\n\tNode *p = h-&gt;table[key];\r\n\tNode *temp;\r\n\twhile (p != NULL) {\r\n\t\tif (p-&gt;data.no == x-&gt;no)\r\n\t\t\treturn 1;\r\n\t\tp = p-&gt;next;\r\n\t}\r\n\tif ((temp = calloc(1, sizeof(Node))) == NULL)\r\n\t\treturn 2;\r\n\tSetNode(temp, x, h-&gt;table[key]);\r\n\th-&gt;table[key] = temp;\r\n\treturn 0;\r\n}\r\n\r\nint Remove(ChainHash *h, const Member *x)\r\n{\r\n\tint key = hash(x-&gt;no, h-&gt;size);\r\n\tNode *p = h-&gt;table[key];\r\n\tNode **pp = &amp;h-&gt;table[key];\r\n\twhile (p != NULL) {\r\n\t\tif (p-&gt;data.no == x-&gt;no) {\r\n\t\t\t*pp = p-&gt;next;\r\n\t\t\tfree(p);\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\tpp = &amp;p-&gt;next;\r\n\t\tp = p-&gt;next;\r\n\t}\r\n\treturn 1;\r\n}\r\n\r\nvoid Dump(const ChainHash *h)\r\n{\r\n\tint i;\r\n\tfor (i = 0; i &lt; h-&gt;size; i++) {\r\n\t\tNode *p = h-&gt;table[i];\r\n\t\tprintf(&quot;%02d  &quot;, i);\r\n\t\twhile (p != NULL) {\r\n\t\t\tprintf(&quot;\u2192 %d (%s) &quot;, p-&gt;data.no, p-&gt;data.name);\r\n\t\t\tp = p-&gt;next;\r\n\t\t}\r\n\t\tputchar('\\n');\r\n\t}\r\n}\r\n\r\nvoid Clear(ChainHash *h)\r\n{\r\n\tint i;\r\n\tfor (i = 0; i &lt; h-&gt;size; i++) {\r\n\t\tNode *p = h-&gt;table[i];\r\n\t\twhile (p != NULL) {\r\n\t\t\tNode *next = p-&gt;next;\r\n\t\t\tfree(p);\r\n\t\t\tp = next;\r\n\t\t}\r\n\t\th-&gt;table[i] = NULL;\r\n\t}\r\n}\r\n \r\nvoid Terminate(ChainHash *h)\r\n{\r\n\tClear(h);\r\n\tfree(h-&gt;table);\r\n\th-&gt;size = 0;\r\n}\r\n<\/pre>\n<p>ChainHashTest.c<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &quot;Member.h&quot;\r\n#include &quot;ChainHash.h&quot;\r\n\r\ntypedef enum {\r\n\tTERMINATE, ADD, DELETE, SEARCH, CLEAR, DUMP\r\n} Menu;\r\n\r\nMenu SelectMenu(void)\r\n{\r\n\tint ch;\r\n\tdo {\r\n\t\tprintf(&quot;(1)\u8ffd\u52a0 (2)\u524a\u9664 (3)\u63a2\u7d22 (4)\u5168\u524a\u9664 (5)\u30c0\u30f3\u30d7 (0)\u7d42\u4e86: &quot;);\r\n\t\tscanf_s(&quot;%d&quot;, &amp;ch);\r\n\t} while (ch &lt; TERMINATE || ch &gt; DUMP);\r\n\treturn (Menu)ch;\r\n}\r\n\r\nint main(void)\r\n{\r\n\tMenu menu;\r\n\tChainHash hash;\r\n\tInitialize(&amp;hash, 13);\r\n\tdo {\r\n\t\tint result;\r\n\t\tMember x;\r\n\t\tNode *temp;\r\n\t\tswitch (menu = SelectMenu()) {\r\n\t\tcase ADD:\r\n\t\t\tx = ScanMember(&quot;\u8ffd\u52a0&quot;, MEMBER_NO | MEMBER_NAME);\r\n\t\t\tresult = Add(&amp;hash, &amp;x);\r\n\t\t\tif (result)\r\n\t\t\t\tprintf(&quot;\u30a8\u30e9\u30fc\uff1a\u8ffd\u52a0\u306b\u5931\u6557\u3057\u307e\u3057\u305f(%s)\u3002\\n&quot;, (result == 1) ? &quot;\u767b\u9332\u6e08\u307f&quot; : &quot;\u30e1\u30e2\u30ea\u4e0d\u8db3&quot;);\r\n\t\t\tbreak;\r\n\t\tcase DELETE:\r\n\t\t\tx = ScanMember(&quot;\u524a\u9664&quot;, MEMBER_NO);\r\n\t\t\tresult = Remove(&amp;hash, &amp;x);\r\n\t\t\tif (result == 1)\r\n\t\t\t\tprintf(&quot;\u30a8\u30e9\u30fc\uff1a\u305d\u306e\u756a\u53f7\u306e\u30c7\u30fc\u30bf\u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002\\n&quot;);\r\n\t\t\tbreak;\r\n\t\tcase SEARCH:\r\n\t\t\tx = ScanMember(&quot;\u63a2\u7d22&quot;, MEMBER_NO);\r\n\t\t\ttemp = Search(&amp;hash, &amp;x);\r\n\t\t\tif (temp == NULL)\r\n\t\t\t\tprintf(&quot;\u63a2\u7d22\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002\\n&quot;);\r\n\t\t\telse { \r\n\t\t\t\tprintf(&quot;\u63a2\u7d22\u306b\u6210\u529f\u3057\u307e\u3057\u305f\u3002\\n&quot;);\r\n\t\t\t\tPrintLnMember(&amp;temp-&gt;data);\r\n\t\t\t}\r\n\t\t\tbreak;\r\n\t\tcase CLEAR:\r\n\t\t\tClear(&amp;hash);\r\n\t\t\tbreak;\r\n\t\tcase DUMP:\r\n\t\t\tDump(&amp;hash);\r\n\t\t\tbreak;\r\n\t\t}\r\n\t} while (menu != TERMINATE);\r\n\tTerminate(&amp;hash);\r\n\treturn 0;\r\n}\r\n<\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8p.112\u306e\u30cf\u30c3\u30b7\u30e5\u6cd5\u306e\u5b9f\u88c5\u3092\u884c\u3046\u3002 &nbsp; ChainHash.h ChainHash.c ChainHashTest.c<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/28"}],"collection":[{"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=28"}],"version-history":[{"count":4,"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":33,"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/28\/revisions\/33"}],"wp:attachment":[{"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kpc2017.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}