{"id":2919,"date":"2018-01-20T00:01:23","date_gmt":"2018-01-20T08:01:23","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2919"},"modified":"2018-01-06T10:32:35","modified_gmt":"2018-01-06T18:32:35","slug":"playing-with-a-double-linked-list","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2919","title":{"rendered":"Playing with a Double-Linked List"},"content":{"rendered":"<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2905\">last week&#8217;s Lesson<\/a>, I demonstrated code that builds a double-linked list: Each structure in the list references both the next structure and the previous structure. The first and last structure addresses are saved. And <code>NULL<\/code> pointers within the list its start and end. How all that junk becomes useful is apparent as you work with the list.<br \/>\n<!--more--><br \/>\nThe code presented in last week&#8217;s Lesson walked through the linked list from the first structure to the last, displaying each structure&#8217;s data. This feat you can perform in single-linked lists as well. To move through a double-linked list in reverse order, the code looks like this:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* display list back-to-front *\/<\/span>\r\n    current = last;\r\n    while(current)\r\n    {\r\n        printf(\"%s\\n\",current->letter);\r\n        current = current-&gt;prev;\r\n    }<\/pre>\n<p>The <code>current<\/code> pointer is assigned to the <code>last<\/code> structure in the list (instead of <code>first<\/code>). Within the <em>while<\/em> loop, the <code>current<\/code> pointer is updated to the value of <code>current-&gt;prev<\/code> instead of <code>current-&gt;next<\/code>. The result is that the first ten letters of the Greek alphabet are output in reverse order:<\/p>\n<pre><code>kappa\r\niota\r\ntheta\r\neta\r\nzeta\r\nepsilon\r\ndelta\r\ngamma\r\nbeta\r\nalpha<\/code><\/pre>\n<p>For a standard, single-linked list, this task would be difficult, especially if you wanted to show the list both ways in the same program. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/01\/0120a.c\">Click here<\/a> to view the full code.<\/p>\n<p>Another advantage of a double-linked list is that you can move forward or backward from any specific structure, even accessing neighboring structure&#8217;s data.<\/p>\n<p>The following code accesses the fifth structure in the linked list. It displays the current, previous, and next letter names in the Greek alphabet:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* display item 5 in the list *\/<\/span>\r\n    current = first;\r\n    for(x=0;x&lt;4;x++)\r\n        current = current->next;\r\n    printf(\"The fifth letter is %s\\n\",current-&gt;letter);\r\n    printf(\"The previous letter is %s\\n\",(current-&gt;prev)-&gt;letter);\r\n    printf(\"The next letter is %s\\n\",(current-&gt;next)-&gt;letter);<\/pre>\n<p>The <em>for<\/em> loop advances the <code>current<\/code> pointer through the list. Because variable <code>x<\/code> is post-incremented in the <em>for<\/em> loop, the value of <code>x<\/code> equals 4 when the loop is done, which represents the fifth item in the list.<\/p>\n<p>The letter name of the <code>current<\/code> pointer&#8217;s structure is accessed from <code>current-&gt;letter<\/code>.<\/p>\n<p>The previous structure is referenced through the <code>current-&gt;prev<\/code> pointer. That structure&#8217;s <code>letter<\/code> member is accessed through the <code>(current-&gt;prev)-&gt;letter<\/code> construction. Likewise, the next structure&#8217;s letter member is accessed by using <code>(current-&gt;next)-&gt;letter<\/code>. (The parentheses are optional; I&#8217;ve added them for readability.)<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>The fifth letter is epsilon\r\nThe previous letter is delta\r\nThe next letter is zeta<\/code><\/pre>\n<p>Similar output could be generated for each structure in the list; previous and next structures&#8217; members can always be accessed. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/01\/0120b.c\">Click here<\/a> to view the full code.<\/p>\n<p>Just to be obnoxious, the following construction is also possible with a linked list:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* display item 5 in the list *\/<\/span>\r\n    printf(\"The fifth letter is %s\\n\",\r\n            first-&gt;next-&gt;next-&gt;next-&gt;next-&gt;letter);<\/pre>\n<p>I find this type of structure-pointer reference ugly, yet it proves that the method shown earlier (<code>(current-&gt;next)-&gt;letter<\/code>) isn&#8217;t really as weird as it potentially could be.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explore the advantages of going to all the trouble of creating a double-linked list. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2919\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2919","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2919"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2919\/revisions"}],"predecessor-version":[{"id":2934,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2919\/revisions\/2934"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}