{"id":1008,"date":"2017-03-17T07:00:28","date_gmt":"2017-03-17T14:00:28","guid":{"rendered":"http:\/\/somethingk.com\/main\/?p=1008"},"modified":"2017-04-06T07:19:07","modified_gmt":"2017-04-06T14:19:07","slug":"simple-list-class-in-c","status":"publish","type":"post","link":"https:\/\/somethingk.com\/main\/simple-list-class-in-c\/","title":{"rendered":"Simple List Class in C++"},"content":{"rendered":"<section id=\"text-4\" class=\"widget boka-widget widget_text amr_widget\">\t\t\t<div class=\"textwidget\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block; text-align:center;\"\r\n     data-ad-layout=\"in-article\"\r\n     data-ad-format=\"fluid\"\r\n     data-ad-client=\"ca-pub-7619916617995509\"\r\n     data-ad-slot=\"9102150708\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div>\n\t\t<\/section>\n<p>Here is an example of a simple List class I wrote. Follow the comments to understand what is going on. This uses the node structure I wrote about <a href=\"http:\/\/somethingk.com\/main\/?p=1003\">here<\/a>.<\/p>\n<p>Best of luck!<\/p>\n<div class=\"snippetcpt-wrap\" id=\"snippet-1009\" data-id=\"1009\" data-edit=\"\" data-copy=\"\/main\/wp-json\/wp\/v2\/posts\/1008?snippet=17b1fac833&#038;id=1009\" data-fullscreen=\"https:\/\/somethingk.com\/main\/code-snippets\/list-class-in-c\/?full-screen=1\">\n\t\t\t\t<pre class=\"prettyprint linenums lang-c_cpp\" title=\"List Class in C++\">#include &lt;cstdlib&gt;\r\n#include &quot;Node.h&quot; \/\/Your Node class, see previous post\r\n\r\nusing namespace std;\r\n\r\nclass List\r\n    {\r\n        public:\r\n            List(){\r\n                head= NULL;\r\n            }\r\n            ~List(){\r\n                destroyList(head);\r\n            }\r\n            void append(int d){\r\n                head = append(head, d);\r\n            }\r\n            void print(){\r\n                if (head != nullptr)\r\n                print(head);\r\n            }\r\n            void insertInOrder(int d){\r\n                head = insertInOrder(head, d);\r\n            }\r\n            bool findValue (int d){\r\n                return findValue(head, d);\r\n            }\r\n            int getSize(){\r\n                return getSize(head);\r\n            }\r\n    \r\n        private:\r\n            Node *head; \/\/Remember this struct from the previous post\r\n\r\n            \/* Destroy the list\r\n            * We recursively call the destroy function to cycle through a list to the end.\r\n            * Once the end node is reached, it is deleted. Then as we progress back through\r\n            * our recursive call, each associated node is deleted from the end up.\r\n            * This is called by our destructor to clean everything up.\r\n            *\/\r\n            void destroyList(Node *n){\r\n                if (n-&gt;next != nullptr){\r\n                    destroyList(n-&gt;next);\r\n                }\r\n                delete n;\r\n            }\r\n\r\n            \/* Add a Node to the end of a list\r\n            * Use recursion to cycle through the list to the end\r\n            * Once the end is reached, the next pointer of the last node will by nullptr.\r\n            * A new Node will then be inserted replacing that nullptr and chaining to the list\r\n            *\/\r\n            Node* append(Node *n, int d){\r\n                if (n == nullptr){\r\n                    return new Node(d);\r\n                }\r\n                else {\r\n                    n-&gt;next= append(n-&gt;next, d);\r\n                }\r\n                return n;\r\n            }\r\n\r\n            \/* Print the List\r\n            * Use recursion to loop through and print each Node in a list\r\n            *\/\r\n            void print(Node *n){\r\n                cout &lt;&lt; n-&gt;data &lt;&lt; endl;\r\n                if (n-&gt;next != nullptr){\r\n                    print(n-&gt;next);\r\n                }\r\n            }\r\n\r\n            \/* Insert a Node in Numeric order\r\n            * Loop throug a list using recursion\r\n            * Once the inserting value is less than the current Node\r\n            * we know we need to insert the Node in that position\r\n            * else we keep cycling through\r\n            *\/\r\n            Node* insertInOrder(Node *n, int d){\r\n                if (n == nullptr){\r\n                    return new Node(d);\r\n                }\r\n                else if (d &gt; n-&gt;data){\r\n                    n-&gt;next= insertInOrder(n-&gt;next, d);\r\n                }\r\n                else {\r\n                    Node* temp = n; \/\/Temp copy\r\n                    n = new Node(d); \/\/Set the pointer to a new Node\r\n                    n-&gt;next = temp; \/\/Chain the original onto the new Node's next pointer\r\n                }\r\n                return n;\r\n            }\r\n\r\n            \/* Find a Node\r\n            * Loop through the enitre list using recursion\r\n            * Return true once the Node value we want is found\r\n            *\/\r\n            bool findValue(Node *n, int d){\r\n                if (n-&gt;data == d){\r\n                    return true;\r\n                }\r\n                else if(n-&gt;next != nullptr){\r\n                    return findValue(n-&gt;next, d);\r\n                }\r\n                return false;\r\n            }\r\n\r\n            \/* Get the Size of a List\r\n            * Use recursion to cycle through the list\r\n            * all the while keeping a counter going of each Node encountered\r\n            *\/\r\n            int getSize(Node *n, int i = 0){\r\n                if (n != nullptr){\r\n                    ++i;\r\n                    return getSize(n-&gt;next, i);\r\n                }\r\n                return i;\r\n            }\r\n\r\n    };<\/pre>\n\t\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here is an example of a simple List class I wrote. Follow the comments to understand what is going on. This uses the node structure I wrote about here. Best of luck!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[177],"tags":[507,303,508,194,510,506,487,505,509],"class_list":["post-1008","post","type-post","status-publish","format-standard","hentry","category-development","tag-append","tag-c","tag-insert","tag-list","tag-print","tag-recursion","tag-search","tag-simple","tag-size"],"_links":{"self":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/comments?post=1008"}],"version-history":[{"count":2,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1008\/revisions"}],"predecessor-version":[{"id":1141,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1008\/revisions\/1141"}],"wp:attachment":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/media?parent=1008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/categories?post=1008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/tags?post=1008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}