{"id":118,"date":"2022-02-26T22:01:42","date_gmt":"2022-02-26T14:01:42","guid":{"rendered":"https:\/\/blog.lickoo.fun\/?p=118"},"modified":"2022-03-18T21:32:52","modified_gmt":"2022-03-18T13:32:52","slug":"%e4%b8%80%e4%b8%aacpp%e9%93%be%e8%a1%a8%e7%9a%84%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/blog.lickoo.fun\/?p=118","title":{"rendered":"\u4e00\u4e2aCpp\u94fe\u8868\u7684\u5b66\u4e60"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">struct node{\n  int data;\n  struct node* next;\n};\n<\/code><\/pre>\n\n\n\n<h2 id=\"\u524d\u7f6e\u63d2\u5165\">\u524d\u7f6e\u63d2\u5165<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">void insertNewNode(struct node* headPos,int data){\n  struct node* newNodePos = (struct node*)malloc(sizeof(struct node*));\n\/\/malloc\u4f1a\u8fd4\u56devoid*\u7c7b\u578b\u7684\u5730\u5740\uff0c\u6545\u8bd5\u7528\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\n  (*newNodePos).data = data;\n\/\/\u7b49\u540c\u4e8e newNodePos-&gt;data =data;  \u5bf9\u65b0\u8282\u70b9\u7684data\u8fdb\u884c\u8d4b\u503c\n  newNodePos-&gt;next = (*headPos).next;\n\/\/\u5c06\u5934\u7ed3\u70b9\u7684next\u5730\u5740\u8d4b\u503c\u7ed9\u65b0\u8282\u70b9\n  (*headPos).next = newNodePos;\n\/\/\u5c06\u65b0\u8282\u70b9\u7684\u5730\u5740\u8d4b\u503c\u7ed9\u5934\u8282\u70b9\u7684next\n}\n<\/code><\/pre>\n\n\n\n<h2 id=\"\u540e\u7f6e\u63d2\u5165\">\u540e\u7f6e\u63d2\u5165<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">void myinsertTail(struct node *  headPos , int insData ){\n\t\/*\u5728\u6b64\u5904\u5b8c\u6210\u4efb\u52a1\uff0c\u5728head\u4e3a\u8868\u5934\u7684\u5355\u94fe\u8868\u8868\u5c3e\u63d2\u5165\u6570\u636e\u5143\u7d20insData*\/\n\t\/\/begin\n    struct node* p = (struct node*)malloc(sizeof(struct node*));\n    p-&gt;data = insData;\n    p-&gt;next = NULL;\n    \n    struct node* q;\n    q = headPos;\n    while(q-&gt;next!=NULL) \n      q = q-&gt;next;\n    q-&gt;next = p ;\n\t\n\t\/\/\u4e00\u76f4\u67e5\u627e\uff0c\u76f4\u5230\u5352\u540e\u4e00\u4e2a\u4e3aNULL\uff0c\u7136\u540e\u5bf9NULL\u8fdb\u884c\u8d4b\u503c\n<\/code><\/pre>\n\n\n\n<h2 id=\"\u6253\u5370\u51fd\u6570\">\u6253\u5370\u51fd\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">void myprintList(struct node *L){\n     \/*\u5728\u6b64\u5904\u5b8c\u6210\u4efb\u52a1\uff0c\u8f93\u51fahead\u4e3a\u8868\u5934\u94fe\u8868\u4e2d\u7684\u6570\u636e\uff0c\u6bcf\u8f93\u51fa\u4e00\u4e2a\u6570\u636e\u6362\u4e00\u884c*\/\n\t\/\/begin\n    struct node* p = L; \n    while(p-&gt;next != NULL)\n    {\n        p=p-&gt;next;\n        printf(\"%d\\n\",p-&gt;data);\n    }\n\t\/\/end \n    \n}\n<\/code><\/pre>\n\n\n\n<h2 id=\"\u4e3b\u51fd\u6570\">\u4e3b\u51fd\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int main(){\n\tstruct node head;\n\thead.data = -1;\n\thead.next = NULL;\n\n\tmyinsertTail(&amp;head,3);\n\tmyinsertTail(&amp;head,4);\n\tmyinsertTail(&amp;head,5);\n\t\/\/cout&lt;&lt;head.data&lt;&lt;endl&lt;&lt;(*head.next).data&lt;&lt;endl;\n\tmyprintList(&amp;head);\n    \treturn 0;\n}\n    \t\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7f6e\u63d2\u5165 \u540e\u7f6e\u63d2\u5165 \u6253\u5370\u51fd\u6570 \u4e3b\u51fd\u6570<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/posts\/118"}],"collection":[{"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=118"}],"version-history":[{"count":2,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":226,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions\/226"}],"wp:attachment":[{"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lickoo.fun\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}