{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/singlyLinkedLists","result":{"data":{"blog":{"frontmatter":{"title":"SINGLY LINKED LISTS","thumbnail":"blog22","date":"November 28, 2020","dsaCppCodeFile":"https://drive.google.com/file/d/1AnjUA0o5RM5k8ND9dQTEpfP3XT4hXtgp/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is singly 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                  Generally \"linked list\" means a singly linked list. It is a\n                  one way list because we can only traverse this list in one\n                  direction, starting from the head node to the end.\n                </p>\n                <p>\n                  Each node in SLL consists of 1 data field and 1 reference\n                  field. As we have only one reference pointer pointing to the\n                  next node, we can only traverse in one direction starting from\n                  the head node to the end.\n                </p>\n                <p>\n                  The link of the last node in the list is NULL, which indicates\n                  the end of the list.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  SLL are mostly used in implementing stacks, queues and hash\n                  map.\n                </li>\n                <li>Message delivery on network.</li>\n                <li>Hyperlinks between different sections of pages in PDFs.</li>\n                <li>To evaluate a polynomial expression.</li>\n                <li>\n                  Functionality of brain in mugging up answers(We clearly know\n                  what word would come after any specific sentence).\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 theory.\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                    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                    We need to modify two next pointers (last nodes next pointer\n                    and new nodes next pointer).\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 NULL.</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                    Only one next pointer needs to be modified (new node’s next\n                    pointer) and it can be done in two steps.\n                  </li>\n                  <li>\n                    Update the next pointer of new node, to point to the current\n                    head.\n                  </li>\n                  <li>Update head pointer to point to the new node.</li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>4. Insert :</strong> Add a new node to a specific\n                  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                    Move the head node's pointer(first node) to the next node\n                    and the front node will be unlinked with the 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 with NULL and the last\n                    node will be unlinked with the list.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>5. Delete:</strong> Remove a node at specified\n                  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/circularLinkedLists\">Circular 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 singly 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                  Generally \"linked list\" means a singly linked list. It is a\n                  one way list because we can only traverse this list in one\n                  direction, starting from the head node to the end.\n                </p>\n                <p>\n                  Each node in SLL consists of 1 data field and 1 reference\n                  field. As we have only one reference pointer pointing to the\n                  next node, we can only traverse in one direction starting from\n                  the head node to the end.\n                </p>\n                <p>\n                  The link of the last node in the list is NULL, which indicates\n                  the end of the list.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  SLL are mostly used in implementing stacks, queues and hash\n                  map.\n                </li>\n                <li>Message delivery on network.</li>\n                <li>Hyperlinks between different sections of pages in PDFs.</li>\n                <li>To evaluate a polynomial expression.</li>\n                <li>\n                  Functionality of brain in mugging up answers(We clearly know\n                  what word would come after any specific sentence).\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 theory.\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                    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                    We need to modify two next pointers (last nodes next pointer\n                    and new nodes next pointer).\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 NULL.</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                    Only one next pointer needs to be modified (new node’s next\n                    pointer) and it can be done in two steps.\n                  </li>\n                  <li>\n                    Update the next pointer of new node, to point to the current\n                    head.\n                  </li>\n                  <li>Update head pointer to point to the new node.</li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>4. Insert :</strong> Add a new node to a specific\n                  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                    Move the head node's pointer(first node) to the next node\n                    and the front node will be unlinked with the 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 with NULL and the last\n                    node will be unlinked with the list.\n                  </li>\n                </ul>\n                <p class=\"mb-0\">\n                  <strong>5. Delete:</strong> Remove a node at specified\n                  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/circularLinkedLists\">Circular 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='M52%2091l-1%203v4l-1-4-4-3c-3%200-4%203-4%2013%200%206%200%206%203%206%202%200%202%200%202-5v-4l2%204c1%204%202%205%204%205s3-1%203-10c0-8%200-9-2-9h-2m10%200c-3%202-5%206-5%2011%200%208%207%2011%2011%205%204-4%202-9-3-8-2%201-2%201-2%204v2c-2%200-1-8%201-9h1c0%201%201%202%203%202s2%200%202-3c-1-4-4-5-8-4m10%204l-1%2010v5h6c5%200%206-1%206-3%200-3%200-3-4-3h-3v-7c0-6%200-7-2-7-1%200-2%201-2%205m36%200l-1%2010v5h18V91c0-1-4-1-5%201a311%20311%200%2000-1%2014c0-2-1-2-4-2h-3v-7c0-6%200-7-2-7-1%200-2%201-2%205m28-4l-1%203v4l-1-4-4-3c-3%200-4%203-4%2013%200%206%200%206%203%206%202%200%202%200%202-4v-4l2%204c4%208%207%203%207-12%200-3-3-5-4-3m6%201l-1%2010c0%208%200%208%203%208%202%200%202%200%202-3s0-3%202-1l2%203h3l2-1c0%202%206%203%2011%201%204-1%203-4-2-4s-5-2%200-3c4-1%204-4%200-4-6%200-5-2%201-3%203%200%204-1%204-2%200-2-1-2-6-2h-7l-1%208v8l-3-4-2-3%203-4c2-3%202-3%201-5-2-1-2-1-4%201-3%204-4%204-4%201s-3-4-4-1m31-1c-2%201-3%201-3%209-1%2010-1%2010%203%2010%206%200%2010-5%2010-12%200-6-4-9-10-7m20%202l-1%2010v7h6c5%200%206-1%206-3%200-3%200-3-3-3h-4v-7c0-6%200-7-2-7s-2%200-2%203m36-2c-5%200-5%200-5%203%200%202%201%202%203%202s2%200%202%207c-1%207-1%207%202%207%202%200%203-4%203-12%200-2%200-3%202-3l2-2c0-2-1-3-2-3l-7%201M25%2093c-3%203-2%206%201%208%204%204%204%205%200%205-4-1-5%202-1%203%203%201%206%201%208-1%203-3%202-6-1-8l-3-4h1c1%202%204%202%205%200%201-5-8-7-10-3m59-1l2%207%202%208c0%203%200%203%202%203s2-1%202-2l4-12%202-5h-2c-2%200-3%201-4%203l-1%203-2-3c-1-3-5-4-5-2m130%201c-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%201c2%203%204%201%204-2-1-3-7-4-9-1m28%200c-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%205%202%205%200%200-5-7-7-10-3m26%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/f329df0dd1143dafb97a2388e3e539af/ee604/blog22.png","srcSet":"/static/f329df0dd1143dafb97a2388e3e539af/69585/blog22.png 200w,\n/static/f329df0dd1143dafb97a2388e3e539af/497c6/blog22.png 400w,\n/static/f329df0dd1143dafb97a2388e3e539af/ee604/blog22.png 800w,\n/static/f329df0dd1143dafb97a2388e3e539af/f3583/blog22.png 1200w,\n/static/f329df0dd1143dafb97a2388e3e539af/e4d72/blog22.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"singlyLinkedLists","thumbnail":"thumbnails/blog22.png"}},"staticQueryHashes":["2987289216","63159454"]}