{"id":1160,"date":"2024-04-16T13:53:03","date_gmt":"2024-04-16T05:53:03","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=1160"},"modified":"2025-10-09T13:55:02","modified_gmt":"2025-10-09T05:55:02","slug":"stack-queue-sort","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2024\/04\/16\/stack-queue-sort\/","title":{"rendered":"\u7528\u6808\u5b9e\u73b0\u961f\u5217\u548c\u6392\u5e8f"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<p>&emsp;&emsp;\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u7528\u6808\u6765\u5b9e\u73b0\u961f\u5217\u548c\u6392\u5e8f\u3002\u8fd9\u90fd\u9700\u8981\u4f7f\u7528\u4e24\u4e2a\u6808\u6765\u5b9e\u73b0\u3002<\/p>\n<h2>\u4e00\u3001\u961f\u5217<\/h2>\n<p>&emsp;&emsp;\u8fd9\u91cc\u6211\u4eec\u6709\u4e00\u4e2a\u5806\u6808\u201cst1\u201d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4e34\u65f6\u5806\u6808\u201cst2\u201d\u6765\u53cd\u8f6c\u201cst1\u201d\u3002 \u6240\u4ee5\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9\u51fa\u65b9\u6cd5\u4e00\uff1a<\/p>\n<ol>\n<li>in queue: directly in <code>st1<\/code>.<\/li>\n<li>out queue: reverse <code>st1<\/code> to <code>st2<\/code>, next pop the top, then reverse <code>st2<\/code> to <code>st1<\/code>.<\/li>\n<\/ol>\n<p>&emsp;&emsp;\u8fd9\u662f\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u4e3a\u4e86\u51cf\u5c11\u64cd\u4f5c\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u65b9\u6cd5\u4e8c\uff1a<\/p>\n<ol>\n<li>in queue: directly in <code>st1<\/code>.<\/li>\n<li>out queue: <strong>if<\/strong> <code>st2<\/code> not empty, pop from <code>st2<\/code>; <strong>else if<\/strong> <code>st2<\/code> empty, reverse <code>st1<\/code> to <code>st2<\/code>, then pop from <code>st2<\/code>.<\/li>\n<\/ol>\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-cpp\">\/\/ use stack to implement queue.\ntemplate&lt;typename elemType&gt;\nclass stque {\nprivate:\n    std::stack&lt;elemType&gt; m_st1;\n    std::stack&lt;elemType&gt; m_st2;\n\n    void reverse()\n    {\n        while (!m_st1.empty())\n        {\n            m_st2.push(m_st1.top());\n            m_st1.pop();\n        }\n    }\n\npublic:\n    stque() { \/* do nothing *\/ }\n\n    void push(elemType ele)\n    {\n        m_st1.push(ele);\n    }\n\n    elemType pop()\n    {\n        if (m_st1.empty() &amp;&amp; m_st2.empty()) \n        {\n            throw std::out_of_range(&quot;Queue is empty.&quot;);\n        }\n\n        \/\/ if st2 empty, reverse st1 to st2 first, then pop.\n        if (m_st2.empty())\n        {\n            this-&gt;reverse();\n        }\n\n        \/\/ if st2 not empty, directly pop.\n        elemType retval = m_st2.top();\n        m_st2.pop();\n        return retval;\n    }\n};<\/code><\/pre>\n<h2>\u4e8c\u3001 \u6392\u5e8f<\/h2>\n<p>&emsp;&emsp;\u6392\u5e8f\u548c\u961f\u5217\u7684\u601d\u8def\u662f\u4e00\u6837\u7684\uff0c\u65b9\u6cd5\u5982\u4e0b\u3002\u6211\u4eec\u6709\u9700\u8981\u88ab\u6392\u5e8f\u7684\u6808\u7684<code>st1<\/code>\uff0c\u548c\u6392\u5e8f\u540e\u7684\u6808\u6808<code>st2<\/code>\uff0c\u5373\u8f93\u51fa\u3002\u6211\u4eec\u4f7f\u7528<code>lt &lt;<\/code>\u4f5c\u4e3a\u987a\u5e8f\u539f\u5219\u3002<\/p>\n<p><!-- 1. Firstly, We'll push all elements into <code>st1<\/code>.\n2. Then move one element from <code>st1<\/code> to <code>st2<\/code>.\n3. While <code>st1<\/code> is not empty, we compare the <code>st1.top<\/code> and <code>st2.top<\/code>, if <code>st1.top &lt; st2.top<\/code>, then we just need to push <code>st2.top<\/code> into <code>st1<\/code>; else if <code>st1.top &gt;= st2.top<\/code>, we need to push the <code>st2.top<\/code> into <code>st1<\/code>, then use --><\/p>\n<blockquote>\n<p>\u5373\u6bcf\u6b21\u4ece\u6808s\u4e2d\u5f39\u51fa\u4e00\u4e2a\u5143\u7d20a\uff0c\u5e76\u5224\u65ad\u5b83\u4e0es2\u4e4b\u95f4\u7684\u6808\u9876\u5143\u7d20b\u7684\u5927\u5c0f\uff0c\u5982\u679ca\u5c0f\u4e8e\u7b49\u4e8eb\uff0c\u5219\u5c06\u5176\u538b\u5165s2\uff0c \u5426\u5219\uff0c\u5c06 s2 \u51fa\u6808\uff0c\u5e76\u5165\u6808\u5230 s \u4e2d\uff0c\u76f4\u5230 s2 \u7684\u6808\u9876\u5143\u7d20\u6070\u597d\u5c0f\u4e8e a\uff0c\u5c06 a \u653e\u5165\u6808 s2 \u4e2d\uff0c\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u76f4\u81f3 s \u4e3a\u7a7a\u3002<\/p>\n<\/blockquote>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-cpp\">\/\/ use stack to implement sort.\ntemplate&lt;typename elemType&gt;\nclass stsort {\npublic:\n    \/\/ sort principle: &lt;, for st2: small nums on top, big nums in bottom\n    std::stack&lt;elemType&gt; sort(std::stack&lt;elemType&gt; st1)\n    {\n        std::stack&lt;elemType&gt; st2;\n\n        st2.push(st1.top());\n        st1.pop();\n\n        while (!st1.empty())\n        {\n            elemType st1Top = st1.top();\n            st1.pop();\n\n        loop:\n            elemType st2Top = st2.top();\n            st2.pop();\n            if (st1Top &lt; st2Top)\n            {\n                st2.push(st2Top);\n                st2.push(st1Top);\n            }\n            else\n            {\n                st1.push(st2Top);\n                if (st2.empty())\n                {\n                    st2.push(st1Top);\n                    continue;\n                }\n                goto loop;\n            }\n        }\n        return st2;\n    }\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;&emsp;\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u7528\u6808\u6765\u5b9e\u73b0\u961f\u5217\u548c\u6392\u5e8f\u3002\u8fd9\u90fd\u9700\u8981\u4f7f\u7528\u4e24\u4e2a\u6808\u6765\u5b9e\u73b0\u3002 \u4e00\u3001\u961f\u5217 &emsp;&#038;emsp &#8230","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[14],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1160"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=1160"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1160\/revisions"}],"predecessor-version":[{"id":1161,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1160\/revisions\/1161"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=1160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=1160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=1160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}