{"id":79,"date":"2024-09-05T15:23:21","date_gmt":"2024-09-05T07:23:21","guid":{"rendered":"https:\/\/cnsn.rr.nu\/?p=79"},"modified":"2024-09-05T15:23:21","modified_gmt":"2024-09-05T07:23:21","slug":"c%e8%af%ad%e8%a8%80%e4%b8%ad%e7%9a%84%e5%be%aa%e7%8e%af%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/blog.cnsn.fun\/?p=79","title":{"rendered":"C\u8bed\u8a00\u4e2d\u7684\u5faa\u73af\u94fe\u8868"},"content":{"rendered":"<p>\u5faa\u73af\u94fe\u8868\uff08Circular Linked List\uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u94fe\u8868\u5f62\u5f0f\uff0c\u5b83\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u4e0d\u662f\u6307\u5411 <code>NULL<\/code>\uff0c\u800c\u662f\u6307\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\uff0c\u8fd9\u6837\u94fe\u8868\u5f62\u6210\u4e00\u4e2a\u73af\uff0c\u53ef\u4ee5\u4ece\u4efb\u610f\u8282\u70b9\u5f00\u59cb\u8fdb\u884c\u904d\u5386\u3002\u5728\u5faa\u73af\u94fe\u8868\u4e2d\uff0c\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u901a\u8fc7\u6307\u9488\u8fde\u63a5\uff0c\u56e0\u6b64\u53ef\u4ee5\u5faa\u73af\u5730\u8bbf\u95ee\u94fe\u8868\u4e2d\u7684\u6240\u6709\u8282\u70b9\u3002<\/p>\n<p>\u5faa\u73af\u94fe\u8868\u65e2\u53ef\u4ee5\u662f <strong>\u5355\u5411\u5faa\u73af\u94fe\u8868<\/strong>\uff0c\u4e5f\u53ef\u4ee5\u662f <strong>\u53cc\u5411\u5faa\u73af\u94fe\u8868<\/strong>\uff0c\u4f46\u5355\u5411\u5faa\u73af\u94fe\u8868\u66f4\u5e38\u89c1\u3002<\/p>\n<h3>\u5faa\u73af\u94fe\u8868\u7684\u6982\u5ff5<\/h3>\n<ul>\n<li><strong>\u8282\u70b9 (Node)<\/strong>\uff1a\u94fe\u8868\u7684\u57fa\u672c\u5355\u5143\uff0c\u5305\u542b\u6570\u636e\u548c\u6307\u9488\u3002<\/li>\n<li><strong>\u5934\u8282\u70b9 (Head)<\/strong>\uff1a\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\u5934\u8282\u70b9\uff0c\u800c\u4e0d\u662f <code>NULL<\/code>\u3002<\/li>\n<li><strong>\u5faa\u73af\u7279\u6027<\/strong>\uff1a\u5c3e\u8282\u70b9\u7684\u6307\u9488\u6307\u5411\u5934\u8282\u70b9\uff0c\u4f7f\u94fe\u8868\u5f62\u6210\u4e00\u4e2a\u5faa\u73af\u3002<\/li>\n<li><strong>\u5355\u5411\u5faa\u73af\u94fe\u8868<\/strong>\uff1a\u8282\u70b9\u53ea\u5305\u542b\u4e00\u4e2a\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u3002<\/li>\n<li><strong>\u53cc\u5411\u5faa\u73af\u94fe\u8868<\/strong>\uff1a\u8282\u70b9\u5305\u542b\u6307\u5411\u524d\u4e00\u4e2a\u548c\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u4e24\u4e2a\u6307\u9488\u3002<\/li>\n<\/ul>\n<h3>\u5faa\u73af\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\u7684\u5faa\u73af\u94fe\u8868\u3002<\/li>\n<li><strong>\u63d2\u5165\u8282\u70b9<\/strong>\uff1a\u5728\u94fe\u8868\u5934\u90e8\u3001\u5c3e\u90e8\u6216\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u8282\u70b9\u3002<\/li>\n<li><strong>\u5220\u9664\u8282\u70b9<\/strong>\uff1a\u5220\u9664\u6307\u5b9a\u7684\u8282\u70b9\u3002<\/li>\n<li><strong>\u904d\u5386\u94fe\u8868<\/strong>\uff1a\u7531\u4e8e\u94fe\u8868\u662f\u5faa\u73af\u7684\uff0c\u904d\u5386\u8fc7\u7a0b\u4e2d\u9700\u8981\u6ce8\u610f\u907f\u514d\u65e0\u9650\u5faa\u73af\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\u5faa\u73af\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\u5355\u5411\u5faa\u73af\u94fe\u8868\u7684\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) {\n        printf(&quot;\u5185\u5b58\u5206\u914d\u5931\u8d25\\n&quot;);\n        exit(1);\n    }\n    newNode-&gt;data = data;  \/\/ \u8bbe\u7f6e\u8282\u70b9\u6570\u636e\n    newNode-&gt;next = newNode;  \/\/ \u521d\u59cb\u5316\u65f6\uff0c\u8282\u70b9\u6307\u5411\u81ea\u8eab\u5f62\u6210\u5faa\u73af\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\n    if (*head == NULL) {  \/\/ \u5982\u679c\u94fe\u8868\u4e3a\u7a7a\n        *head = newNode;  \/\/ \u65b0\u8282\u70b9\u5c31\u662f\u5934\u8282\u70b9\n        newNode-&gt;next = newNode;  \/\/ \u65b0\u8282\u70b9\u6307\u5411\u81ea\u8eab\uff0c\u5f62\u6210\u5faa\u73af\n        return;\n    } \n        struct Node* temp = *head;\n\n        \/\/ \u627e\u5230\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\n        while (temp-&gt;next != *head) {\n            temp = temp-&gt;next;\n        }\n\n        \/\/ \u63d2\u5165\u65b0\u8282\u70b9\u5230\u5934\u90e8\n        temp-&gt;next = newNode;  \/\/ \u5c3e\u8282\u70b9\u6307\u5411\u65b0\u8282\u70b9\n        newNode-&gt;next = *head;  \/\/ \u65b0\u8282\u70b9\u6307\u5411\u539f\u5934\u8282\u70b9\n        *head = newNode;        \/\/ \u66f4\u65b0\u5934\u8282\u70b9\n\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\n    if (*head == NULL) {  \/\/ \u5982\u679c\u94fe\u8868\u4e3a\u7a7a\n        *head = newNode;  \/\/ \u65b0\u8282\u70b9\u5c31\u662f\u5934\u8282\u70b9\n        newNode-&gt;next = newNode;  \/\/ \u65b0\u8282\u70b9\u6307\u5411\u81ea\u8eab\uff0c\u5f62\u6210\u5faa\u73af\n    } else {\n        struct Node* temp = *head;\n\n        \/\/ \u627e\u5230\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\n        while (temp-&gt;next != *head) {\n            temp = temp-&gt;next;\n        }\n\n        \/\/ \u63d2\u5165\u65b0\u8282\u70b9\u5230\u5c3e\u90e8\n        temp-&gt;next = newNode;  \/\/ \u5c3e\u8282\u70b9\u6307\u5411\u65b0\u8282\u70b9\n        newNode-&gt;next = *head;  \/\/ \u65b0\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\uff0c\u5f62\u6210\u5faa\u73af\n    }\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\u503c\u4e3a data \u7684\u8282\u70b9\nvoid deleteNode(struct Node** head, int data) {\n    if (*head == NULL) return;  \/\/ \u7a7a\u94fe\u8868\n\n    struct Node* temp = *head;\n    struct Node* prev = NULL;\n\n    \/\/ \u5982\u679c\u5934\u8282\u70b9\u5c31\u662f\u8981\u5220\u9664\u7684\u8282\u70b9\n    if ((*head)-&gt;data == data) {\n        if ((*head)-&gt;next == *head) {  \/\/ \u94fe\u8868\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\n            free(*head);\n            *head = NULL;\n            return;\n        }\n\n        \/\/ \u627e\u5230\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\n        while (temp-&gt;next != *head) {\n            temp = temp-&gt;next;\n        }\n\n        temp-&gt;next = (*head)-&gt;next;  \/\/ \u6700\u540e\u4e00\u4e2a\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n        free(*head);  \/\/ \u91ca\u653e\u539f\u5934\u8282\u70b9\n        *head = temp-&gt;next;  \/\/ \u66f4\u65b0\u5934\u8282\u70b9\n        return;\n    }\n\n    \/\/ \u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\n    prev = *head;\n    temp = (*head)-&gt;next;\n    while (temp != *head &amp;&amp; temp-&gt;data != data) {\n        prev = temp;\n        temp = temp-&gt;next;\n    }\n\n    \/\/ \u5982\u679c\u627e\u5230\u8981\u5220\u9664\u7684\u8282\u70b9\n    if (temp-&gt;data == data) {\n        prev-&gt;next = temp-&gt;next;\n        free(temp);\n    }\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    if (head == NULL) {\n        printf(&quot;\u94fe\u8868\u4e3a\u7a7a\\n&quot;);\n        return;\n    }\n\n    struct Node* temp = head;\n    do {\n        printf(&quot;%d -&gt; &quot;, temp-&gt;data);\n        temp = temp-&gt;next;\n    } while (temp != head);  \/\/ \u5faa\u73af\u76f4\u5230\u518d\u6b21\u56de\u5230\u5934\u8282\u70b9\n    printf(&quot;(\u56de\u5230\u5934\u8282\u70b9)\\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    if (*head == NULL) return;\n\n    struct Node* current = *head;\n    struct Node* nextNode;\n\n    do {\n        nextNode = current-&gt;next;  \/\/ \u4fdd\u5b58\u4e0b\u4e00\u4e2a\u8282\u70b9\n        free(current);             \/\/ \u91ca\u653e\u5f53\u524d\u8282\u70b9\n        current = nextNode;        \/\/ \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\n    } while (current != *head);    \/\/ \u5faa\u73af\u76f4\u5230\u518d\u6b21\u56de\u5230\u5934\u8282\u70b9\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;\u5faa\u73af\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>\u5faa\u73af\u94fe\u8868\u5185\u5bb9: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; (\u56de\u5230\u5934\u8282\u70b9)\n\u5220\u9664\u8282\u70b93\u540e\u7684\u94fe\u8868: 1 -&gt; 2 -&gt; 4 -&gt; 5 -&gt; (\u56de\u5230\u5934\u8282\u70b9)<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<ul>\n<li><strong>\u5faa\u73af\u94fe\u8868<\/strong> \u4e0e\u666e\u901a\u94fe\u8868\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5176\u5faa\u73af\u7279\u6027\uff1a\u5c3e\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\uff0c\u5f62\u6210\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u3002<\/li>\n<li>\u8fd9\u79cd\u7ed3\u6784\u5141\u8bb8\u4ece\u4efb\u610f\u8282\u70b9\u5f00\u59cb\u904d\u5386\uff0c\u5e76\u4e14\u80fd\u591f\u7b80\u5316\u67d0\u4e9b\u9700\u8981\u5faa\u73af\u64cd\u4f5c\u7684\u7b97\u6cd5\u3002<\/li>\n<li>\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u5faa\u73af\u94fe\u8868\u5728\u5904\u7406\u94fe\u5c3e\u65f6\u66f4\u52a0\u65b9\u4fbf\uff08\u4e0d\u9700\u8981\u989d\u5916\u5904\u7406\u5c3e\u8282\u70b9\u7684 <code>next<\/code> \u6307\u9488\u4e3a <code>NULL<\/code> \u7684\u60c5\u51b5\uff09\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5faa\u73af\u94fe\u8868\uff08Circular Linked List\uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u94fe\u8868\u5f62\u5f0f\uff0c\u5b83\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u4e0d\u662f\u6307\u5411 NU [&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,28,34],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-28","tag-34"],"_links":{"self":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/79","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=79"}],"version-history":[{"count":0,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=\/wp\/v2\/posts\/79\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cnsn.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}