{"id":305,"date":"2026-01-10T12:25:26","date_gmt":"2026-01-10T04:25:26","guid":{"rendered":"https:\/\/www.xunfen.cloud\/?p=305"},"modified":"2026-01-10T12:25:26","modified_gmt":"2026-01-10T04:25:26","slug":"%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%843-1-%e7%ae%80%e5%8d%95%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/blog.xunfen.cloud\/?p=305","title":{"rendered":"\u6570\u636e\u7ed3\u67843.1-\u7b80\u5355\u961f\u5217"},"content":{"rendered":"<h1>\u961f\u5217<\/h1>\n<h2>\u4f55\u4e3a<\/h2>\n<p>\u961f\u5217\u548c\u6808\u76f8\u53cd\uff0c\u9057\u5740\u4e2d<strong>\u5148\u8fdb\u5148\u51fa<\/strong>\u7684\u7ebf\u6027\u8868\uff0c\u4ed6\u7684\u5165\u961f\u4e0e\u51fa\u961f\u5206\u522b\u5728\u961f\u5217\u7684\u4fe9\u5934\uff0c\u5373\u53ea\u5141\u8bb8\u4e00\u7aef\u63d2\u5165\u5143\u7d20\uff0c\u53e6\u4e00\u7aef\u5220\u9664\u5143\u7d20\uff0c\u4e0e\u73b0\u5b9e\u4e2d\u7684\u961f\u5217\u4e00\u6837<\/p>\n<pre><code>\u961f\u5217\u793a\u610f\u56fe\uff1a\n\n\u5165\u961f\u7aef\uff08\u961f\u5c3e\uff09                    \u51fa\u961f\u7aef\uff08\u961f\u5934\uff09\n     \u2193                              \u2193\n\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502  A  \u2502  B  \u2502  C  \u2502  D  \u2502  E  \u2502     \u2502\n\u251c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2524\n\u2190 \u63d2\u5165\u5143\u7d20\u65b9\u5411              \u5220\u9664\u5143\u7d20 \u2192\n     (rear)                        (front)<\/code><\/pre>\n<p>\u961f\u5217\u7684\u64cd\u4f5c\u6709\uff1a<strong>\u5165\u961f<\/strong>\u3001<strong>\u51fa\u961f<\/strong>\u3001<strong>\u53d6\u961f\u5934\u5143\u7d20<\/strong>\u3001<strong>\u5224\u961f\u7a7a\/\u6ee1<\/strong><br \/>\n\u6b64\u5904\u6211\u4eec\u4f9d\u65e7\u4f7f\u7528C\u8bed\u8a00\u793a\u4f8b\u961f\u5217\u7684\u589e\u5220\u67e5\u6539<\/p>\n<h2>\u64cd\u4f5c<\/h2>\n<h3>1.\u521d\u59cb\u5316<\/h3>\n<p>\u5728\u8fd9\u91cc\u6211\u4eec\u5148\u5b9a\u4e49\u961f\u5217\u8282\u70b9\u7ed3\u6784\u4f53<\/p>\n<pre><code class=\"language-C\">typedef struct QNode{\n    int data;\n    struct QNode *next;\n} QNode, *QueuePtr;<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u961f\u5217\u7ed3\u6784\u4f53\u7684\u522b\u540dQNode\uff0c\u540c\u65f6\u5b9a\u4e49\u4e86\u6307\u5411\u7ed3\u6784\u4f53\u7684\u6307\u9488\u522b\u540dQueuePtr<\/p>\n<pre><code class=\"language-C\">typedef struct{\n    QueuePtr front;\/\/\u5bf9\u5934\u6307\u9488\u57df\n    QueuePtr rear;\/\/\u961f\u5c3e\u6307\u9488\u57df\n} *LinkQueue;<\/code><\/pre>\n<p>\u7531\u4e8e\u6211\u4eec\u8981\u5904\u7406\u961f\u5934\u961f\u5c3e\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u8fd9\u91cc\u5b9a\u4e49\u4e86\u5bf9\u5934\u548c\u961f\u5c3e\u7684\u6307\u9488<\/p>\n<pre><code class=\"language-C\">void InitQueue(LinkQueue Q){ \n    Q-&gt;front = Q-&gt;rear = NULL;\n    printf(\"\u961f\u5217\u521d\u59cb\u5316\u6210\u529f\uff01\");\n}<\/code><\/pre>\n<p>\u968f\u540e\u6211\u4eec\u5c06\u961f\u5934\u548c\u961f\u5c3e\u90fd\u521d\u59cb\u5316\u4e3aNULL\uff0c\u540c\u65f6\u8f93\u51fa\u521d\u59cb\u5316\u6210\u529f<\/p>\n<h3>2.\u5165\u961f<\/h3>\n<pre><code class=\"language-C\">void append(LinkQueue Q, int e){\n    QNode *p = (QNode *)malloc(sizeof(QNode));\/\/\u7533\u8bf7\u7a7a\u95f4\n    p-&gt;data = e;\/\/\u628a\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u57df\n    p-&gt;next = NULL;\/\/\u8be5\u8282\u70b9\u7684\u4e0b\u4e00\u8282\u70b9\u7f6e\u7a7a\u6307\u5411NULL\n    Q-&gt;rear-&gt;next = p;\/\/\u961f\u5c3e\u6307\u9488\u57df\u7684next\u6307\u5411\u65b0\u8282\u70b9\n    Q-&gt;rear = p;\n}<\/code><\/pre>\n<p>\u961f\u5217\u7684\u5165\u961f\u64cd\u4f5c\uff0c\u6211\u4eec\u9996\u5148\u7533\u8bf7\u4e00\u4e2a\u8282\u70b9\uff0c\u7136\u540e\u628a\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u57df\uff0c\u540c\u65f6\u5c06\u4e0b\u4e00\u8282\u70b9\u7f6e\u7a7a\uff0c\u7136\u540e\u961f\u5c3e\u6307\u9488\u57df\u7684next\u6307\u5411\u65b0\u8282\u70b9\uff0c\u6700\u540e\u5c06\u961f\u5c3e\u6307\u9488\u57df\u6307\u5411\u65b0\u8282\u70b9<\/p>\n<h3>3.\u51fa\u961f<\/h3>\n<pre><code class=\"language-C\">\/\/ \u51fa\u961f\nvoid pop(LinkQueue Q, int *e){\n    \/\/\u5224\u5bf9\u7a7a\n    if(Q == NULL || Q-&gt;front == NULL){\n        printf(\"\u961f\u5217\u4e3a\u7a7a\uff01n\");\n    }\n    QNode *p = Q-&gt;front;     \/\/ \u4fdd\u5b58\u5f53\u524d\u961f\u5934\u8282\u70b9\u7684\u5730\u5740\n    *e = p-&gt;data;            \/\/ \u5c06\u8981\u5220\u9664\u8282\u70b9\u7684\u6570\u636e\u8d4b\u503c\u7ed9\u8f93\u51fa\u53c2\u6570e\n    Q-&gt;front = p-&gt;next;      \/\/ \u5c06\u961f\u5934\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u5b9e\u73b0\u961f\u5934\u51fa\u961f\n\n    \/\/ \u5982\u679c\u5220\u9664\u7684\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9(\u5373\u5220\u9664\u540e\u961f\u5217\u53d8\u7a7a)\uff0c\u5219rear\u4e5f\u9700\u8981\u7f6e\u4e3aNULL\n    if(Q-&gt;front == NULL) { \n        Q-&gt;rear = NULL;    \n    }\n\n    free(p);                 \/\/ \u91ca\u653e\u88ab\u5220\u9664\u8282\u70b9\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\n    printf(\"\u5143\u7d20 %d \u51fa\u961f\u6210\u529fn\", *e);  \/\/ \u8f93\u51fa\u51fa\u961f\u6210\u529f\u7684\u63d0\u793a\u4fe1\u606f\n}<\/code><\/pre>\n<p>\u5728\u51fa\u961f\u7684\u64cd\u4f5c\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5224\u5bf9\u7a7a\uff0c\u5f53\u961f\u4f0d\u4e0d\u4e3a\u7a7a\u65f6\u6211\u4eec\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c<br \/>\n\u5148\u5b9a\u4e49\u4e00\u4e2a*p\u6307\u9488\u6307\u5411\u5bf9\u5934\uff0c\u968f\u540e\u4fdd\u5b58\u4e0b\u5bf9\u5934\u7684\u6570\u636e\uff0c\u7136\u540e\u961f\u5934\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u5982\u679c\u5220\u9664\u7684\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\uff0c\u5219\u961f\u5c3e\u6307\u9488\u4e5f\u7f6e\u4e3aNULL<br \/>\n\u6700\u540e\u91ca\u653e\u8981\u5220\u9664\u7684\u8282\u70b9<br \/>\n<strong>\u6ce8\u610f\uff1a<\/strong>\u5728\u5220\u9664\u65f6\uff0c\u6211\u4eec\u52a1\u5fc5\u8981\u5148\u5224\u65ad\u662f\u5426\u961f\u7a7a\uff0c\u5426\u5219\u4f1a\u51fa\u95ee\u9898\uff01\uff01\uff01<\/p>\n<h2>4.\u53d6\u961f\u5934<\/h2>\n<pre><code class=\"language-C\">void getHead(LinkQueue Q, int *e){\n    \/\/\u5224\u961f\u7a7a\n    if(Q == NULL || Q-&gt;front == NULL){\n        printf(\"\u961f\u5217\u4e3a\u7a7a\uff01n\");\n    }\n    *e = Q-&gt;front-&gt;data;\n    printf(\"\u961f\u5934\u5143\u7d20\u4e3a\uff1a%dn\", *e);\n}<\/code><\/pre>\n<h3>5.\u904d\u5386<\/h3>\n<pre><code class=\"language-C\">void traverse(LinkQueue Q){\n    if(Q == NULL || Q-&gt;front == NULL){\n        printf(\"\u961f\u5217\u4e3a\u7a7a\uff01n\");\n    }\n    for(QNode *p = Q-&gt;front; p != NULL; p = p-&gt;next){\n        printf(\"%d \", p-&gt;data);\n        printf(\"n\");\n    }\n}<\/code><\/pre>\n<p>\u5728for\u5faa\u73af\u904d\u5386\u4e2d\uff0c\u6211\u4eec\u4ece\u961f\u5934\u5f00\u59cb\uff0c\u901a\u8fc7\u6307\u9488\u57df\u4f9d\u6b21\u904d\u5386\uff0c\u76f4\u81f3\u904d\u5386\u81f3\u961f\u5c3e\u6307\u5411NULL<\/p>\n<p><em>\u5199\u5230\u8fd9\u91cc\u7684\u65f6\u5019\u7a81\u7136\u60f3\u5230\u6211\u4e0d\u5e94\u8be5\u7528int\u6765\u5b9a\u4e49\u7136\u540ereturn 0\/return 1\u7684\uff0c\u56e0\u4e3a\u8fd9\u91cc\u662f\u4e00\u4e2a\u4e2a\u65b9\u6cd5\u4ee3\u8868\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5904\u7406\u90fd\u662f\u5728\u65b9\u6cd5\u4e2d\u7684\uff0c\u6240\u4ee5\u8fd4\u56de\u503c\u5e94\u8be5\u8fd4\u56devoid<\/em><\/p>\n<h3>\u9500\u6bc1\u961f\u5217<\/h3>\n<pre><code class=\"language-C\">void destroy(LinkQueue Q) {\n    if(Q == NULL) {\n        printf(\"\u961f\u5217\u6307\u9488\u4e3a\u7a7a\uff0c\u65e0\u9700\u9500\u6bc1n\");\n        return;\n    }\n\n    \/\/ \u5faa\u73af\u5220\u9664\u6240\u6709\u8282\u70b9\n    while(Q-&gt;front != NULL) {\n        QNode *temp = Q-&gt;front;      \/\/ \u4fdd\u5b58\u5f53\u524d\u961f\u5934\u8282\u70b9\n        Q-&gt;front = Q-&gt;front-&gt;next;   \/\/ \u79fb\u52a8\u961f\u5934\u6307\u9488\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\n        free(temp);                  \/\/ \u91ca\u653e\u5f53\u524d\u8282\u70b9\u5185\u5b58\n    }\n\n    \/\/ \u961f\u5217\u4e3a\u7a7a\u65f6\uff0crear\u4e5f\u5e94\u8be5\u4e3aNULL\n    Q-&gt;rear = NULL;\n\n    printf(\"\u961f\u5217\u9500\u6bc1\u6210\u529f\uff01n\");\n}<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u7efc\u4e0a\u4fbf\u662f\u7b80\u5355\u961f\u5217\u7684\u4e00\u4e9b\u7b80\u5355\u7684\u589e\u5220\u67e5\u6539\u7684\u4e00\u4e9b\u64cd\u4f5c\u4e86\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u4e2a\u5faa\u73af\u961f\u5217\u548c\u5176\u4ed6\u51e0\u4e2a\uff0c\u53ef\u80fd\u8981\u6401\u7f6e\u4e00\u4e0b\uff0c\u56e0\u4e3a\u76ee\u524d\u9119\u4eba\u5bf9\u961f\u5217\u8fd8\u6709\u4e00\u4e9b\u4e0d\u592a\u6e05\u695a\u7684\uff0c\u6240\u4ee5\u5c31\u5148\u5230\u8fd9\u91cc\u4e86\uff0c\u540e\u9762\u4f1a\u5c3d\u5feb\u8865\u4e0a\u7684<br \/>\n\u5b8c\u6574\u4ee3\u7801\u53ef\u4ee5\u5728\u540c\u6587\u4ef6\u5939\u4e0b\u7684.\/queque1.c\u4e2d\u627e\u5230,\u535a\u5ba2\u4f1a\u628a\u4ee3\u7801\u9644\u5728\u8bc4\u8bba\u533a<br \/>\n\u611f\u8c22\u652f\u6301\uff01<\/p>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u961f\u5217 \u4f55\u4e3a \u961f\u5217\u548c\u6808\u76f8\u53cd\uff0c\u9057\u5740\u4e2d\u5148\u8fdb\u5148\u51fa\u7684\u7ebf\u6027\u8868\uff0c\u4ed6\u7684\u5165\u961f\u4e0e\u51fa\u961f\u5206\u522b\u5728\u961f\u5217\u7684\u4fe9\u5934\uff0c\u5373\u53ea\u5141\u8bb8\u4e00\u7aef\u63d2\u5165\u5143\u7d20\uff0c\u53e6\u4e00\u7aef [&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-305","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\/305","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=305"}],"version-history":[{"count":0,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=\/wp\/v2\/posts\/305\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xunfen.cloud\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}