{"id":294,"date":"2025-12-28T16:47:28","date_gmt":"2025-12-28T08:47:28","guid":{"rendered":"https:\/\/www.xunfen.cloud\/?p=293"},"modified":"2025-12-28T16:47:28","modified_gmt":"2025-12-28T08:47:28","slug":"day02-2","status":"publish","type":"post","link":"https:\/\/blog.xunfen.cloud\/?p=294","title":{"rendered":"\u6570\u636e\u7ed3\u67842.1-\u6808(\u94fe\u8868)"},"content":{"rendered":"<h1>\u6808(\u94fe\u8868)<\/h1>\n<h2>1.\u4e00\u4e9b\u94fa\u57ab<\/h2>\n<p>\u5728\u4e0a\u4e00\u8282\u4e2d\u7684\u6808\u6211\u4eec\u662f\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u4f46\u662f\u76f8\u5bf9\u800c\u8a00\u6709\u4e00\u4e9b\u7f3a\u70b9\uff0c\u6bd4\u5982\u8bf4\u6570\u7ec4\u7684\u5bb9\u91cf\u6709\u9650\uff0c\u4e0d\u80fd\u505a\u52a8\u6001\u7684\u5904\u7406\uff0c\u6240\u4ee5\u5916\u9762\u5f15\u5165\u4e00\u79cd\u65b0\u7684\u6808\uff0c\u7528\u94fe\u8868\u53bb\u5199\uff0c\u53ef\u4ee5\u89e3\u51b3\u5bb9\u91cf\u7684\u95ee\u9898\uff0c\u4f46\u662f\u94fe\u8868\u7684\u5199\u6cd5\u5bf9\u4e8e\u6570\u7ec4\u800c\u8a00\u8f83\u4e3a\u590d\u6742\uff0c\u9c7c\u548c\u718a\u638c\u4e0d\u53ef\u5f97\u517c<\/p>\n<pre><code class=\"language-\u793a\u610f\u56fe\">                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n                    \u2502  top    \u2502 \u2190 stack.top \u6307\u9488\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502  size   \u2502 \u2190 stack.size \u8bb0\u5f55\u6808\u5927\u5c0f\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                         \u2502\n                         \u25bc\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n             \u2500 \u2500 \u25b6 \u2502  data   \u2502\u2500\u2500\u2500\u25b6\u2502  data   \u2502\u2500\u2500\u2500\u25b6\u2502  data   \u2502\u2500\u2500\u2500\u25b6 NULL\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502  next   \u2502    \u2502  next   \u2502    \u2502  next   \u2502\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                         \u25b2\n                         \u2502\n                    \u6808\u9876\u5143\u7d20 (\u540e\u8fdb\u5148\u51fa)<\/code><\/pre>\n<h2>2.1 \u521d\u59cb\u5316<\/h2>\n<pre><code class=\"language-C\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ \u521d\u59cb\u5316\u94fe\u8868\ntypedef struct node\n{\n    int data;          \/\/ \u6570\u636e\u57df\n    struct node *next; \/\/ \u6307\u9488\u57df\n} node;\n\n\/\/\u521d\u59cb\u5316\u6808\ntypedef struct stack\n{\n    node *top; \/\/ \u6808\u9876\u6307\u9488\n    int size;\n} stack;\n\nint main()\n{\n    stack stack;\n    stack.top = NULL;\/\/ \u521d\u59cb\u5316\u6808\u9876\u6307\u9488\u4e3aNULL\n    stack.size = 0;\/\/  \u521d\u59cb\u5316\u6808\u5927\u5c0f\u4e3a0\n\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6211\u4eec\u4e0d\u4ec5\u5b9a\u4e49\u4e86\u6808\u7684\u7ed3\u6784\u4f53\uff0c\u4e5f\u5b9a\u4e49\u4e86\u94fe\u8868\u7684\u7ed3\u6784\u4f53\u65b9\u4fbf\u540e\u671f\u94fe\u8868\u5165\u6808<\/p>\n<h2>2.2 \u5165\u6808<\/h2>\n<pre><code class=\"language-C\">\/\/\u5165\u6808\n    node *p = (node *)malloc(sizeof(node));\/\/\u7533\u8bf7\u4e00\u4e2a\u94fe\u8868\u7684\u7a7a\u95f4\n    p -&gt;data = 1;\/\/\u6570\u636e\u57df\u6211\u4eec\u586b1\n    p -&gt;next = stack.top;\/\/\u6307\u9488\u57df\u6211\u4eec\u6307\u5411\u6808\u9876\n    stack.top = p;\/\/\u6808\u9876\u6307\u9488\u6211\u4eec\u6307\u5411\u8fd9\u4e2a\u8282\u70b9\n    stack.size++;\/\/\u6808\u5927\u5c0f\u52a01\n    printf(\"success!n\");<\/code><\/pre>\n<p>\u5bf9\u4e8e\u5165\u6808\u7684\u4ee3\u7801\u4e2d\uff0c\u5728\u67e5\u8d44\u6599\u7684\u65f6\u5019\u6709\u4e2a\u7591\u95ee\uff1a p -&gt;next = stack.top; stack.top = p;\u8fd9\u4fe9\u884c\u4e3a\u4ec0\u4e48\u8981\u600e\u4e48\u5199<br \/>\n\u540e\u671f\u901a\u8fc7\u901a\u4e49\u4e86\u89e3\u5230\u7b2c\u4e00\u884c\u4ee3\u7801\u662f\u4e3a\u4e86\u4fdd\u8bc1\u94fe\u8868\u7684\u8fde\u7eed\u6027\uff0c\u80fd\u591f\u8ba9\u65b0\u8282\u70b9\u7684\u6307\u9488\u57df\u6307\u5411\u5f53\u524d\u7684\u6808\u9876<br \/>\n\u7b2c\u4e8c\u884c\u662f\u4e3a\u4e86\u8ba9\u6808\u9876\u6307\u5411\u73b0\u5728\u7684\u65b0\u8282\u70b9<br \/>\n\u540c\u65f6\u5728\u5b66\u4e60\u65f6\u603b\u7ed3\u51fa\u4e00\u4e2a\u7ed3\u8bba\uff1a\u5bf9\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u800c\u8a00\uff0c\u4ed6\u4eec\u7684\u6307\u9488\u57df\u6240\u6307\u5411\u7684\u662f\u51fa\u4e8e\u4ed6\u4eec\u6b63\u4e0b\u65b9\u7684\u8282\u70b9<br \/>\n\u7136\u540e\u8fd9\u91cc\u53ea\u5165\u6808\u4e00\u4e2a\u5143\u7d20\u770b\u4e0d\u51fa\u6765\u4ec0\u4e48\uff0c\u6240\u4ee5\u6211\u4eec\u8fd9\u91cc\u591a\u5165\u51e0\u4e2a\u6765\u7406\u89e3<\/p>\n<pre><code class=\"language-C\">\/\/\u5faa\u73af\u5165\u68085\u4e2a\u5143\u7d20\n     for (int i = 0; i &lt; 5; i++)\n    {\n        node *p = (node *)malloc(sizeof(node));\n        p-&gt;data = i + 1;\n        p-&gt;next = stack.top;\n        stack.top = p;\n        stack.size++;\n        printf(\"%d success!n\",i + 1);\n    }<\/code><\/pre>\n<p>\u90a3\u4e48\uff0c\u73b0\u5728\u6211\u4eec\u7684\u6808\u7684\u793a\u610f\u56fe\u662f<\/p>\n<pre><code>\u6808\u9876 (stack.top) \u2192  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n                    \u2502   5     \u2502 \u2190 \u6700\u540e\u5165\u6808\u7684\u5143\u7d20 (i=4)\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502  \u2500 \u2500 \u2500  \u2502 \u2190 next \u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502   4     \u2502 \u2190 \u7b2c4\u6b21\u5165\u6808\u7684\u5143\u7d20 (i=3)\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502  \u2500 \u2500 \u2500  \u2502 \u2190 next \u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502   3     \u2502 \u2190 \u7b2c3\u6b21\u5165\u6808\u7684\u5143\u7d20 (i=2)\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502  \u2500 \u2500 \u2500  \u2502 \u2190 next \u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502   2     \u2502 \u2190 \u7b2c2\u6b21\u5165\u6808\u7684\u5143\u7d20 (i=1)\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502  \u2500 \u2500 \u2500  \u2502 \u2190 next \u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502   1     \u2502 \u2190 \u7b2c1\u6b21\u5165\u6808\u7684\u5143\u7d20 (i=0)\n                    \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n                    \u2502  NULL   \u2502 \u2190 next \u4e3a NULL (\u6808\u5e95)\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n\n                    \u2193\n                 stack.size = 5<\/code><\/pre>\n<h2>2.3 \u9000\u6808<\/h2>\n<pre><code class=\"language-C\">\/\/ \u9000\u6808\n    \/\/ \u5224\u6808\u7a7a\n    if (stack.top == NULL)\n    {\n        printf(\"stack is empty!n\");\n    }\n    else\n    {\n        node *temp = stack.top; \/\/ \u4fdd\u5b58\u6808\u9876\u8282\u70b9\n        int data = temp-&gt;data;  \/\/ \u53d6\u51fa\u8981\u9000\u6808\u7684\u6570\u636e\u503c\n        stack.top = temp-&gt;next; \/\/\u628a\u6808\u9876\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\n        stack.size--; \/\/\u6808\u5927\u5c0f\u8981\u51cf\u4e00\n        free (temp);\n        printf(\"seccess!n\");\n    }<\/code><\/pre>\n<p><strong> \u6ce8\u610f\uff1a <\/strong> \u4e00\u5b9a\u8981\u5224\u65ad\u662f\u5426<strong>\u6808\u7a7a<\/strong>\uff01\uff01\uff01<\/p>\n<h2>2.4 \u83b7\u53d6\u6808\u9876\u5143\u7d20\u4ee5\u53ca\u904d\u5386\u6808<\/h2>\n<p>1) \u53d6\u6808\u9876<\/p>\n<pre><code class=\"language-C\">    if (stack.top == NULL)\n    {\n        printf(\"stack is empty!n\");\n    }\n    else\n    {\n        printf(\"stack top is %dn\", stack.top-&gt;data);\n    }<\/code><\/pre>\n<p>2) \u904d\u5386\u6808<\/p>\n<pre><code class=\"language-C\">    \/\/ \u904d\u5386\n    if (stack.top == NULL)\n    {\n        printf(\"stack is empty!n\");\n    }\n    else\n    {\n        node *p = stack.top; \/\/ \u5b9a\u4e49\u4e00\u4e2a\u4fbf\u5229\u7684\u6307\u9488\n        while (p != NULL)\n        {\n            printf(\"%d \", p-&gt;data);\n            p = p-&gt;next;\n        }\n    }<\/code><\/pre>\n<p><strong> \u6ce8\u610f\uff1a <\/strong> \u589e\u5220\u67e5\u6539\u65f6\u52a1\u5fc5\u5224\u6808\u7a7a\uff0c\u5426\u5219\u4f1a\u62a5\u9519<br \/>\n\u4ee5\u4e0a\u4fbf\u662f\u7528\u94fe\u8868\u6a21\u62df\u6808\u7684\u4e00\u4e9b\u57fa\u672c\u5199\u6cd5\uff0c\u5b8c\u6574\u4ee3\u7801\u53ef\u4ee5\u5728\u540c\u6587\u4ef6\u5939\u4e0b\u7684.\/stackDemo2.c\u4e2d\u627e\u5230,\u535a\u5ba2\u7684\u8bdd\u4f1a\u628a\u4ee3\u7801\u9644\u5728\u8bc4\u8bba\u533a<br \/>\n\u611f\u8c22\u652f\u6301\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6808(\u94fe\u8868) 1.\u4e00\u4e9b\u94fa\u57ab \u5728\u4e0a\u4e00\u8282\u4e2d\u7684\u6808\u6211\u4eec\u662f\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u4f46\u662f\u76f8\u5bf9\u800c\u8a00\u6709\u4e00\u4e9b\u7f3a\u70b9\uff0c\u6bd4\u5982\u8bf4\u6570\u7ec4\u7684\u5bb9\u91cf\u6709\u9650\uff0c\u4e0d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,6],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","hentry","category-2","category-6"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294"}],"version-history":[{"count":0,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/posts\/294\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}