{"id":67,"date":"2022-02-26T21:23:06","date_gmt":"2022-02-26T13:23:06","guid":{"rendered":"https:\/\/blog.lickoo.fun\/?p=67"},"modified":"2022-02-26T21:23:08","modified_gmt":"2022-02-26T13:23:08","slug":"c%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84-%e6%a0%88","status":"publish","type":"post","link":"https:\/\/blog.lickoo.fun\/?p=67","title":{"rendered":"C++\u6570\u636e\u7ed3\u6784\u2014\u2014\u6808"},"content":{"rendered":"\n<!doctype html>\n<html>\n<head>\n<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>\n<\/head>\n<body>\n<p>1\u3001\u6808(Stack)\u662f\u4e00\u79cd\u7ebf\u6027\u5b58\u50a8\u7ed3\u6784\uff0c\u5b83\u5177\u6709\u5982\u4e0b\u7279\u70b9\uff1a<\/p>\n<p>\uff081\uff09\u6808\u4e2d\u7684\u6570\u636e\u5143\u7d20\u9075\u5b88\u201c\u5148\u8fdb\u540e\u51fa&quot;(First In Last Out)\u7684\u539f\u5219\uff0c\u7b80\u79f0FILO\u7ed3\u6784\u3002\uff08\u540e\u8fdb\u5148\u51fa\u7684\u53eb\u6cd5\uff0c\u4e5f\u662f\u53ef\u4ee5\u7684\uff09<\/p>\n<p>\uff082\uff09\u9650\u5b9a\u53ea\u80fd\u5728\u6808\u9876\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<p>#2\u3001\u6808\u7684\u76f8\u5173\u6982\u5ff5\uff1a<\/p>\n<p>\uff081\uff09\u6808\u9876\u4e0e\u6808\u5e95\uff1a\u5141\u8bb8\u5143\u7d20\u63d2\u5165\u4e0e\u5220\u9664\u7684\u4e00\u7aef\u79f0\u4e3a\u6808\u9876\uff0c\u53e6\u4e00\u7aef\u79f0\u4e3a\u6808\u5e95\u3002<\/p>\n<p>\uff082\uff09\u538b\u6808\uff1a\u6808\u7684\u63d2\u5165\u64cd\u4f5c\uff0c\u53eb\u505a\u8fdb\u6808\uff0c\u4e5f\u79f0\u538b\u6808\u3001\u5165\u6808\u3002<\/p>\n<p>\uff083\uff09\u5f39\u6808\uff1a\u6808\u7684\u5220\u9664\u64cd\u4f5c\uff0c\u4e5f\u53eb\u505a\u51fa\u6808\u3002<\/p>\n<p>#3\u3001\u6808\u7684\u5e38\u7528\u64cd\u4f5c\u4e3a\uff1a<\/p>\n<p>\uff081\uff09\u5f39\u6808\uff0c\u901a\u5e38\u547d\u540d\u4e3apop<\/p>\n<p>\uff082\uff09\u538b\u6808\uff0c\u901a\u5e38\u547d\u540d\u4e3apush<\/p>\n<p>\uff083\uff09\u6c42\u6808\u7684\u5927\u5c0f<\/p>\n<p>\uff084\uff09\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a<\/p>\n<p>\uff085\uff09\u83b7\u53d6\u6808\u9876\u5143\u7d20\u7684\u503c<\/p>\n<p>#4\u3001\u6808\u7684\u5e38\u89c1\u5206\u7c7b\uff1a<\/p>\n<p>\uff081\uff09\u57fa\u4e8e\u6570\u7ec4\u7684\u6808\u2014\u2014\u4ee5\u6570\u7ec4\u4e3a\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u901a\u5e38\u4ee5\u6570\u7ec4\u5934\u4e3a\u6808\u5e95\uff0c\u6570\u7ec4\u5934\u5230\u6570\u7ec4\u5c3e\u4e3a\u6808\u9876\u7684\u751f\u957f\u65b9\u5411<\/p>\n<p>\uff082\uff09\u57fa\u4e8e\u5355\u94fe\u8868\u7684\u6808\u2014\u2014\u4ee5\u94fe\u8868\u4e3a\u5e95\u5c42\u7684\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u4ee5\u94fe\u8868\u5934\u4e3a\u6808\u9876\uff0c\u4fbf\u4e8e\u8282\u70b9\u7684\u63d2\u5165\u4e0e\u5220\u9664\uff0c\u538b\u6808\u4ea7\u751f\u7684\u65b0\u8282\u70b9\u5c06\u4e00\u76f4\u51fa\u73b0\u5728\u94fe\u8868\u7684\u5934\u90e8<\/p>\n<p>#5\u3001\u5b9e\u4f8b\u5206\u6790<\/p>\n<blockquote><p>\u4f7f\u7528\u6807\u51c6\u5e93\u7684\u6808\u65f6, \u5e94\u5305\u542b\u76f8\u5173\u5934\u6587\u4ef6\uff0c\u5728\u6808\u4e2d\u5e94\u5305\u542b\u5934\u6587\u4ef6\uff1a #include&lt; stack &gt; \u3002\u5b9a\u4e49\uff1astack&lt; int &gt; s;<\/p>\n<\/blockquote>\n<pre><code>s.empty();         \/\/\u5982\u679c\u6808\u4e3a\u7a7a\u5219\u8fd4\u56detrue, \u5426\u5219\u8fd4\u56defalse;\ns.size();          \/\/\u8fd4\u56de\u6808\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\ns.top();           \/\/\u8fd4\u56de\u6808\u9876\u5143\u7d20, \u4f46\u4e0d\u5220\u9664\u8be5\u5143\u7d20\ns.pop();           \/\/\u5f39\u51fa\u6808\u9876\u5143\u7d20, \u4f46\u4e0d\u8fd4\u56de\u5176\u503c\ns.push();          \/\/\u5c06\u5143\u7d20\u538b\u5165\u6808\u9876\n<\/code><\/pre>\n<p>\uff081\uff09\u57fa\u4e8e\u6570\u7ec4\u7684\u6808<\/p>\n<pre><code>#include &lt;stack&gt;\n#include &lt;iostream&gt;\nusing namespace std;\n \nint main()\n{\n\tstack&lt;int&gt; mystack;\n\tint sum = 0;\n\tfor (int i = 0; i &lt;= 10; i++){\n\t\tmystack.push(i);\n\t}\n\tcout &lt;&lt; &quot;size is &quot; &lt;&lt; mystack.size() &lt;&lt; endl;\n\twhile (!mystack.empty()){\n\t\tcout &lt;&lt; &quot; &quot; &lt;&lt; mystack.top();\n\t\tmystack.pop();\n\t}\n\tcout &lt;&lt; endl;\n\tsystem(&quot;pause&quot;);\n\treturn 0;\n}\n\/\/size is 11\n\/\/ 10 9 8 7 6 5 4 3 2 1 0\n<\/code><\/pre>\n<p>\uff082\uff09\u57fa\u4e8e\u5355\u94fe\u8868\u7684\u6808<\/p>\n<pre><code>#include &lt;iostream&gt;\nusing namespace std;\ntemplate&lt;class T&gt;class Stack\n{\nprivate:\n\tstruct Node\n\t{\n\t\tT data;\n\t\tNode *next;\n\t};\n\tNode *head;\n\tNode *p;\n\tint length;\n \npublic:\n\tStack()\n\t{\n\t\thead = NULL;\n\t\tlength = 0;\n\t}\n\tvoid push(T n)\/\/\u5165\u6808\n\t{\n\t\tNode *q = new Node;\n\t\tq-&gt;data = n;\n\t\tif (head == NULL)\n\t\t{\n\t\t\tq-&gt;next = head;\n\t\t\thead = q;\n\t\t\tp = q;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tq-&gt;next = p;\n\t\t\tp = q;\n\t\t}\n\t\tlength++;\n\t}\n \n\tT pop()\/\/\u51fa\u6808\u5e76\u4e14\u5c06\u51fa\u6808\u7684\u5143\u7d20\u8fd4\u56de\n\t{\n\t\tif (length &lt;= 0)\n\t\t{\n\t\t\tabort();\n\t\t}\n\t\tNode *q;\n\t\tT data;\n\t\tq = p;\n\t\tdata = p-&gt;data;\n\t\tp = p-&gt;next;\n\t\tdelete(q);\n\t\tlength--;\n\t\treturn data;\n\t}\n\tint size()\/\/\u8fd4\u56de\u5143\u7d20\u4e2a\u6570\n\t{\n\t\treturn length;\n\t}\n\tT top()\/\/\u8fd4\u56de\u6808\u9876\u5143\u7d20\n\t{\n\t\treturn p-&gt;data;\n\t}\n\tbool isEmpty()\/\/\u5224\u65ad\u6808\u662f\u4e0d\u662f\u7a7a\u7684\n\t{\n\t\tif (length == 0)\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t}\n\tvoid clear()\/\/\u6e05\u7a7a\u6808\u4e2d\u7684\u6240\u6709\u5143\u7d20\n\t{\n\t\twhile (length &gt; 0)\n\t\t{\n\t\t\tpop();\n\t\t}\n\t}\n};\nint main()\n{\n\tStack&lt;char&gt; s;\n\ts.push(&#39;a&#39;);\n\ts.push(&#39;b&#39;);\n\ts.push(&#39;c&#39;);\n\twhile (!s.isEmpty())\n\t{\n\t\tcout &lt;&lt; s.pop() &lt;&lt; endl;\n\t}\n\tsystem(&quot;pause&quot;);\n\treturn 0;\n}\n<\/code><\/pre>\n<p> <\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<hr>\n<p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300cGeekZW\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002\n\u539f\u6587\u94fe\u63a5\uff1a<a href='https:\/\/blog.csdn.net\/zichen_ziqi\/article\/details\/80807989' target='_blank' class='url' rel=\"noopener\">https:\/\/blog.csdn.net\/zichen_ziqi\/article\/details\/80807989<\/a><\/p>\n<\/body>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u6808(Stack)\u662f\u4e00\u79cd\u7ebf\u6027\u5b58\u50a8\u7ed3\u6784\uff0c\u5b83\u5177\u6709\u5982\u4e0b\u7279\u70b9\uff1a \uff081\uff09\u6808\u4e2d\u7684\u6570\u636e\u5143\u7d20\u9075\u5b88\u201c\u5148\u8fdb\u540e\u51fa&quot;(Fi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/posts\/67"}],"collection":[{"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=67"}],"version-history":[{"count":1,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":68,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions\/68"}],"wp:attachment":[{"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}