{"id":2893,"date":"2018-01-06T00:01:23","date_gmt":"2018-01-06T08:01:23","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2893"},"modified":"2018-01-13T10:01:04","modified_gmt":"2018-01-13T18:01:04","slug":"the-dreaded-double-linked-list","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2893","title":{"rendered":"The Dreaded Double-Linked List"},"content":{"rendered":"<p>As if a linked list itself isn&#8217;t one of the most terrifying things in C, another beast exists: the double-linked list. It mixes structures and pointers and offers gross potential for extreme mayhem. That sounds like fun!<br \/>\n<!--more--><br \/>\nAs a review, a linked list is a chain of structures. It works like an array, though it&#8217;s allocated dynamically: Each structure is created in memory and it references the next structure in the chain. That&#8217;s the &#8220;link&#8221; part, the specific structure member that&#8217;s a pointer to the next structure. Figure 1 illustrates the linked list concept.<\/p>\n<div id=\"attachment_2897\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2897\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0106-figure1_linked-list-1.png\" alt=\"\" width=\"550\" height=\"98\" class=\"size-full wp-image-2897\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0106-figure1_linked-list-1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0106-figure1_linked-list-1-300x53.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0106-figure1_linked-list-1-500x89.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-2897\" class=\"wp-caption-text\">Figure 1. A linked list should be this simple.<\/p><\/div>\n<p>I cover linked lists in my book, <em>Beginning Programming with C For Dummies<\/em>. I don&#8217;t cover double-linked lists because the publisher allots me only so many pages and I have a lot of ground to cover. Double-linked lists got cut.<\/p>\n<p>The concept of a double-linked list is like a linked list, but with one addition: a second pointer member that references the previous structure in the chain. Figure 2 illustrates the extra pointer as well as the original pointer as lines that reference other structures.<\/p>\n<div id=\"attachment_2898\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2898\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0106-figure2_double-linked-list.png\" alt=\"\" width=\"550\" height=\"157\" class=\"size-full wp-image-2898\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0106-figure2_double-linked-list.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0106-figure2_double-linked-list-300x86.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/01\/0106-figure2_double-linked-list-500x143.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-2898\" class=\"wp-caption-text\">Figure 2. A double-linked list has a lot of pointers, links, and messy lines.<\/p><\/div>\n<p>As you might suspect, it takes some thought to create and manage that second pointer. So why bother?<\/p>\n<p>Yes, there is a reason: The advantage to the double-linked list is that you can work both forward and backward. You can reference the current structure, the next structure, <em>and<\/em> the previous structure. For complex data, this type of arrangement is beneficial.<\/p>\n<p>To create a double-linked list, the structure must have two self-referencing structure pointers as members. One references the next structure in the list and the other references the previous structure. Null pointers mark the start and end of the chain.<\/p>\n<p>Here is an example of a double-linked list structure:<\/p>\n<pre class=\"screen\">\r\nstruct greek {\r\n    struct greek *prev;\r\n    char *letter;\r\n    struct greek *next;\r\n};<\/pre>\n<p><code>greek<\/code> is the name of the structure. Two members are pointers to other <code>greek<\/code> structures, <code>*prev<\/code> and <code>*next<\/code>. The other member is a <em>char<\/em> pointer, a string.<\/p>\n<p>If multiple functions refer to the double-linked list structure, it must be declared externally. Further, the structure must be declared before any function that references the structure, as well as any structure variables. You can get into lots of trouble if you don&#8217;t heed this admonition.<\/p>\n<p>To work with the double-linked list, four structure pointer variables are required:<\/p>\n<p><code>struct greek *first;<br \/>\nstruct greek *last;<br \/>\nstruct greek *current;<br \/>\nstruct greek *created;<\/code><\/p>\n<p>The <code>first<\/code> and <code>last<\/code> pointers reference the first and last items in the double-linked list, respectively. <code>first<\/code> is set when the list is initially created; <code>last<\/code> is set when the final structure in the list is created. The <code>current<\/code> and <code>created<\/code> pointers are used as the list is built.<\/p>\n<blockquote><p>I&#8217;m using the variable name <code>created<\/code> for newly-allocated structures in the linked list. The traditional variable name is <code>new<\/code>, though <em>new<\/em> is a reserved word in the C++ language, so I shy away from it.<\/p><\/blockquote>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2905\">next week&#8217;s Lesson<\/a>, I present code that builds a double-linked list of <code>greek<\/code> structure variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It doesn&#8217;t need to be as horrible as it sounds. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2893\">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-2893","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\/2893","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=2893"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2893\/revisions"}],"predecessor-version":[{"id":2938,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2893\/revisions\/2938"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}