{"id":294,"date":"2011-10-27T13:55:47","date_gmt":"2011-10-27T05:55:47","guid":{"rendered":"http:\/\/wangkaixuan.tech\/?p=294"},"modified":"2020-06-06T13:57:25","modified_gmt":"2020-06-06T05:57:25","slug":"%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84_%e6%95%b0%e7%bb%84%e4%b8%8e%e5%b9%bf%e4%b9%89%e8%a1%a8_%e5%b9%bf%e4%b9%89%e8%a1%a8%e7%9a%84%e5%bb%ba%e7%ab%8b%e3%80%81%e9%81%8d%e5%8e%86%e3%80%81%e5%a4%8d%e5%88%b6","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=294","title":{"rendered":"\u6570\u636e\u7ed3\u6784_\u6570\u7ec4\u4e0e\u5e7f\u4e49\u8868_\u5e7f\u4e49\u8868\u7684\u5efa\u7acb\u3001\u904d\u5386\u3001\u590d\u5236\u3001\u6c42\u6df1\u5ea6"},"content":{"rendered":"\n<p>\u7531\u4e00\u4e2a\u5b58\u50a8\u7740\u5e7f\u4e49\u8868\u4fe1\u606f\u7684\u5b57\u7b26\u4e32\u5efa\u7acb\u4e00\u4e2a\u5e7f\u4e49\u8868\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u590d\u5236\uff0c\u6c42\u6df1\u5ea6\u3002<br>\u9012\u5f52\u771f\u662f\u5947\u5999\u65e0\u7a77\u554a\uff0c\u6709\u4e9b\u770b\u4f3c\u5f88\u590d\u6742\u7684\u4e1c\u897f\u53ea\u8981\u627e\u5230\u89c4\u5f8b\u5c31\u80fd\u8fdb\u884c\u9012\u5f52\u6c42\u89e3\u3002\u8ba9\u6211\u4e0d\u5f97\u4e0d\u5bf9\u90a3\u4e9b\u53d1\u73b0\u89c4\u5f8b\u5229\u7528\u89c4\u5f8b\u521b\u9020\u65b0\u4e8b\u7269\u7684\u4eba\u8083\u7136\u8d77\u656c\u3002\u5176\u5b9e\u5728\u4f17\u591a\u7f8e\u4e3d\u7684\u4e8b\u7269\u4e2d\uff0c\u4eba\u7684\u601d\u60f3\u4e5f\u662f\u5f88\u7f8e\u4e3d\u7684\uff0c\u90a3\u4e9b\u6570\u5b66\u5bb6\uff0c\u54f2\u5b66\u5bb6\u80fd\u5728\u610f\u8bc6\u7684\u4e16\u754c\u521b\u9020\u51fa\u7470\u4e3d\u7684\u5947\u89c2\uff0c\u8ba9\u65e0\u6570\u540e\u4eba\u656c\u4ef0\u4e0d\u5df2\uff0c\u597d\u591a\u4eba\u4e0d\u7406\u89e3\u6570\u5b66\u5bb6\u548c\u54f2\u5b66\u5bb6\uff0c\u8ba4\u4e3a\u4ed6\u4eec\u592a\u53e4\u677f\u592a\u75af\u72c2\uff0c\u5176\u5b9e\u4ed6\u4eec\u662f\u88ab\u7269\u8d28\u4e16\u754c\u8499\u853d\u4e86\u53cc\u773c\uff0c\u770b\u5230\u5404\u79cd\u73b0\u5b9e\u4e16\u754c\u7684\u4e0d\u5b8c\u7f8e\uff0c\u6bcf\u5929\u7ea0\u7ed3\u4e8e\u83dc\u4ef7\u6cb9\u4ef7\u548c\u4e03\u5927\u59d1\u516b\u5927\u59e8\u7684\u7410\u4e8b\u4e4b\u4e2d\u3002\u76f8\u53cd\u5012\u662f\u90a3\u4e9b\u4ed6\u4eec\u4e0d\u7406\u89e3\u7684\u6570\u5b66\u5bb6\u548c\u54f2\u5b66\u5bb6\u6d3b\u5728\u81ea\u5df1\u521b\u9020\u51fa\u7684\u201c\u5b8c\u7f8e\u4e16\u754c\u201d\uff0c\u5176\u4e50\u65e0\u7a77\u3002\u7f8e\u4e3d\u7684\u601d\u60f3\uff0c\u7f8e\u4e3d\u7684\u5fc3\u7075\u3002\u4e3a\u90a3\u4e9b\u6570\u5b66\u5bb6\u548c\u54f2\u5b66\u5bb6\u81f4\u656c!<\/p>\n\n\n\n<p><br>glists.h<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\n#include&lt;string>\nusing namespace std;\n\ntypedef enum\n{\n\tATOM, LIST\n} ElemTag;\ntypedef char AtomType;\ntypedef struct GLNode \/\/GLNode\u662f\u4e00\u4e2a\u7c7b\u578b\u540d\n{\n\tElemTag tag; \/\/\u6807\u5fd7\u57df\n\tunion\n\t{\n\t\tAtomType atom;\n\t\tstruct\n\t\t{\n\t\t\tGLNode *hp, *tp;\n\t\t} ptr;\n\t\t\/\/ptr\u662f\u5217\u8868\uff0chp\uff0ctp\u5206\u522b\u6307\u5411\u8be5\u5217\u8868\u7684\u8868\u5934\u548c\u8868\u5c3e\n\t};\n\tGLNode()\n\t{\n\t\tptr.hp = ptr.tp = NULL;\n\t}\n} *List; \/\/List\u662f\u6307\u5411GLNode\u7c7b\u578b\u53d8\u91cf\u7684\u6307\u9488\u7c7b\u578b\n\nclass GLists\n{\npublic:\n\tvoid GetGList(List&amp;); \/\/\u5f97\u5230\u5e7f\u4e49\u8868\n\tvoid CopyGList(List&amp;, List&amp;); \/\/\u590d\u5236\u5e7f\u4e49\u8868\n\tvoid ListTraverse(List);\n\tint GListDepth(List&amp;); \/\/\u6c42\u5e7f\u4e49\u8868\u6df1\u5ea6\nprivate:\n\tvoid CreatGList(List&amp;, string&amp;); \/\/\u9012\u5f52\u5efa\u7acb\u5e7f\u4e49\u8868\n\tvoid sever(string&amp;, string&amp;); \/\/\u5b58\u50a8\u5e7f\u4e49\u8868\u7684\u5b57\u7b26\u4e32\u5904\u7406\n};\n\nvoid GLists::GetGList(List &amp;l)\n{\n\tcout &lt;&lt; \"Please Input The Lists :\" &lt;&lt; endl &lt;&lt; endl;\n\tstring str;\n\tcin >> str;\n\tCreatGList(l, str);\n}\n\nvoid GLists::CreatGList(List &amp;l, string &amp;s) \/\/\u6839\u636e\u7ed9\u5b9a\u5b57\u7b26\u4e32s\uff0c\u4ecel\u9012\u5f52\u521b\u5efa\u5e7f\u4e49\u8868\n{\n\tList p, q; \/\/\u5e7f\u4e49\u8868\u8282\u70b9\u7c7b\u578b\u53d8\u91cf\n\tstring sub, hsub; \/\/\u4e24\u4e2a\u5b57\u7b26\u4e32\n\tif (s == \"()\") \/\/\u7a7a\u8868\n\t\tl = NULL; \/\/l\u7f6e\u7a7a\n\telse \/\/\u975e\u7a7a\u8868\n\t{\n\t\tl = new GLNode; \/\/\u5efa\u7acb\u8282\u70b9\n\t\tif (s.size() == 1) \/\/\u539f\u5b50\u7c7b\u578b\n\t\t{\n\t\t\tl->tag = ATOM; \/\/\u6807\u5fd7\u57df\n\t\t\tl->atom = s&#91;0]; \/\/\u539f\u5b50\n\t\t} \/\/if\n\t\telse \/\/\u5217\u8868\u7c7b\u578b\n\t\t{\n\t\t\tl->tag = LIST; \/\/\u6807\u5fd7\u57df\n\t\t\tp = l;\n\t\t\tsub = s.substr(1, s.size() - 2); \/\/\u8131\u53bb\u62ec\u53f7\n\t\t\tdo \/\/\u6839\u636e\u5f97\u5230\u7684\u5217\u8868\u5b57\u7b26\u4e32\u5efa\u7acb\u65b0\u7684\u5e7f\u4e49\u8868\n\t\t\t{\n\t\t\t\tsever(sub, hsub); \/\/\u5f97\u5230\u5217\u8868\u4e2d\u201c\u6700\u5c0f\u5355\u4f4d\u201dhsub\uff0c\u7136\u540e\u628asub\u7f6e\u6210\u53bb\u6389\u6700\u5c0f\u5355\u4f4d\u7684\u5269\u4f59\u5b57\u7b26\u4e32\n\t\t\t\tCreatGList(p->ptr.hp, hsub); \/\/\u9012\u5f52\u5efa\u7acb\u5e7f\u4e49\u8868\n\t\t\t\tq = p; \/\/\u8bb0\u5f55p\n\t\t\t\tif (!sub.empty()) \/\/\u4e3a\u4e0b\u4e00\u4e2a\u8282\u70b9\u63d0\u524d\u5f00\u8f9f\u8282\u70b9\u7a7a\u95f4\n\t\t\t\t{\n\t\t\t\t\tp = new GLNode;\n\t\t\t\t\tp->tag = LIST; \/\/\u66f4\u6539\u6807\u5fd7\u57df\n\t\t\t\t\tq->ptr.tp = p; \/\/\u8fde\u63a5\u8282\u70b9\n\t\t\t\t}\n\t\t\t} while (!sub.empty());\n\t\t}\n\t} \/\/else\n}\n\nvoid GLists::CopyGList(List &amp;t, List &amp;l) \/\/\u590d\u5236\u5e7f\u4e49\u8868l->t\n{\n\tif (!l)\n\t\tt = NULL; \/\/l\u662f\u7a7a\u8868\n\telse\n\t{\n\t\tt = new GLNode; \/\/\u5f00\u8f9f\u8282\u70b9\n\t\tt->tag = l->tag; \/\/\u6807\u5fd7\u57df\n\t\tif (l->tag == ATOM) \/\/\u662f\u539f\u5b50\u8282\u70b9\n\t\t\tt->atom = l->atom; \/\/\u590d\u5236\u539f\u5b50\n\t\telse \/\/\u5217\u8868\n\t\t{\n\t\t\tCopyGList(t->ptr.hp, l->ptr.hp); \/\/\u9012\u5f52\u590d\u5236\u8868\u5934\n\t\t\tCopyGList(t->ptr.tp, l->ptr.tp); \/\/\u9012\u5f52\u590d\u5236\u8868\u5c3e\n\t\t}\n\t}\n}\n\nint GLists::GListDepth(List &amp;l) \/\/\u6c42\u5e7f\u4e49\u8868\u6df1\u5ea6\n{\n\tint max = 0, dep;\n\tList p = l;\n\tif (l == NULL)\n\t\treturn 1; \/\/\u7a7a\u8868\u6df1\u5ea6\u4e3a1\n\tif (l->tag == ATOM)\n\t\treturn 0; \/\/\u539f\u5b50\u6df1\u5ea6\u4e3a\u96f6\n\t\/\/\u975e\u7a7a\u5217\u8868\u60c5\u51b5\n\tfor (; p; p = p->ptr.tp)\t\/\/\u904d\u5386\u5217\u8868\n\t{\n\t\tdep = GListDepth(p->ptr.hp);\t\/\/\u9012\u5f52\u6c42\u6df1\u5ea6\n\t\tif (dep > max)\t\/\/\u66f4\u65b0max\n\t\t\tmax = dep;\n\t}\n\treturn max + 1;\t\/\/\u8fd4\u56de\u6df1\u5ea6\n}\n\nvoid GLists::sever(string &amp;str, string &amp;hstr)\t\/\/\u5e7f\u4e49\u8868\u5b57\u7b26\u4e32\u5904\u7406\n{\t\/\/\u5c06\u975e\u7a7a\u5b57\u7b26\u4e32\u5206\u5272\u6210\u4e24\u90e8\u5206hstr\u4e3a\u7b2c\u4e00\u4e2a','\u4e4b\u524d\u7684\u90e8\u5206,str\u4e3a\u4e4b\u540e\u7684\u90e8\u5206\n\tint n = str.size(), i = 0, k = 0;\n\tdo\n\t{\n\t\tif (str&#91;i] == '(')\n\t\t\tk++;\n\t\tif (str&#91;i] == ')')\n\t\t\tk--;\n\t\ti++;\n\t} while (i &lt; n &amp;&amp; (str&#91;i] != ',' || k != 0));\n\tif (i &lt; n)\n\t{\n\t\thstr = str.substr(0, i);\n\t\tstr = str.substr(i + 1, n - i - 1);\n\t}\n\telse\n\t{\n\t\thstr = str;\n\t\tstr.clear();\n\t}\n}\n\nvoid GLists::ListTraverse(List L)\t\/\/\u5e7f\u4e49\u8868\u904d\u5386\n{\n\tif (L->tag == ATOM)\n\t{\n\t\tcout &lt;&lt; L->atom &lt;&lt; \",\";\n\t}\n\telse\n\t{\n\t\tcout &lt;&lt; \"(\";\n\t\twhile (L != NULL)\n\t\t{\n\t\t\tListTraverse(L->ptr.hp);\n\t\t\tL = L->ptr.tp;\n\t\t}\n\t\tcout &lt;&lt; \")\";\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p>main.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include\"glists.h\"\n \nint main()\n{\n\tGLists gl;\n\tList l,t;\n\tgl.GetGList(l);\n\tgl.CopyGList(t,l);\n\tcout&lt;&lt;gl.GListDepth(l)&lt;&lt;endl;\n\tcout&lt;&lt;gl.GListDepth(t)&lt;&lt;endl;\n\tgl.ListTraverse(l);\/\/\u904d\u5386\u5e7f\u4e49\u8868\n\tsystem(\"pause\");\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7531\u4e00\u4e2a\u5b58\u50a8\u7740\u5e7f\u4e49\u8868\u4fe1\u606f\u7684\u5b57\u7b26\u4e32\u5efa\u7acb\u4e00\u4e2a\u5e7f\u4e49\u8868\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u590d\u5236\uff0c\u6c42\u6df1\u5ea6\u3002\u9012\u5f52\u771f\u662f\u5947\u5999\u65e0\u7a77\u554a\uff0c\u6709\u4e9b\u770b\u4f3c\u5f88\u590d\u6742\u7684\u4e1c\u897f\u53ea\u8981\u627e\u5230\u89c4\u5f8b\u5c31\u80fd\u8fdb\u884c\u9012\u5f52\u6c42\u89e3\u3002\u8ba9\u6211\u4e0d\u5f97\u4e0d\u5bf9\u90a3\u4e9b\u53d1\u73b0&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/www.wangkaixuan.tech\/?p=294\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-06-02-"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}