{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/circularLinkedLists","result":{"data":{"blog":{"frontmatter":{"title":"CIRCULAR LINKED LISTS","thumbnail":"blog23","date":"December 1, 2020","dsaCppCodeFile":"https://drive.google.com/file/d/1zlFfsFMh_U9_xEMwktL7WWYCZoyScMxg/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is circular 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                  Similar to singly linked lists, circular linked lists are also\n                  one way list but the difference is that the last node contains\n                  the reference to the head node making it a circular.\n                </p>\n                <p>\n                  CLL do not have ends that is the last node reference field\n                  isn't NULL. While traversing the circular linked lists we\n                  should be careful, otherwise we will be traversing the list\n                  infinitely.\n                </p>\n                <p>\n                  In circular linked lists, each node has a successor which\n                  means to say that every node contains the link to next node.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>To implement circular queue.</li>\n                <li>\n                  Implementation of Advanced data structures like Fibonacci\n                  Heap.\n                </li>\n                <li>\n                  Used by the Operating system to share time for different\n                  users, generally uses a Round-Robin time-sharing mechanism.\n                </li>\n                <li>\n                  Multiplayer games use a circular list to swap between players\n                  in a loop.\n                </li>\n                <li>Music playlist which repeats endlessly playing songs.</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.\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                    head.\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 new node and initially keep its next pointer\n                    pointing to itself.\n                  </li>\n                  <li>\n                    The new node will be placed just after the tail node (last\n                    node of the list), which means it will have to be inserted\n                    in between the tail node and the first node.\n                  </li>\n                  <li>\n                    Traverse to the last node and change the next pointer, to\n                    point to the new node.\n                  </li>\n                  <li>Change the new node's next pointer, to point to head.</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                    Create a new node and initially keep its next pointer\n                    pointing to itself.\n                  </li>\n                  <li>\n                    Update the next pointer of new node, to point to the current\n                    head's next node.\n                  </li>\n                  <li>Update head's next pointer to point to the new node.</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 a node has to be inserted at a given position then we\n                    need to modify two next pointers.\n                  </li>\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                    Make the new node's next pointer point to the the next\n                    pointer of position node.\n                  </li>\n                  <li>\n                    Change the next pointer of the position node, to point to\n                    the 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                    Traverse and reach the tail node. Update the tail node's\n                    next pointer to point to next node of head\n                  </li>\n                  <li>Store the first node in temporary node pointer.</li>\n                  <li>\n                    Now, move the head node's pointer to the next node of\n                    temporary node and the first node will be unlinked with\n                    list.\n                  </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                    We need to find the node which is previous of the last node,\n                    so traverse the list and while traversing maintain the\n                    previous node address also.\n                  </li>\n                  <li>\n                    By the time we reach the end of the list, we will have two\n                    pointers, one pointing to the tail node and the other\n                    pointing to the node before the tail node.\n                  </li>\n                  <li>\n                    Update previous node’s next pointer to point to head and the\n                    last 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                    In this case, the node to be removed is always located\n                    between two nodes. Head and tail links are not updated in\n                    this case and such a removal can be done in two steps\n                  </li>\n                  <li>\n                    Similar to the previous case, maintain the previous node\n                    while traversing the list. Once we find the node to be\n                    deleted, change the previous node’s next pointer to the next\n                    pointer of the node to be deleted.\n                  </li>\n                  <li>\n                    The current node to be deleted will be unlinked with the\n                    list.\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              </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/doublyLinkedLists\">Doubly Linked Lists</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is circular 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                  Similar to singly linked lists, circular linked lists are also\n                  one way list but the difference is that the last node contains\n                  the reference to the head node making it a circular.\n                </p>\n                <p>\n                  CLL do not have ends that is the last node reference field\n                  isn't NULL. While traversing the circular linked lists we\n                  should be careful, otherwise we will be traversing the list\n                  infinitely.\n                </p>\n                <p>\n                  In circular linked lists, each node has a successor which\n                  means to say that every node contains the link to next node.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>To implement circular queue.</li>\n                <li>\n                  Implementation of Advanced data structures like Fibonacci\n                  Heap.\n                </li>\n                <li>\n                  Used by the Operating system to share time for different\n                  users, generally uses a Round-Robin time-sharing mechanism.\n                </li>\n                <li>\n                  Multiplayer games use a circular list to swap between players\n                  in a loop.\n                </li>\n                <li>Music playlist which repeats endlessly playing songs.</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.\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                    head.\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 new node and initially keep its next pointer\n                    pointing to itself.\n                  </li>\n                  <li>\n                    The new node will be placed just after the tail node (last\n                    node of the list), which means it will have to be inserted\n                    in between the tail node and the first node.\n                  </li>\n                  <li>\n                    Traverse to the last node and change the next pointer, to\n                    point to the new node.\n                  </li>\n                  <li>Change the new node's next pointer, to point to head.</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                    Create a new node and initially keep its next pointer\n                    pointing to itself.\n                  </li>\n                  <li>\n                    Update the next pointer of new node, to point to the current\n                    head's next node.\n                  </li>\n                  <li>Update head's next pointer to point to the new node.</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 a node has to be inserted at a given position then we\n                    need to modify two next pointers.\n                  </li>\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                    Make the new node's next pointer point to the the next\n                    pointer of position node.\n                  </li>\n                  <li>\n                    Change the next pointer of the position node, to point to\n                    the 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                    Traverse and reach the tail node. Update the tail node's\n                    next pointer to point to next node of head\n                  </li>\n                  <li>Store the first node in temporary node pointer.</li>\n                  <li>\n                    Now, move the head node's pointer to the next node of\n                    temporary node and the first node will be unlinked with\n                    list.\n                  </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                    We need to find the node which is previous of the last node,\n                    so traverse the list and while traversing maintain the\n                    previous node address also.\n                  </li>\n                  <li>\n                    By the time we reach the end of the list, we will have two\n                    pointers, one pointing to the tail node and the other\n                    pointing to the node before the tail node.\n                  </li>\n                  <li>\n                    Update previous node’s next pointer to point to head and the\n                    last 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                    In this case, the node to be removed is always located\n                    between two nodes. Head and tail links are not updated in\n                    this case and such a removal can be done in two steps\n                  </li>\n                  <li>\n                    Similar to the previous case, maintain the previous node\n                    while traversing the list. Once we find the node to be\n                    deleted, change the previous node’s next pointer to the next\n                    pointer of the node to be deleted.\n                  </li>\n                  <li>\n                    The current node to be deleted will be unlinked with the\n                    list.\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              </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/doublyLinkedLists\">Doubly 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='M27%2092c-7%205-6%2018%202%2018%203%200%208-5%206-7-1-1-2-1-3%201l-3%201v-8c1-3%203-3%202%200%200%202%200%202%202%202%204%200%204-7%200-8-3-1-3-1-6%201m11%200l-1%2010v8h5c5%200%205%200%206-3v-3l2%202%202%203h2c2-1%202-2%201-4v-6c3-6%201-9-7-8-4%200-4%200-4%204-1%203-1%203-1-1s0-4-2-4-3%201-3%202m25-1c-7%204-6%2019%200%2019%203%200%208-4%208-6s-2-2-4%200c-4%204-5%200-3-6%201-4%203-4%202-1%200%202%200%202%203%202%203-1%203-1%203%203%200%207%206%2010%2011%206%202-2%203-3%203-9%200-8%200-8-2-8-3%200-3%200-3%207%200%205-1%207-2%207l-1-7c0-7%200-7-2-7-3%200-3%200-3%204v4l-1-4c-1-4-6-6-9-4m25%200l-1%2010-1%209h9c7%200%209-1%209-2l1-1%202-1c2%200%202%200%202%202%200%201%201%202%203%202s2-1%201-10c-2-12-7-13-11-2-3%2010-4%2011-4%208%200-2-1-2-3-2h-3v-7c0-6%200-7-2-7l-2%201m32%200c-4%200-4%200-4%203l-1%209c0%207%200%207%202%207s3-1%203-3c0-4%201-4%203%200%201%203%201%203%204%202%202-1%202-2%200-4v-5c4-6%201-10-7-9m148%2011c-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%203s3-1%201-2l-1-3h4c3%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%204l-23%201-3%201v-10l-14-1-13%201m2%208v6h22v-13h-22v7M23%20132l-1%2010h23v-4c0-5%201-5%203%200%201%203%202%204%204%204s2%200%202-7l1-10c0-2%200-2-2-2-3%200-3%200-3%204v4l-2-4c-1-3-2-3-4-3s-3%200-3%207l-1%208v-8c0-8%200-8-3-8-2%200-2%200-1%201v1l-1%207v5h-7v-7c0-7%200-7-2-7-3%200-3%200-3%209m33-4c-2%2011-1%2014%202%2014%202%200%202%200%202-3s0-3%202-1l2%203h5l7%201c6%200%206%200%206-2%200-3%200-3-4-3l-4-1%202-1c3%200%205-2%205-3l-4-2c-4%200-3-2%202-2%203%200%204-1%204-3s0-2-6-2h-7v4l-1%208v4l-2-3-3-3%203-4c2-3%202-3%201-5-2-1-2-1-4%201l-3%202v-2l-2-2c-2%200-3%201-3%205m29-4l-1%2010v9l2-1c8-1%2010-4%2010-11l-1-7H85m20%208v10h22c9%200%2011-5%205-10l-2-4h1c0%202%203%202%207%200%202%200%202%201%202%207%200%207%200%207%203%207%202%200%202%200%202-7%200-6%200-7%202-7s3-1%203-3%200-2-7-2c-6%200-7%200-7%202h-2c-2-3-6-2-8%200-3%203-3%207%201%209%204%203%204%204%200%204-1-1-3%200-4%201v-7c0-9%200-9-2-9s-3%200-3%204c0%209-1%2010-5%2010h-3l1-7c0-7%200-7-2-7-3%200-3%200-4%209m48-6c-3%203-3%205%201%208s4%204%200%204c-3-1-5%201-3%203s8%201%2010-1c3-2%202-5-1-8l-2-2h2c2%200%203%200%203-2%200-5-6-7-10-2m117%2015l1%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%2010-1%207-7v-5l-1-3-3-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/748bc68a0d17eb38d7a8be78e6588200/ee604/blog23.png","srcSet":"/static/748bc68a0d17eb38d7a8be78e6588200/69585/blog23.png 200w,\n/static/748bc68a0d17eb38d7a8be78e6588200/497c6/blog23.png 400w,\n/static/748bc68a0d17eb38d7a8be78e6588200/ee604/blog23.png 800w,\n/static/748bc68a0d17eb38d7a8be78e6588200/f3583/blog23.png 1200w,\n/static/748bc68a0d17eb38d7a8be78e6588200/e4d72/blog23.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"circularLinkedLists","thumbnail":"thumbnails/blog23.png"}},"staticQueryHashes":["2987289216","63159454"]}