{"id":75,"date":"2024-09-05T15:20:44","date_gmt":"2024-09-05T07:20:44","guid":{"rendered":"https:\/\/cnsn.rr.nu\/?p=75"},"modified":"2024-09-05T15:20:44","modified_gmt":"2024-09-05T07:20:44","slug":"c%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e5%8d%95%e5%90%91%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/blog.cnsn.fun\/?p=75","title":{"rendered":"C\u8bed\u8a00\u4e2d\u7684\u5355\u5411\u94fe\u8868"},"content":{"rendered":"<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u5355\u5411\u94fe\u8868\u662f\u4e00\u79cd\u52a8\u6001\u6570\u636e\u7ed3\u6784\uff0c\u7531\u591a\u4e2a\u8282\u70b9\uff08Node\uff09\u7ec4\u6210\u3002\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e24\u4e2a\u90e8\u5206\uff1a\u4e00\u4e2a\u662f\u6570\u636e\u90e8\u5206\uff08\u901a\u5e38\u662f\u5b58\u50a8\u7684\u5b9e\u9645\u503c\uff09\uff0c\u53e6\u4e00\u4e2a\u662f\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u3002\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u5934\u8282\u70b9\uff08head\uff09\uff0c\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u6307\u5411<code>NULL<\/code>\uff0c\u8868\u793a\u94fe\u8868\u7684\u7ed3\u675f\u3002<\/p>\n<h3>\u5355\u5411\u94fe\u8868\u7684\u6982\u5ff5<\/h3>\n<ul>\n<li><strong>\u8282\u70b9 (Node)<\/strong>\uff1a\u94fe\u8868\u7684\u57fa\u672c\u5355\u5143\u3002\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u6570\u636e\u57df\u548c\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u3002<\/li>\n<li><strong>\u5934\u8282\u70b9 (Head)<\/strong>\uff1a\u94fe\u8868\u7684\u5165\u53e3\u70b9\uff0c\u6307\u5411\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u3002<\/li>\n<li><strong>\u5c3e\u8282\u70b9 (Tail)<\/strong>\uff1a\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\uff0c\u5b83\u7684\u6307\u9488\u6307\u5411 <code>NULL<\/code>\u3002<\/li>\n<li><strong>NULL \u6307\u9488<\/strong>\uff1a\u7528\u4e8e\u6807\u8bc6\u94fe\u8868\u7684\u7ed3\u675f\u3002<\/li>\n<\/ul>\n<p>\u5355\u5411\u94fe\u8868\u4e0e\u6570\u7ec4\u4e0d\u540c\uff0c\u94fe\u8868\u8282\u70b9\u7684\u5b58\u50a8\u4f4d\u7f6e\u5728\u5185\u5b58\u4e2d\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u5b83\u4eec\u901a\u8fc7\u6307\u9488\u76f8\u4e92\u8fde\u63a5\uff0c\u56e0\u6b64\u63d2\u5165\u6216\u5220\u9664\u8282\u70b9\u65f6\u4e0d\u9700\u8981\u79fb\u52a8\u5176\u4ed6\u8282\u70b9\uff0c\u4f46\u9700\u8981\u52a8\u6001\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\u3002<\/p>\n<h3>\u5355\u5411\u94fe\u8868\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<ul>\n<li><strong>\u521b\u5efa\u94fe\u8868<\/strong>\uff1a\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u94fe\u8868\u3002<\/li>\n<li><strong>\u63d2\u5165\u8282\u70b9<\/strong>\uff1a\u5728\u94fe\u8868\u7684\u5934\u90e8\u6216\u5c3e\u90e8\u63d2\u5165\u8282\u70b9\uff0c\u6216\u8005\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u3002<\/li>\n<li><strong>\u5220\u9664\u8282\u70b9<\/strong>\uff1a\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u6307\u5b9a\u4f4d\u7f6e\u7684\u8282\u70b9\u3002<\/li>\n<li><strong>\u904d\u5386\u94fe\u8868<\/strong>\uff1a\u4ece\u5934\u8282\u70b9\u5f00\u59cb\uff0c\u4f9d\u6b21\u8bbf\u95ee\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u3002<\/li>\n<li><strong>\u91ca\u653e\u94fe\u8868<\/strong>\uff1a\u91ca\u653e\u94fe\u8868\u4e2d\u52a8\u6001\u5206\u914d\u7684\u6240\u6709\u8282\u70b9\u5185\u5b58\u3002<\/li>\n<\/ul>\n<h3>\u5355\u5411\u94fe\u8868\u7684\u7ed3\u6784\u5b9a\u4e49<\/h3>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ \u5b9a\u4e49\u94fe\u8868\u8282\u70b9\u7ed3\u6784\nstruct Node {\n    int data;           \/\/ \u6570\u636e\u57df\n    struct Node* next;  \/\/ \u6307\u9488\u57df\uff0c\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\n};<\/code><\/pre>\n<h3>\u57fa\u672c\u64cd\u4f5c\u5b9e\u73b0<\/h3>\n<ol>\n<li><strong>\u521b\u5efa\u65b0\u8282\u70b9<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/\/ \u521b\u5efa\u65b0\u8282\u70b9\nstruct Node* createNode(int data) {\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); \/\/ \u5206\u914d\u5185\u5b58\n    if(newNode == NULL){\n        printf(&quot;\u7533\u8bf7\u5185\u5b58\u5931\u8d25\uff01&quot;);\n        exit(-1);\n    }\n    newNode-&gt;data = data; \/\/ \u8bbe\u7f6e\u8282\u70b9\u6570\u636e\n    newNode-&gt;next = NULL; \/\/ \u521d\u59cb\u5316\u4e0b\u4e00\u4e2a\u8282\u70b9\u6307\u9488\u4e3a NULL\n    return newNode;\n}<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u63d2\u5165\u8282\u70b9\u5230\u94fe\u8868\u5934\u90e8<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/\/ \u63d2\u5165\u8282\u70b9\u5230\u94fe\u8868\u5934\u90e8\nvoid insertAtHead(struct Node** head, int data) {\n    struct Node* newNode = createNode(data); \/\/ \u521b\u5efa\u65b0\u8282\u70b9\n    newNode-&gt;next = *head; \/\/ \u65b0\u8282\u70b9\u6307\u5411\u539f\u6765\u7684\u5934\u8282\u70b9\n    *head = newNode; \/\/ \u66f4\u65b0\u5934\u8282\u70b9\u4e3a\u65b0\u8282\u70b9\n}<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u5728\u94fe\u8868\u5c3e\u90e8\u63d2\u5165\u8282\u70b9<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/\/ \u63d2\u5165\u8282\u70b9\u5230\u94fe\u8868\u5c3e\u90e8\nvoid insertAtTail(struct Node** head, int data) {\n    struct Node* newNode = createNode(data);\n    if (*head == NULL) {  \/\/ \u5982\u679c\u94fe\u8868\u4e3a\u7a7a\n        *head = newNode;  \/\/ \u65b0\u8282\u70b9\u5c31\u662f\u5934\u8282\u70b9\n        return;\n    }\n    struct Node* temp = *head; \/\/ \u4e34\u65f6\u6307\u9488\u904d\u5386\u94fe\u8868\n    while (temp-&gt;next != NULL) {\n        temp = temp-&gt;next;  \/\/ \u79fb\u52a8\u5230\u6700\u540e\u4e00\u4e2a\u8282\u70b9\n    }\n    temp-&gt;next = newNode;  \/\/ \u6700\u540e\u4e00\u4e2a\u8282\u70b9\u6307\u5411\u65b0\u8282\u70b9\n}<\/code><\/pre>\n<ol start=\"4\">\n<li><strong>\u5220\u9664\u8282\u70b9<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/\/ \u4ece\u94fe\u8868\u4e2d\u5220\u9664\u503c\u4e3adata\u7684\u8282\u70b9\nvoid deleteNode(struct Node** head, int data) {\n    struct Node* temp = *head, *prev = NULL;\n\n    \/\/ \u68c0\u67e5\u94fe\u8868\u662f\u5426\u4e3a\u7a7a\n    if (temp == NULL) {\n        printf(&quot;\u94fe\u8868\u4e3a\u7a7a\uff0c\u6240\u4ee5\u8282\u70b9\u4e2d\u627e\u4e0d\u5230%s\\n&quot;, data);\n        return;\n    }\n\n    \/\/ \u5982\u679c\u5934\u8282\u70b9\u5c31\u662f\u8981\u5220\u9664\u7684\u8282\u70b9\n    if (temp != NULL &amp;&amp; temp-&gt;data == data) {\n        *head = temp-&gt;next; \/\/ \u5c06\u5934\u8282\u70b9\u66f4\u65b0\u4e3a\u4e0b\u4e00\u4e2a\u8282\u70b9\n        free(temp);         \/\/ \u91ca\u653e\u539f\u6765\u7684\u5934\u8282\u70b9\u5185\u5b58\n        return;\n    }\n\n    \/\/ \u904d\u5386\u94fe\u8868\u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\n    while (temp != NULL &amp;&amp; temp-&gt;data != data) {\n        prev = temp;\n        temp = temp-&gt;next;\n    }\n\n    \/\/ \u5982\u679c\u904d\u5386\u7ed3\u675f\u4e5f\u6ca1\u6709\u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\u6253\u5370\u63d0\u793a\u4fe1\u606f\n    if (temp == NULL) {\n        printf(&quot;\u5728\u8282\u70b9\u4e2d\uff0c\u627e\u4e0d\u5230%s\\n&quot;, data);\n        return;\n    }\n\n    prev-&gt;next = temp-&gt;next; \/\/ \u8df3\u8fc7\u8981\u5220\u9664\u7684\u8282\u70b9\n    free(temp);              \/\/ \u91ca\u653e\u8282\u70b9\u5185\u5b58\n}<\/code><\/pre>\n<ol start=\"5\">\n<li><strong>\u904d\u5386\u94fe\u8868<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/\/ \u904d\u5386\u94fe\u8868\u5e76\u6253\u5370\u6bcf\u4e2a\u8282\u70b9\u7684\u6570\u636e\nvoid printList(struct Node* head) {\n    struct Node* temp = head;\n    while (temp != NULL) {\n        printf(&quot;%d -&gt; &quot;, temp-&gt;data);\n        temp = temp-&gt;next;\n    }\n    printf(&quot;NULL\\n&quot;);\n}<\/code><\/pre>\n<ol start=\"6\">\n<li><strong>\u91ca\u653e\u94fe\u8868<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/\/ \u91ca\u653e\u94fe\u8868\u7684\u6240\u6709\u8282\u70b9\nvoid freeList(struct Node** head) {\n    struct Node* current = *head;\n    struct Node* next;\n\n    while (current != NULL) {\n        next = current-&gt;next; \/\/ \u4fdd\u5b58\u4e0b\u4e00\u4e2a\u8282\u70b9\n        free(current);        \/\/ \u91ca\u653e\u5f53\u524d\u8282\u70b9\n        current = next;       \/\/ \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\n    }\n\n    *head = NULL; \/\/ \u5934\u6307\u9488\u7f6e\u4e3a NULL\uff0c\u8868\u793a\u94fe\u8868\u5df2\u7a7a\n}<\/code><\/pre>\n<h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<pre><code class=\"language-c\">int main() {\n    struct Node* head = NULL;  \/\/ \u521d\u59cb\u5316\u7a7a\u94fe\u8868\n\n    \/\/ \u63d2\u5165\u6570\u636e\n    insertAtHead(&amp;head, 3);\n    insertAtHead(&amp;head, 2);\n    insertAtHead(&amp;head, 1);\n    insertAtTail(&amp;head, 4);\n    insertAtTail(&amp;head, 5);\n\n    \/\/ \u6253\u5370\u94fe\u8868\n    printf(&quot;\u94fe\u8868\u5185\u5bb9: &quot;);\n    printList(head);\n\n    \/\/ \u5220\u9664\u8282\u70b9\n    deleteNode(&amp;head, 3);\n    printf(&quot;\u5220\u9664\u8282\u70b93\u540e\u7684\u94fe\u8868: &quot;);\n    printList(head);\n\n    \/\/ \u91ca\u653e\u94fe\u8868\n    freeList(&amp;head);\n\n    return 0;\n}<\/code><\/pre>\n<h3>\u8fd0\u884c\u7ed3\u679c<\/h3>\n<pre><code>\u94fe\u8868\u5185\u5bb9: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; NULL\n\u5220\u9664\u8282\u70b93\u540e\u7684\u94fe\u8868: 1 -&gt; 2 -&gt; 4 -&gt; 5 -&gt; NULL<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u5355\u5411\u94fe\u8868\u662f\u4e00\u79cd\u5e38\u7528\u7684\u52a8\u6001\u6570\u636e\u7ed3\u6784\uff0c\u9002\u5408\u9891\u7e41\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u666f\u3002\u4e0e\u6570\u7ec4\u76f8\u6bd4\uff0c\u5b83\u53ef\u4ee5\u6709\u6548\u5229\u7528\u5185\u5b58\uff0c\u4f46\u56e0\u4e3a\u8282\u70b9\u5b58\u50a8\u5728\u4e0d\u8fde\u7eed\u7684\u5185\u5b58\u4e2d\uff0c\u67e5\u627e\u64cd\u4f5c\u9700\u8981\u4ece\u5934\u8282\u70b9\u904d\u5386\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u5355\u5411\u94fe\u8868\u662f\u4e00\u79cd\u52a8\u6001\u6570\u636e\u7ed3\u6784\uff0c\u7531\u591a\u4e2a\u8282\u70b9\uff08Node\uff09\u7ec4\u6210\u3002\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e24\u4e2a\u90e8\u5206\uff1a\u4e00\u4e2a\u662f\u6570\u636e\u90e8\u5206\uff08\u901a\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[14,26,34],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-26","tag-34"],"_links":{"self":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=75"}],"version-history":[{"count":0,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/75\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}