{"id":27013,"date":"2018-01-02T21:34:21","date_gmt":"2018-01-02T16:04:21","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27013"},"modified":"2018-01-02T21:34:21","modified_gmt":"2018-01-02T16:04:21","slug":"doubly-linked-list-introduction-insertion","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/doubly-linked-list-introduction-insertion\/","title":{"rendered":"Doubly Linked List | Set 1 (Introduction and Insertion)"},"content":{"rendered":"<p>A <strong>D<\/strong>oubly <strong>L<\/strong>inked <strong>L<\/strong>ist (DLL) contains an extra pointer, typically called <em>previous pointer<\/em>, together with next pointer and data which are there in singly linked list.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-27039\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL1.png\" alt=\"Doubly Linked List | Set 1 (Introduction and Insertion)\" width=\"907\" height=\"186\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL1.png 907w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL1-300x62.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL1-768x157.png 768w\" sizes=\"(max-width: 907px) 100vw, 907px\" \/><\/p>\n<p>Following is representation of a DLL node in C language.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/* Node of a doubly linked list *\/<br\/>struct node<br\/>{<br\/>  int data;<br\/>  struct node *next; \/\/ Pointer to next node in DLL<br\/>  struct node *prev; \/\/ Pointer to previous node in DLL<br\/>};<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p>Following are advantages\/disadvantages of doubly linked list over singly linked list.<\/p>\n<p><strong>Advantages over singly linked list<\/strong><br \/>\n<strong>1)<\/strong> A DLL can be traversed in both forward and backward direction.<br \/>\n<strong>2)<\/strong> The delete operation in DLL is more efficient if pointer to the node to be deleted is given.<br \/>\nIn singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.<\/p>\n<p><strong>Disadvantages over singly linked list<\/strong><br \/>\n<strong>1)<\/strong> Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though (See <a href=\"http:\/\/www.geeksforgeeks.org\/xor-linked-list-a-memory-efficient-doubly-linked-list-set-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">this <\/a>and <a href=\"http:\/\/www.geeksforgeeks.org\/xor-linked-list-a-memory-efficient-doubly-linked-list-set-2\/\" target=\"_blank\" rel=\"noopener noreferrer\">this<\/a>).<br \/>\n<strong>2)<\/strong> All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.<\/p>\n<p><strong>Insertion<\/strong><br \/>\nA node can be added in four ways<br \/>\n<strong>1) <\/strong>At the front of the DLL<br \/>\n<strong>2)<\/strong> After a given node.<br \/>\n<strong>3)<\/strong> At the end of the DLL<br \/>\n<strong>4)<\/strong> Before a given node.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>1) Add a node at the front: (A 5 steps process)<\/strong><br \/>\nThe new node is always added before the head of the given Linked List. And newly added node becomes the new head of DLL. For example if the given Linked List is 10152025 and we add an item 5 at the front, then the Linked List becomes 510152025. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node (See <a href=\"http:\/\/www.geeksforgeeks.org\/how-to-write-functions-that-modify-the-head-pointer-of-a-linked-list\/\" target=\"_blank\" rel=\"noopener noreferrer\">this<\/a>)<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-27042\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_front1.png\" alt=\"Doubly Linked List | Set 1 (Introduction and Insertion)\" width=\"1096\" height=\"340\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_front1.png 1096w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_front1-300x93.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_front1-768x238.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_front1-1024x318.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_front1-990x307.png 990w\" sizes=\"(max-width: 1096px) 100vw, 1096px\" \/><\/p>\n<p>Following are the 5 steps to add node at the front.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/* Given a reference (pointer to pointer) to the head of a list<br\/>   and an int, inserts a new node on the front of the list. *\/<br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* 1. allocate node *\/<br\/>    struct node* new_node = (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* 2. put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* 3. Make next of new node as head and previous as NULL *\/<br\/>    new_node-&gt;next = (*head_ref);<br\/>    new_node-&gt;prev = NULL;<br\/> <br\/>    \/* 4. change prev of head node to new node *\/<br\/>    if((*head_ref) !=  NULL)<br\/>      (*head_ref)-&gt;prev = new_node ;<br\/> <br\/>    \/* 5. move the head to point to the new node *\/<br\/>    (*head_ref)    = new_node;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p>Four steps of the above five steps are same as <a href=\"http:\/\/quiz.geeksforgeeks.org\/linked-list-set-2-inserting-a-node\/\" target=\"_blank\" rel=\"noopener noreferrer\">the 4 steps used for inserting at the front in singly linked list<\/a>. The only extra step is to change previous of head.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>2) Add a node after a given node.: (A 7 steps process)<\/strong><br \/>\nWe are given pointer to a node as prev_node, and the new node is inserted after the given node.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-27046\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_middle1.png\" alt=\"Doubly Linked List | Set 1 (Introduction and Insertion)\" width=\"1023\" height=\"307\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_middle1.png 1023w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_middle1-300x90.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_middle1-768x230.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_middle1-990x297.png 990w\" sizes=\"(max-width: 1023px) 100vw, 1023px\" \/><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/* Given a node as prev_node, insert a new node after the given node *\/<br\/>void insertAfter(struct node* prev_node, int new_data)<br\/>{<br\/>    \/*1. check if the given prev_node is NULL *\/<br\/>    if (prev_node == NULL)<br\/>    {<br\/>        printf(&quot;the given previous node cannot be NULL&quot;);<br\/>        return;<br\/>    }<br\/> <br\/>    \/* 2. allocate new node *\/<br\/>    struct node* new_node =(struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* 3. put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* 4. Make next of new node as next of prev_node *\/<br\/>    new_node-&gt;next = prev_node-&gt;next;<br\/> <br\/>    \/* 5. Make the next of prev_node as new_node *\/<br\/>    prev_node-&gt;next = new_node;<br\/> <br\/>    \/* 6. Make prev_node as previous of new_node *\/<br\/>    new_node-&gt;prev = prev_node;<br\/> <br\/>    \/* 7. Change previous of new_node&#039;s next node *\/<br\/>    if (new_node-&gt;next != NULL)<br\/>      new_node-&gt;next-&gt;prev = new_node;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p>Five of the above steps step process are same as <a href=\"http:\/\/quiz.geeksforgeeks.org\/linked-list-set-2-inserting-a-node\/\" target=\"_blank\" rel=\"noopener noreferrer\">the 5 steps used for inserting after a given node in singly linked list<\/a>. The two extra steps are needed to change previous pointer of new node and previous pointer of new node\u2019s next node.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>3) Add a node at the end: (7 steps process)<\/strong><br \/>\nThe new node is always added after the last node of the given Linked List. For example if the given DLL is 510152025 and we add an item 30 at the end, then the DLL becomes 51015202530.<br \/>\nSince a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-27049\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_end1.png\" alt=\"Doubly Linked List | Set 1 (Introduction and Insertion)\" width=\"1052\" height=\"269\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_end1.png 1052w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_end1-300x77.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_end1-768x196.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_end1-1024x262.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/DLL_add_end1-990x253.png 990w\" sizes=\"(max-width: 1052px) 100vw, 1052px\" \/><\/p>\n<p>Following are the 7 steps to add node at the end<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/* Given a reference (pointer to pointer) to the head<br\/>   of a DLL and an int, appends a new node at the end  *\/<br\/>void append(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* 1. allocate node *\/<br\/>    struct node* new_node = (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    struct node *last = *head_ref;  \/* used in step 5*\/<br\/> <br\/>    \/* 2. put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* 3. This new node is going to be the last node, so<br\/>          make next of it as NULL*\/<br\/>    new_node-&gt;next = NULL;<br\/> <br\/>    \/* 4. If the Linked List is empty, then make the new<br\/>          node as head *\/<br\/>    if (*head_ref == NULL)<br\/>    {<br\/>        new_node-&gt;prev = NULL;<br\/>        *head_ref = new_node;<br\/>        return;<br\/>    }<br\/> <br\/>    \/* 5. Else traverse till the last node *\/<br\/>    while (last-&gt;next != NULL)<br\/>        last = last-&gt;next;<br\/> <br\/>    \/* 6. Change the next of last node *\/<br\/>    last-&gt;next = new_node;<br\/> <br\/>    \/* 7. Make last node as previous of new node *\/<br\/>    new_node-&gt;prev = last;<br\/> <br\/>    return;<br\/>}<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p>Six of the above 7 steps are same as <a href=\"http:\/\/quiz.geeksforgeeks.org\/linked-list-set-2-inserting-a-node\/\" target=\"_blank\" rel=\"noopener noreferrer\">the 6 steps used for inserting after a given node in singly linked list<\/a>. The one extra step is needed to change previous pointer of new node.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>4) Add a node before a given node<\/strong><br \/>\nThis is left as an exercise for the readers.<\/p>\n<p><strong>A complete working program to test above functions.<\/strong><br \/>\nFollowing is complete C program to test above functions.<\/p>\n<p>C Programming:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A complete working C program to demonstrate all insertion methods<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/> <br\/>\/\/ A linked list node<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node *next;<br\/>    struct node *prev;<br\/>};<br\/> <br\/>\/* Given a reference (pointer to pointer) to the head of a list<br\/>   and an int, inserts a new node on the front of the list. *\/<br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* 1. allocate node *\/<br\/>    struct node* new_node = (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* 2. put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* 3. Make next of new node as head and previous as NULL *\/<br\/>    new_node-&gt;next = (*head_ref);<br\/>    new_node-&gt;prev = NULL;<br\/> <br\/>    \/* 4. change prev of head node to new node *\/<br\/>    if((*head_ref) !=  NULL)<br\/>      (*head_ref)-&gt;prev = new_node ;<br\/> <br\/>    \/* 5. move the head to point to the new node *\/<br\/>    (*head_ref)    = new_node;<br\/>}<br\/> <br\/>\/* Given a node as prev_node, insert a new node after the given node *\/<br\/>void insertAfter(struct node* prev_node, int new_data)<br\/>{<br\/>    \/*1. check if the given prev_node is NULL *\/<br\/>    if (prev_node == NULL)<br\/>    {<br\/>        printf(&quot;the given previous node cannot be NULL&quot;);<br\/>        return;<br\/>    }<br\/> <br\/>    \/* 2. allocate new node *\/<br\/>    struct node* new_node =(struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* 3. put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* 4. Make next of new node as next of prev_node *\/<br\/>    new_node-&gt;next = prev_node-&gt;next;<br\/> <br\/>    \/* 5. Make the next of prev_node as new_node *\/<br\/>    prev_node-&gt;next = new_node;<br\/> <br\/>    \/* 6. Make prev_node as previous of new_node *\/<br\/>    new_node-&gt;prev = prev_node;<br\/> <br\/>    \/* 7. Change previous of new_node&#039;s next node *\/<br\/>    if (new_node-&gt;next != NULL)<br\/>      new_node-&gt;next-&gt;prev = new_node;<br\/>}<br\/> <br\/>\/* Given a reference (pointer to pointer) to the head<br\/>   of a DLL and an int, appends a new node at the end  *\/<br\/>void append(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* 1. allocate node *\/<br\/>    struct node* new_node = (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    struct node *last = *head_ref;  \/* used in step 5*\/<br\/> <br\/>    \/* 2. put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* 3. This new node is going to be the last node, so<br\/>          make next of it as NULL*\/<br\/>    new_node-&gt;next = NULL;<br\/> <br\/>    \/* 4. If the Linked List is empty, then make the new<br\/>          node as head *\/<br\/>    if (*head_ref == NULL)<br\/>    {<br\/>        new_node-&gt;prev = NULL;<br\/>        *head_ref = new_node;<br\/>        return;<br\/>    }<br\/> <br\/>    \/* 5. Else traverse till the last node *\/<br\/>    while (last-&gt;next != NULL)<br\/>        last = last-&gt;next;<br\/> <br\/>    \/* 6. Change the next of last node *\/<br\/>    last-&gt;next = new_node;<br\/> <br\/>    \/* 7. Make last node as previous of new node *\/<br\/>    new_node-&gt;prev = last;<br\/> <br\/>    return;<br\/>}<br\/> <br\/>\/\/ This function prints contents of linked list starting from the given node<br\/>void printList(struct node *node)<br\/>{<br\/>    struct node *last;<br\/>    printf(&quot;\\nTraversal in forward direction \\n&quot;);<br\/>    while (node != NULL)<br\/>    {<br\/>        printf(&quot; %d &quot;, node-&gt;data);<br\/>        last = node;<br\/>        node = node-&gt;next;<br\/>    }<br\/> <br\/>    printf(&quot;\\nTraversal in reverse direction \\n&quot;);<br\/>    while (last != NULL)<br\/>    {<br\/>        printf(&quot; %d &quot;, last-&gt;data);<br\/>        last = last-&gt;prev;<br\/>    }<br\/>}<br\/> <br\/>\/* Drier program to test above functions*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/> <br\/>    \/\/ Insert 6.  So linked list becomes 6-&gt;NULL<br\/>    append(&amp;head, 6);<br\/> <br\/>    \/\/ Insert 7 at the beginning. So linked list becomes 7-&gt;6-&gt;NULL<br\/>    push(&amp;head, 7);<br\/> <br\/>    \/\/ Insert 1 at the beginning. So linked list becomes 1-&gt;7-&gt;6-&gt;NULL<br\/>    push(&amp;head, 1);<br\/> <br\/>    \/\/ Insert 4 at the end. So linked list becomes 1-&gt;7-&gt;6-&gt;4-&gt;NULL<br\/>    append(&amp;head, 4);<br\/> <br\/>    \/\/ Insert 8, after 7. So linked list becomes 1-&gt;7-&gt;8-&gt;6-&gt;4-&gt;NULL<br\/>    insertAfter(head-&gt;next, 8);<br\/> <br\/>    printf(&quot;Created DLL is: &quot;);<br\/>    printList(head);<br\/> <br\/>    getchar();<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre> Created DLL is:\r\nTraversal in forward direction\r\n 1  7  8  6  4\r\nTraversal in reverse direction\r\n 4  6  8  7  1<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Doubly Linked List (Introduction and Insertion) &#8211; Doubly Linked List  &#8211; A Doubly Linked List (DLL) contains an extra pointer, typically called previous.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79479,79476],"tags":[80728,80725,80731,80730,80724,80726,80727,80729],"class_list":["post-27013","post","type-post","status-publish","format-standard","hentry","category-doubly-linked-list","category-linked-list","tag-algorithm-for-deletion-in-doubly-linked-list","tag-algorithm-to-insert-a-node-at-the-end-of-a-doubly-linked-list","tag-doubly-linked-list-algorithm-pdf","tag-doubly-linked-list-insertion","tag-doubly-linked-list-operations","tag-explain-transitive-and-reflexive-closure-of-a-graph","tag-insertion-and-deletion-in-doubly-linked-list-in-c-program","tag-what-is-circular-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27013"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27013\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}