{"id":77,"date":"2024-09-05T15:21:33","date_gmt":"2024-09-05T07:21:33","guid":{"rendered":"https:\/\/cnsn.rr.nu\/?p=77"},"modified":"2024-09-05T15:21:33","modified_gmt":"2024-09-05T07:21:33","slug":"c%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e5%8f%8c%e5%90%91%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/blog.cnsn.fun\/?p=77","title":{"rendered":"C\u8bed\u8a00\u4e2d\u7684\u53cc\u5411\u94fe\u8868"},"content":{"rendered":"<p>\u53cc\u5411\u94fe\u8868\uff08Doubly Linked List\uff09\u662f\u94fe\u8868\u7684\u4e00\u79cd\uff0c\u5b83\u4e0e\u5355\u5411\u94fe\u8868\u7684\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u6bcf\u4e2a\u8282\u70b9\u4e0d\u4ec5\u6709\u4e00\u4e2a\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff08<code>next<\/code>\uff09\uff0c\u8fd8\u5305\u542b\u4e00\u4e2a\u6307\u5411\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff08<code>prev<\/code>\uff09\u3002\u8fd9\u79cd\u7ed3\u6784\u5141\u8bb8\u6211\u4eec\u5728\u94fe\u8868\u4e2d\u5411\u524d\u548c\u5411\u540e\u8fdb\u884c\u904d\u5386\uff0c\u589e\u52a0\u4e86\u7075\u6d3b\u6027\u3002<\/p>\n<h3>\u53cc\u5411\u94fe\u8868\u7684\u6982\u5ff5<\/h3>\n<ul>\n<li><strong>\u8282\u70b9 (Node)<\/strong>\uff1a\u53cc\u5411\u94fe\u8868\u7684\u57fa\u672c\u5355\u5143\uff0c\u5305\u542b\u6570\u636e\u57df\u548c\u4e24\u4e2a\u6307\u9488\uff0c\u5206\u522b\u6307\u5411\u524d\u4e00\u4e2a\u8282\u70b9\u548c\u540e\u4e00\u4e2a\u8282\u70b9\u3002<\/li>\n<li><strong>\u5934\u8282\u70b9 (Head)<\/strong>\uff1a\u6307\u5411\u94fe\u8868\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\uff0c<code>head-&gt;prev<\/code>\u4e3a <code>NULL<\/code>\u3002<\/li>\n<li><strong>\u5c3e\u8282\u70b9 (Tail)<\/strong>\uff1a\u6307\u5411\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\uff0c<code>tail-&gt;next<\/code>\u4e3a <code>NULL<\/code>\u3002<\/li>\n<li><strong>\u524d\u5411\u6307\u9488 (Prev)<\/strong>\uff1a\u6307\u5411\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff0c\u5934\u8282\u70b9\u7684 <code>prev<\/code> \u4e3a <code>NULL<\/code>\u3002<\/li>\n<li><strong>\u540e\u5411\u6307\u9488 (Next)<\/strong>\uff1a\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff0c\u5c3e\u8282\u70b9\u7684 <code>next<\/code> \u4e3a <code>NULL<\/code>\u3002<\/li>\n<\/ul>\n<p>\u53cc\u5411\u94fe\u8868\u63d0\u4f9b\u4e86\u6bd4\u5355\u5411\u94fe\u8868\u66f4\u591a\u7684\u64cd\u4f5c\u7075\u6d3b\u6027\uff0c\u4f46\u4e5f\u9700\u8981\u66f4\u591a\u7684\u5185\u5b58\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u8282\u70b9\u9664\u4e86\u6570\u636e\u548c <code>next<\/code> \u6307\u9488\u5916\uff0c\u8fd8\u9700\u8981\u5b58\u50a8\u4e00\u4e2a <code>prev<\/code> \u6307\u9488\u3002<\/p>\n<h3>\u53cc\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\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\u5220\u9664\u6307\u5b9a\u4f4d\u7f6e\u7684\u8282\u70b9\u3002<\/li>\n<li><strong>\u904d\u5386\u94fe\u8868<\/strong>\uff1a\u53ef\u4ee5\u4ece\u5934\u8282\u70b9\u6216\u5c3e\u8282\u70b9\u8fdb\u884c\u904d\u5386\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>\u53cc\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\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\nstruct Node {\n    int data;           \/\/ \u6570\u636e\u57df\n    struct Node* prev;  \/\/ \u6307\u5411\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\n    struct Node* next;  \/\/ \u6307\u5411\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\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    {\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;prev = NULL;  \/\/ \u521d\u59cb\u5316\u524d\u4e00\u4e2a\u8282\u70b9\u6307\u9488\u4e3a NULL\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    if (*head == NULL) {  \/\/ \u5982\u679c\u94fe\u8868\u4e3a\u7a7a\n        *head = newNode;  \/\/ \u65b0\u8282\u70b9\u5c31\u662f\u5934\u8282\u70b9\n        return;\n    }\n    (*head)-&gt;prev = newNode;  \/\/ \u5934\u8282\u70b9\u7684\u524d\u6307\u9488\u6307\u5411\u65b0\u8282\u70b9\n    newNode-&gt;next = *head;    \/\/ \u65b0\u8282\u70b9\u7684\u540e\u6307\u9488\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);  \/\/ \u521b\u5efa\u65b0\u8282\u70b9\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\u7684\u540e\u6307\u9488\u6307\u5411\u65b0\u8282\u70b9\n    newNode-&gt;prev = temp;  \/\/ \u65b0\u8282\u70b9\u7684\u524d\u6307\u9488\u6307\u5411\u6700\u540e\u4e00\u4e2a\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\">\/\/ \u5220\u9664\u94fe\u8868\u4e2d\u503c\u4e3a data \u7684\u8282\u70b9\nvoid deleteNode(struct Node** head, int data) {\n\n    if (*head == NULL)\n    {\n        printf(&quot;\u8282\u70b9\u4e3a\u7a7a\uff0c\u65e0\u6cd5\u5220\u9664\u8282\u70b9!&quot;);\n        return;\n    }\n\n    struct Node* temp = *head;\n\n    \/\/ \u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\n    while (temp != NULL &amp;&amp; temp-&gt;data != data) {\n        temp = temp-&gt;next;\n    }\n\n    \/\/ \u5982\u679c\u672a\u627e\u5230\n    if (temp == NULL) return;\n\n    \/\/ \u5982\u679c\u8981\u5220\u9664\u7684\u662f\u5934\u8282\u70b9\n    if (*head == temp) {\n        *head = temp-&gt;next;\n    }\n\n    \/\/ \u5982\u679c\u5b58\u5728\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u66f4\u65b0\u5b83\u7684 prev \u6307\u9488\n    if (temp-&gt;next != NULL) {\n        temp-&gt;next-&gt;prev = temp-&gt;prev;\n    }\n\n    \/\/ \u5982\u679c\u5b58\u5728\u524d\u4e00\u4e2a\u8282\u70b9\uff0c\u66f4\u65b0\u5b83\u7684 next \u6307\u9488\n    if (temp-&gt;prev != NULL) {\n        temp-&gt;prev-&gt;next = temp-&gt;next;\n    }\n\n    free(temp);  \/\/ \u91ca\u653e\u8282\u70b9\u5185\u5b58\n}<\/code><\/pre>\n<ol start=\"5\">\n<li><strong>\u4ece\u5934\u5230\u5c3e\u904d\u5386\u94fe\u8868<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/\/ \u4ece\u5934\u5230\u5c3e\u904d\u5386\u94fe\u8868\u5e76\u6253\u5370\u6570\u636e\nvoid printListForward(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>\u4ece\u5c3e\u5230\u5934\u904d\u5386\u94fe\u8868<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-c\">\/\/ \u4ece\u5c3e\u5230\u5934\u904d\u5386\u94fe\u8868\u5e76\u6253\u5370\u6570\u636e\nvoid printListBackward(struct Node* head) {\n    struct Node* temp = head;\n    if (temp == NULL) return;  \/\/ \u7a7a\u94fe\u8868\u4e0d\u9700\u8981\u904d\u5386\n\n    \/\/ \u79fb\u52a8\u5230\u5c3e\u8282\u70b9\n    while (temp-&gt;next != NULL) {\n        temp = temp-&gt;next;\n    }\n\n    \/\/ \u4ece\u5c3e\u5230\u5934\u904d\u5386\n    while (temp != NULL) {\n        printf(&quot;%d -&gt; &quot;, temp-&gt;data);\n        temp = temp-&gt;prev;\n    }\n    printf(&quot;NULL\\n&quot;);\n}<\/code><\/pre>\n<ol start=\"7\">\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    \/\/ \u6b63\u5411\u904d\u5386\u94fe\u8868\n    printf(&quot;\u6b63\u5411\u904d\u5386\u94fe\u8868: &quot;);\n    printListForward(head);\n\n    \/\/ \u53cd\u5411\u904d\u5386\u94fe\u8868\n    printf(&quot;\u53cd\u5411\u904d\u5386\u94fe\u8868: &quot;);\n    printListBackward(head);\n\n    \/\/ \u5220\u9664\u8282\u70b9\n    deleteNode(&amp;head, 3);\n    printf(&quot;\u5220\u9664\u8282\u70b93\u540e\u7684\u6b63\u5411\u94fe\u8868: &quot;);\n    printListForward(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>\u6b63\u5411\u904d\u5386\u94fe\u8868: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; NULL\n\u53cd\u5411\u904d\u5386\u94fe\u8868: 5 -&gt; 4 -&gt; 3 -&gt; 2 -&gt; 1 -&gt; NULL\n\u5220\u9664\u8282\u70b93\u540e\u7684\u6b63\u5411\u94fe\u8868: 1 -&gt; 2 -&gt; 4 -&gt; 5 -&gt; NULL<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<ul>\n<li><strong>\u53cc\u5411\u94fe\u8868<\/strong> \u76f8\u8f83\u4e8e\u5355\u5411\u94fe\u8868\u589e\u52a0\u4e86\u6307\u5411\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4ece\u4efb\u610f\u8282\u70b9\u5411\u524d\u6216\u5411\u540e\u904d\u5386\u94fe\u8868\u3002<\/li>\n<li>\u5b83\u7684\u4f18\u70b9\u5728\u4e8e\u63d0\u4f9b\u4e86\u66f4\u65b9\u4fbf\u7684\u53cc\u5411\u904d\u5386\u548c\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff08\u4e0d\u9700\u8981\u5bfb\u627e\u524d\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u4f46\u7f3a\u70b9\u662f\u9700\u8981\u989d\u5916\u7684\u5b58\u50a8\u7a7a\u95f4\u6765\u5b58\u50a8 <code>prev<\/code> \u6307\u9488\uff0c\u5e76\u4e14\u63d2\u5165\u3001\u5220\u9664\u65f6\u9700\u8981\u540c\u65f6\u7ef4\u62a4 <code>prev<\/code> \u548c <code>next<\/code> \u6307\u9488\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u53cc\u5411\u94fe\u8868\uff08Doubly Linked List\uff09\u662f\u94fe\u8868\u7684\u4e00\u79cd\uff0c\u5b83\u4e0e\u5355\u5411\u94fe\u8868\u7684\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u6bcf\u4e2a\u8282\u70b9\u4e0d\u4ec5\u6709\u4e00\u4e2a\u6307 [&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,27,34],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-27","tag-34"],"_links":{"self":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/77","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=77"}],"version-history":[{"count":0,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}