{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/doublyLinkedLists","result":{"data":{"blog":{"frontmatter":{"title":"DOUBLY LINKED LISTS","thumbnail":"blog24","date":"December 5, 2020","dsaCppCodeFile":"https://drive.google.com/file/d/1UVxVgfyqHo-G-fSwesiXHXCQDOslxoPl/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is doubly linked lists, its\n                applications, operations and implementation in C++.\n              </p>\n              <p>\n                Please check the blog post if you don't know what is\n                <a href=\"/blogs/linkedLists\">Linked Lists.</a>\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Definition</h4>\n              <div class=\"m-2\">\n                <p>\n                  Doubly linked lists are two-way lists because we can traverse\n                  in both the directions that is from head node to end or in\n                  reverse direction.\n                </p>\n                <p>\n                  Each node in DLL contains 1 data field and 2 reference fields.\n                  In these 2 reference feilds, there is one pointer reference to\n                  previous node and another to next node in the list.\n                </p>\n                <p>\n                  As we have 2 pointers, we can traverse in two directions\n                  starting from the head node to the end and vice versa.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>Previous and next page in web browser.</li>\n                <li>Undo and redo in editors.</li>\n                <li>DNA molecules.</li>\n                <li>\n                  Applications that have a most recently used/search list.\n                </li>\n                <li>\n                  Music players in which you can play the previous or next song\n                  in the list.\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Operations</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following thoery.\n              </p>\n              <p>\n                Note that the head node's data is used to count the no. of nodes\n                in the list & it's next pointer points to the first node. The\n                previous pointer of head and the next pointer all the tail nodes\n                will always point to NULL.\n              </p>\n              <div class=\"m-2\">\n                <p class=\"mb-0\">\n                  <strong>1. Traverse:</strong> Iterate through the nodes in the\n                  linked list starting from the head node.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Let us assume that the head points to the first node of the\n                    list.\n                  </li>\n                  <li>\n                    Create a temporary node which will point to the same node as\n                    that of head.\n                  </li>\n                  <li>\n                    Follow the pointers and stop when the next pointer points to\n                    NULL.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>2. Append:</strong> Add a new node to the end of a\n                  list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Create a temporary pointer which points to current head node\n                    and traverse to the end of the list.\n                  </li>\n                  <li>\n                    Make the new node's right pointer, to point to NULL and left\n                    pointer, to point to the last node.\n                  </li>\n                  <li>\n                    Update right pointer of last node to point to new node.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>3. Prepend:</strong> Add a new node to the beginning\n                  of a list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Right pointer of head node, left & right pointer of new node\n                    need to be modified and it can be done in two steps.\n                  </li>\n                  <li>\n                    Update the right pointer of the new node to point to the\n                    current head's next node and also make left pointer of new\n                    node to point to head.\n                  </li>\n                  <li>\n                    Update head node’s right pointer to point to the new node.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>4. Insert by position :</strong> Add a new node to a\n                  specific position on the list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    If we want to add an element at position 3 then we stop at\n                    position 2. That means we traverse 2 nodes and insert the\n                    new node. For simplicity let us assume that the second node\n                    is called position node.\n                  </li>\n                  <li>\n                    New node right pointer points to the next node of the\n                    position node and new node left pointer points to the\n                    position node.\n                  </li>\n                  <li>\n                    Position node right pointer points to the new node and the\n                    next node of position node left pointer points to new node.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>5. Delete at front:</strong> Remove the first node\n                  from the list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Move the head node's pointer to the next node and change the\n                    head node's left pointer to NULL.\n                  </li>\n                  <li>The front node will be unlinked with the list.</li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>5. Delete at rear:</strong> Remove the last node from\n                  the list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Traverse the list and while traversing maintain the previous\n                    node address also. By the time we reach the end of the list,\n                    we will have two pointers, one pointing to the tail and the\n                    other pointing to the node before the tail.\n                  </li>\n                  <li>\n                    Update the next pointer of previous node to the tail node\n                    with NULL and the rear node will be unlinked with the list.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>5. Delete by position:</strong> Remove a node at\n                  specified position from the list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    The node to be removed is always located between two nodes.\n                    The head and tail links are not updated.\n                  </li>\n                  <li>\n                    Similar to the previous case, maintain the previous node\n                    while also traversing the list. Upon locating the node to be\n                    deleted(position node), change the previous node’s right\n                    pointer to the right pointer of the node to be deleted.\n                  </li>\n                  <li>\n                    Update the left pointer of the node(which is the right node\n                    of the node to be deleted) to point to the previous node(of\n                    the node to be deleted). Bit clumsy right? :(\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>8. Display:</strong> Traverse and display the data of\n                  all nodes starting from head node.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Traverse the linked list(1st operation) and at each node,\n                    display its content\n                  </li>\n                  <li>\n                    This traversing method can also be used to count the number\n                    of nodes in the linked list.\n                  </li>\n                </ul>\n                <br />\n                <span class=\"text-muted\">\n                  You can do all the operations mentioned above with one\n                  temporary pointer itself.\n                </span>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/singlyLinkedLists\">Singly Linked Lists</a> |\n              <a href=\"/blogs/circularLinkedLists\">Circular Linked Lists</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is doubly linked lists, its\n                applications, operations and implementation in C++.\n              </p>\n              <p>\n                Please check the blog post if you don't know what is\n                <a href=\"/blogs/linkedLists\">Linked Lists.</a>\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Definition</h4>\n              <div class=\"m-2\">\n                <p>\n                  Doubly linked lists are two-way lists because we can traverse\n                  in both the directions that is from head node to end or in\n                  reverse direction.\n                </p>\n                <p>\n                  Each node in DLL contains 1 data field and 2 reference fields.\n                  In these 2 reference feilds, there is one pointer reference to\n                  previous node and another to next node in the list.\n                </p>\n                <p>\n                  As we have 2 pointers, we can traverse in two directions\n                  starting from the head node to the end and vice versa.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>Previous and next page in web browser.</li>\n                <li>Undo and redo in editors.</li>\n                <li>DNA molecules.</li>\n                <li>\n                  Applications that have a most recently used/search list.\n                </li>\n                <li>\n                  Music players in which you can play the previous or next song\n                  in the list.\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Operations</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following thoery.\n              </p>\n              <p>\n                Note that the head node's data is used to count the no. of nodes\n                in the list & it's next pointer points to the first node. The\n                previous pointer of head and the next pointer all the tail nodes\n                will always point to NULL.\n              </p>\n              <div class=\"m-2\">\n                <p class=\"mb-0\">\n                  <strong>1. Traverse:</strong> Iterate through the nodes in the\n                  linked list starting from the head node.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Let us assume that the head points to the first node of the\n                    list.\n                  </li>\n                  <li>\n                    Create a temporary node which will point to the same node as\n                    that of head.\n                  </li>\n                  <li>\n                    Follow the pointers and stop when the next pointer points to\n                    NULL.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>2. Append:</strong> Add a new node to the end of a\n                  list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Create a temporary pointer which points to current head node\n                    and traverse to the end of the list.\n                  </li>\n                  <li>\n                    Make the new node's right pointer, to point to NULL and left\n                    pointer, to point to the last node.\n                  </li>\n                  <li>\n                    Update right pointer of last node to point to new node.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>3. Prepend:</strong> Add a new node to the beginning\n                  of a list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Right pointer of head node, left & right pointer of new node\n                    need to be modified and it can be done in two steps.\n                  </li>\n                  <li>\n                    Update the right pointer of the new node to point to the\n                    current head's next node and also make left pointer of new\n                    node to point to head.\n                  </li>\n                  <li>\n                    Update head node’s right pointer to point to the new node.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>4. Insert by position :</strong> Add a new node to a\n                  specific position on the list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    If we want to add an element at position 3 then we stop at\n                    position 2. That means we traverse 2 nodes and insert the\n                    new node. For simplicity let us assume that the second node\n                    is called position node.\n                  </li>\n                  <li>\n                    New node right pointer points to the next node of the\n                    position node and new node left pointer points to the\n                    position node.\n                  </li>\n                  <li>\n                    Position node right pointer points to the new node and the\n                    next node of position node left pointer points to new node.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>5. Delete at front:</strong> Remove the first node\n                  from the list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Move the head node's pointer to the next node and change the\n                    head node's left pointer to NULL.\n                  </li>\n                  <li>The front node will be unlinked with the list.</li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>5. Delete at rear:</strong> Remove the last node from\n                  the list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Traverse the list and while traversing maintain the previous\n                    node address also. By the time we reach the end of the list,\n                    we will have two pointers, one pointing to the tail and the\n                    other pointing to the node before the tail.\n                  </li>\n                  <li>\n                    Update the next pointer of previous node to the tail node\n                    with NULL and the rear node will be unlinked with the list.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>5. Delete by position:</strong> Remove a node at\n                  specified position from the list.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    The node to be removed is always located between two nodes.\n                    The head and tail links are not updated.\n                  </li>\n                  <li>\n                    Similar to the previous case, maintain the previous node\n                    while also traversing the list. Upon locating the node to be\n                    deleted(position node), change the previous node’s right\n                    pointer to the right pointer of the node to be deleted.\n                  </li>\n                  <li>\n                    Update the left pointer of the node(which is the right node\n                    of the node to be deleted) to point to the previous node(of\n                    the node to be deleted). Bit clumsy right? :(\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>8. Display:</strong> Traverse and display the data of\n                  all nodes starting from head node.\n                </p>\n                <ul class=\"pl-4 pl-md-5\">\n                  <li>\n                    Traverse the linked list(1st operation) and at each node,\n                    display its content\n                  </li>\n                  <li>\n                    This traversing method can also be used to count the number\n                    of nodes in the linked list.\n                  </li>\n                </ul>\n                <br />\n                <span class=\"text-muted\">\n                  You can do all the operations mentioned above with one\n                  temporary pointer itself.\n                </span>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/singlyLinkedLists\">Singly Linked Lists</a> |\n              <a href=\"/blogs/circularLinkedLists\">Circular Linked Lists</a>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M26%2091c-3%200-3%200-3%2011l-1%208h3c4%200%2010-4%2010-7l2%202c3%206%209%207%2011%201l1-3%202%203c2%205%207%205%2010%202%204-4%204-17%201-17-2%200-3%201-3%207%200%205-1%207-2%207l-1-7c0-7%200-7-2-7-3%200-3%200-3%206v7l-1-6c-2-11-11-11-13%200l-1%205v-5c0-3-1-4-3-6s-3-2-7-1m55%200l-1%2010-1%209h6c5%200%206-1%206-3%200-3%200-3-3-3h-3v-7c0-5-1-7-2-7l-2%201m34%203l-1%2010v6h23v-9l2%204c2%204%203%205%205%205s2%200%202-10c0-9%200-9-2-9s-2%201-2%203v4l-2-4c-1-2-2-3-4-3-3%200-3%200-3%206l-1%207v-7c0-7%200-7-2-7-3%201-3%200-3%209v5h-8v-7c0-6%200-7-2-7s-2%200-2%204m33%200l-1%2010c0%206%200%206%203%206%202%200%202%200%202-3v-3l3%203c2%203%203%203%204%201h2c0%202%206%203%2011%201%204-1%203-4-2-4s-5-2%200-3l2-1v-2l-3-1c-4%200-4-2%201-3%204%200%205-1%205-3l-6-1-7%201h-2c-2-2-2-2-4%201-3%203-4%203-4%200%201-5-4-4-4%201m30-3c-2%200-3%205-3%2015%200%204%200%204%203%204%209%200%2015-14%208-19h-8m20%204l-1%2010v5h17v-5c2-12%201-15-2-15l-2%201v7c-1%206-1%206-5%206h-3v-7c0-6%200-7-2-7-1%200-2%201-2%205m35-4c-4%200-5%201-5%202h-1c-1-3-7-3-9%200-3%203-2%207%201%209%204%203%204%204%200%204-3-1-5%201-2%203%207%205%2014-4%207-10l-3-4%202%201c1%202%202%202%203%201%205-3%206-2%206%206%200%207%200%207%203%207%202%200%202%200%202-7%200-8%200-8%203-8%202%200%202%200%202-2-1-3-2-3-9-2M66%2096l-1%2010v4h5c6%200%208-2%208-7V92l-7-1h-5v5m26-4l2%208%202%208c0%202%200%202%202%202s2-1%202-3l1-4%203-7%202-5h-2c-2%200-3%201-4%203l-1%203-2-3c-1-3-5-4-5-2m152%201c-2%204-1%207%202%209%204%202%204%204%200%204h-3c-2%204%207%205%2010%202s3-5-1-8l-3-5%201%201c1%202%204%202%205%200%201-5-8-7-11-3m24%209c-3%203-5%2013-3%2017%201%202%204%203%205%201%201-1-1-9-3-10-4-1%202-9%208-9%207%200%209%205%206%2011-2%205-2%208%200%209%204%201%207-8%204-9l-1-4c0-8-10-12-16-6m31%204l-1%208v7h17l-5%205a66%2066%200%2000-19%2037l-2%2010v1l6-7c7-9%2017-15%2017-10%200%202%201%203%203%203l2-2-1-3h3c3%200%202%201%200%204l-2%204v3h-17v7c0%206%200%207-2%208-3%202-3%203%200%203%202%200%203%204%201%206l-6%201c-9%200-11-4-7-9l2-5c0-3-8%206-8%2010-1%203-1%203-10%204-8%200-8%200-8%202s1%203%205%203c5%200%206%200%204%205-1%202%201%202%2048%202h49l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2-1-2c-7-1-7-4-3-12%207-13%207-17%200-22-4-3-5-4-5-8l-1-4c-1-1-1%201-1%204%200%205%200%205-2%204-3-1-19%200-23%201l-3%201v-10l-14-1-13%201m2%208v6h22v-13h-22v7m-31%2027l1%209h12v-18h-13v9m55%2016c0%201-1%202-3%202s-2%201-2%204v3h6c9%200%209%200%208%207l1%208v2l-1%203c0%204%200%204%205%204%207%201%208%200%208-5l-1-6-2-4-2-5-1-3h-1v-2l-3-5c-1-2-3-3-7-4l-5-2v3m-52%207l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%204-2%203-2-2-1-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/2b32c7c0955d330cdf60c2e3e0c7adb3/ee604/blog24.png","srcSet":"/static/2b32c7c0955d330cdf60c2e3e0c7adb3/69585/blog24.png 200w,\n/static/2b32c7c0955d330cdf60c2e3e0c7adb3/497c6/blog24.png 400w,\n/static/2b32c7c0955d330cdf60c2e3e0c7adb3/ee604/blog24.png 800w,\n/static/2b32c7c0955d330cdf60c2e3e0c7adb3/f3583/blog24.png 1200w,\n/static/2b32c7c0955d330cdf60c2e3e0c7adb3/e4d72/blog24.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"doublyLinkedLists","thumbnail":"thumbnails/blog24.png"}},"staticQueryHashes":["2987289216","63159454"]}