{"id":2905,"date":"2018-01-13T00:01:49","date_gmt":"2018-01-13T08:01:49","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2905"},"modified":"2018-01-20T08:39:54","modified_gmt":"2018-01-20T16:39:54","slug":"building-a-double-linked-list","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2905","title":{"rendered":"Building a Double-Linked List"},"content":{"rendered":"<p>Similar to a linked list, a double-linked list requires that you work on both the current item in the list as well as the the previous item. You just have one additional structure member to update, which is the pointer from the current item to the previous structure.<br \/>\n<!--more--><br \/>\nIn a linked list, two pointer variables are used to create the list. They reference the current and newly-created structures in the list, so I use the variable names <code>current<\/code> and <code>created<\/code> (formerly &#8220;new&#8221;). After allocating the <code>created<\/code> structure, you update the <code>current<\/code> structure&#8217;s member referencing that new structure, <code>*next<\/code>, as in:<\/p>\n<p><code>current-&gt;next=created;<\/code><\/p>\n<p>So the <code>current<\/code> structure&#8217;s <code>next<\/code> member helps find the next structure in the list, <code>created<\/code>.<\/p>\n<p>In a double-linked list you must also set the <code>created<\/code> structure&#8217;s <code>*prev<\/code> member to reference the previous structure. That structure&#8217;s location is saved in the <code>current<\/code> pointer:<\/p>\n<p><code>created-&gt;prev=current;<\/code><\/p>\n<p>That&#8217;s really all the overhead you need, though the sequence of events is crucial.<\/p>\n<p>The following code uses the <code>greek<\/code> structure introduced in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2893\">last week&#8217;s Lesson<\/a>. The double-linked list created contains the names of the first ten letters of the Greek alphabet.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define ITEMS 10\r\n\r\nint main()\r\n{\r\n    struct greek {\r\n        struct greek *prev;\r\n        char *letter;\r\n        struct greek *next;\r\n    };\r\n    char *ab[] = {\r\n        \"alpha\", \"beta\", \"gamma\", \"delta\", \"epsilon\",\r\n        \"zeta\", \"eta\", \"theta\", \"iota\", \"kappa\"\r\n    };\r\n    struct greek *first,*last,*current,*created;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* build the double-linked list *\/<\/span>\r\n    for(x=0;x<=&lt;ITEMS;x++)\r\n    {\r\n        created = (struct greek *)malloc(sizeof(struct greek));\r\n        if( created==NULL )\r\n        {\r\n            puts(\"Unable to allocate memory\");\r\n            exit(1);\r\n        }\r\n        \/* first item in the list is special *\/\r\n        if( x==0 )\r\n        {\r\n            created-&gt;prev = NULL;\r\n            first = created;\r\n        }\r\n        else\r\n        {\r\n            created-&gt;prev = current;\r\n            current-&gt;next = created;\r\n        }\r\n        created-&gt;letter = ab[x];\r\n        created-&gt;next = NULL;\r\n        current = created;\r\n    }\r\n    last = current;\r\n\r\n    <span class=\"comments\">\/* display list front-to-back *\/<\/span>\r\n    current = first;\r\n    while(current)\r\n    {\r\n        printf(\"%s\\n\",current->letter);\r\n        current = current-&gt;next;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code has a lot of setup, which is necessary for a double-linked list: The structure <code>greek<\/code> is declared at the start of the <em>main()<\/em> function, as is the <code>*ab[]<\/code> array, and the pointers required to manage the structure. Because this example uses only the <em>main()<\/em> function, the <code>greek<\/code> structure as well as the pointers are declared inside that function. If any other functions referenced the structure pointers, they must be declared externally.<\/p>\n<p>The core of the code is the <em>for<\/em> loop between Lines 21 and 43.<\/p>\n<p>Line 23 allocates memory for a new structure and assigns that address to the <code>created<\/code> pointer.<\/p>\n<p>Lines 30 through 34 handle the first item in the double-linked list, which is special: Line 32 sets the <code>*prev<\/code> element to <code>NULL<\/code>, marking the start of the list. The <code>first<\/code> pointer is assigned to the beginning structure to mark its location for reference.<\/p>\n<p>The <em>else<\/em> statements set options for all structures after the first: The <code>current<\/code> pointer references what becomes the previous structure in the list; its location is saved in the <code>created<\/code> structure&#8217;s <code>*prev<\/code> member. The <code>current<\/code> structure&#8217;s <code>*next<\/code> member is updated to point to the <code>created<\/code> structure&#8217;s address:<\/p>\n<p><code>created-&gt;prev = current;<br \/>\ncurrent-&gt;next = created;<\/code><\/p>\n<p>Next, the new (<code>created<\/code>) structure&#8217;s <code>letter<\/code> member is set to a letter of the Greek alphabet (Line 40). The <code>*next<\/code> member is assigned the <code>NULL<\/code> pointer. Finally, Line 42 sets the new structure to the <code>current<\/code> pointer, <code>current=created<\/code>.<\/p>\n<p>When the <em>for<\/em> loop is complete, the <code>last<\/code> pointer is updated to reference <code>current<\/code>, which becomes the final structure in the list.<\/p>\n<p>To prove that it all works, a <em>while<\/em> loop displays the double-linked list:<\/p>\n<pre><code>alpha\r\nbeta\r\ngamma\r\ndelta\r\nepislon\r\nzeta\r\neta\r\ntheta\r\niota\r\nkappa<\/pre>\n<p><\/code><\/p>\n<p>Because the list is double-linked, you can also display it in reverse order, as well as reference previous and next items from within the list. I'll explore these options in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2919\">next week's Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A structure in a double-linked list must reference the previous structure in memory, which requires extra overhead when creating the list. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2905\">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-2905","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\/2905","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=2905"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2905\/revisions"}],"predecessor-version":[{"id":2940,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2905\/revisions\/2940"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}