{"id":14734,"date":"2022-10-08T22:47:09","date_gmt":"2022-10-08T17:17:09","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/"},"modified":"2022-10-08T22:47:09","modified_gmt":"2022-10-08T17:17:09","slug":"solved-linked-list-private-pointers-c-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/","title":{"rendered":"[Solved] Linked List private pointers C++ [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-38235373\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"38235373\" data-parentid=\"38234616\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p><code>private<\/code> members, pointers or otherwise, are data that the object either does not want messed by anyone else with or wants to know if someone messed with them.<\/p>\n<p>Setter and getter methods allow access to the <code>private<\/code> member, but in a controlled manner. For example if you have a private integer that under no circumstances can ever be greater than ten, you can have code in the setter that checks for a caller trying to force the value out of range and reject the request.<\/p>\n<pre><code>bool setX( int newX)\n{\n    if (newX &gt; 10)\n    {\n        return false;\n    }\n    else\n    {\n        X = newX;\n        return true;\n    }\n}\n<\/code><\/pre>\n<p>Now the program can&#8217;t have any nasty surprises with X == 11 causing an out-of-range access or whatever.<\/p>\n<p>This is self defense for objects. They maintain control over who sets their data to what and can maintain consistency. Say you have a more complex case where you cannot sample an A\/D at over 10000 samples per second with the FIR filter enabled without starving the CPU and locking up the system. Whoops. If the only way to set the Filter state or the sampling rate is through setters in the A\/D manager object, the object can test and reject and prevent disaster (and possibly leave a nice fat log message pointing at the bad actor).  <\/p>\n<p>Think very hard before implementing a getter that returns a non-constant reference or a pointer. Once the caller has either, they can do whatever they want with the returned data.<\/p>\n<p>The rule of thumb is to default to paranoia: Grant no access to any data without a good reason, and then prefer controlled access through setters and getters.<\/p>\n<p>Onto the specifics of your problem.<\/p>\n<p>Getters and setters for a link node is often a sucker bet. The node most likely cannot determine for itself if a linkage is valid. Only the list manager can. This is a case where the object itself is too ignorant to know what is safe, so you have to open up the internals to another object that knows more. <code>friend<\/code> is useful here, though it is often better to make the node&#8217;s links <code>public<\/code> and never allow the list manager to give a node to a client.<\/p>\n<p>Odds are good the client should know absolutely nothing about how the list works anyway. Read up on coupling.<\/p>\n<p>So the node should be utterly stupid. This means you need to have a <code>ListManager<\/code> class to (duh) manage the list and protect the <code>node<\/code>s from badly behaved actors. <\/p>\n<p><code>ListManager<\/code> contains your <code>head<\/code>, <code>tail<\/code>, <code>root<\/code> or whatever along with <code>append<\/code> and <code>remove<\/code>, <code>print<\/code> and other list management methods. Under no circumstances do any of these functions reveal a <code>node<\/code> to the caller, though they can return a handle or an iterator that can be used to reference a node without giving the caller a tool to damage the list. Iterators are a topic worthy of their own question and probably have quite a few already.<\/p>\n<p>A bit of code to explain the above is in order. Please note I have marked, but not corrected, the logic problems I found. There may be more as this compiles (with C++11 enabled) but I haven&#8217;t run it.<\/p>\n<pre><code>class ListManager\n{\nprivate:\n    class node\n    {\n    public:\n        node *left_link = nullptr; \/\/ recommendation: immediately set or NULL all \n                                   \/\/ pointers unless you have a well documented \n                                   \/\/ reason not to and profiling to back it up.\n                                   \/\/ The time you save can be enormous.\n        char anything;\n        node *right_link = nullptr;\n    };\n    node *head = nullptr;\n    node *tail = nullptr;\n\npublic:\n\n    void append(char c) \/\/ head and tail not required ListManager members\n    {\n        \/* removed because the append function should append and only append.\n         * If you want to read data from the user, call a read function first \n         * and pass it read character in to append\n         * Do one thing and do it well. Every time you add behaviours to a \n         * function, you make it harder to debug. For example, what happens to \n         * the linked list if you fail to read a character? That shouldn't be \n         * append's problem. \n        char c;\n        std::cout &lt;&lt; \"Please enter a single character: \";\n        std::cin &gt;&gt; c;\n        *\/\n        node *current = new node();\n\n        current-&gt;anything = c;\n        \/\/std::cout &lt;&lt; current-&gt;anything &lt;&lt; std::endl; removed for same reason as above.\n\n        \/\/ think on this: how can head and tail NOT both be NULL at the same time?\n        \/\/ if you find a way, you have a bug that needs fixing.\n        if (head == nullptr &amp;&amp; tail == nullptr) \n        {\n            \/\/ If head is NULL, it has no right_link to assign. This will fail horribly.\n            head-&gt;right_link = current;\n            tail-&gt;left_link = current;\n            current-&gt;left_link = head;\n            current-&gt;right_link = tail;\n            \/* Consider instead\n            head = current;\n            tail = current;\n            *\/ \n        }\n        else\n        {\n            tail-&gt;right_link = current;\n            current-&gt;left_link = tail;\n            tail = current;\n            tail-&gt;right_link = nullptr; \/\/ don't need to do this. node constructor \n                                        \/\/ ensures current-&gt;right_link is NULL\n        }\n    }\n    \/\/ print function\n    void print() \/\/ no parameters required. head is ListManager member\n    {\n        node* temp;\n        temp = head;\n        \/\/ Again, if head is NULL, temp will be NULL and there will be no right_link\n        \/\/ consider instead\n        \/\/ while (temp != nullptr)\n        while (temp-&gt;right_link != nullptr)\n        {\n            std::cout &lt;&lt; temp-&gt;anything &lt;&lt; std::endl;\n            temp = temp-&gt;right_link;\n        }\n    }\n};\n<\/code><\/pre>\n<p>Note how <code>node<\/code> is built right into <code>ListManager<\/code> and is <code>private<\/code>. Now only <code>ListManager<\/code> has access to <code>node<\/code> and it has complete access.<\/p>\n<p><code>ListManager<\/code> also needs a destructor to handle <code>delete<\/code>ing all of the <code>new<\/code>ed nodes that were appended. It also needs a copy constructor and an assignment operator to make it Rule of Three compliant. &#8220;What is The Rule of Three?&#8221; you ask? It is very very important. Read the link to save yourself much future debugging. Failure to obey the Rule of Three results in a disproportionate number of C++ questions on Stack Overflow, and there is no point in inflating this statistic further.<\/p>\n<p>Usage:<\/p>\n<pre><code>int main()\n{\n    ListManager list;\n    char choice;\n    std::cout &lt;&lt; \"Please choose one menu option at a time:\\n\" &lt;&lt; \"1 = Append\\n\"\n            &lt;&lt; \"2 = Print list\\n\" &lt;&lt; \"3 = Exit\\n\\n\";\n\n    do\n    {\n        std::cout &lt;&lt; \"Menu option(1-3): \";\n        std::cin &gt;&gt; choice;\n\n        switch (choice)\n        {\n            case '1':\n                list.append('a'); \/\/ add to the end of list.\n                break;\n            case '2':\n                list.print (); \/\/ print list\n                break;\n            case '3':\n                std::cout &lt;&lt; \"end program\\n\\n\";\n                break;\n            default:\n                std::cout &lt;&lt; \"try again\\n\";\n                break;\n        }\n\n    } while (choice != '3');\n\n    return 0;\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Linked List private pointers C++ [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] private members, pointers or otherwise, are data that the object either does not want messed by anyone else with or wants to know if someone messed with them. Setter and getter methods allow access to the private member, but in a controlled manner. For example if you have a private integer that under no &#8230; <a title=\"[Solved] Linked List private pointers C++ [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/\" aria-label=\"More on [Solved] Linked List private pointers C++ [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,519,1402,712,915],"class_list":["post-14734","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-class","tag-linked-list","tag-pointers","tag-private"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Linked List private pointers C++ [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Linked List private pointers C++ [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] private members, pointers or otherwise, are data that the object either does not want messed by anyone else with or wants to know if someone messed with them. Setter and getter methods allow access to the private member, but in a controlled manner. For example if you have a private integer that under no ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-08T17:17:09+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-private-pointers-c-closed\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-private-pointers-c-closed\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Linked List private pointers C++ [closed]\",\"datePublished\":\"2022-10-08T17:17:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-private-pointers-c-closed\\\/\"},\"wordCount\":621,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\",\"class\",\"linked-list\",\"pointers\",\"private\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-private-pointers-c-closed\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-private-pointers-c-closed\\\/\",\"name\":\"[Solved] Linked List private pointers C++ [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-10-08T17:17:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-private-pointers-c-closed\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-private-pointers-c-closed\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-private-pointers-c-closed\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Linked List private pointers C++ [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Linked List private pointers C++ [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Linked List private pointers C++ [closed] - JassWeb","og_description":"[ad_1] private members, pointers or otherwise, are data that the object either does not want messed by anyone else with or wants to know if someone messed with them. Setter and getter methods allow access to the private member, but in a controlled manner. For example if you have a private integer that under no ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-08T17:17:09+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Linked List private pointers C++ [closed]","datePublished":"2022-10-08T17:17:09+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/"},"wordCount":621,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","class","linked-list","pointers","private"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/","name":"[Solved] Linked List private pointers C++ [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-08T17:17:09+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-private-pointers-c-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Linked List private pointers C++ [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=14734"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14734\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=14734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=14734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=14734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}