{"id":26848,"date":"2017-12-24T15:55:16","date_gmt":"2017-12-24T10:25:16","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26848"},"modified":"2017-12-24T15:55:16","modified_gmt":"2017-12-24T10:25:16","slug":"c-algorithm-delete-linked-list-node-given-position","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-delete-linked-list-node-given-position\/","title":{"rendered":"Cpp Algorithm &#8211; Delete a Linked List node at a given position"},"content":{"rendered":"<p>Given a singly linked list and a position, delete a linked list node at the given position.<span id=\"more-142796\"><\/span><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre>Input: position = 1, Linked List = 8-&gt;2-&gt;3-&gt;1-&gt;7\r\nOutput: Linked List =  8-&gt;3-&gt;1-&gt;7\r\n\r\nInput: position = 0, Linked List = 8-&gt;2-&gt;3-&gt;1-&gt;7\r\nOutput: Linked List = 2-&gt;3-&gt;1-&gt;7<\/pre>\n<p>If node to be deleted is root, simply delete it. To delete a middle node, we must have pointer to the node previous to the node to be deleted. So if positions is not zero, we run a loop position-1 times and get pointer to the previous node.<\/p>\n<p><strong>C++ Programming:<\/strong><\/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\">\/\/ A complete working C program to delete a node in a linked list<br\/>\/\/ at a given position<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\/>};<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\/>    struct node* new_node = (struct node*) malloc(sizeof(struct node));<br\/>    new_node-&gt;data  = new_data;<br\/>    new_node-&gt;next = (*head_ref);<br\/>    (*head_ref)    = new_node;<br\/>}<br\/> <br\/>\/* Given a reference (pointer to pointer) to the head of a list<br\/>   and a position, deletes the node at the given position *\/<br\/>void deleteNode(struct node **head_ref, int position)<br\/>{<br\/>   \/\/ If linked list is empty<br\/>   if (*head_ref == NULL)<br\/>      return;<br\/> <br\/>   \/\/ Store head node<br\/>   struct node* temp = *head_ref;<br\/> <br\/>    \/\/ If head needs to be removed<br\/>    if (position == 0)<br\/>    {<br\/>        *head_ref = temp-&gt;next;   \/\/ Change head<br\/>        free(temp);               \/\/ free old head<br\/>        return;<br\/>    }<br\/> <br\/>    \/\/ Find previous node of the node to be deleted<br\/>    for (int i=0; temp!=NULL &amp;&amp; i&lt;position-1; i++)<br\/>         temp = temp-&gt;next;<br\/> <br\/>    \/\/ If position is more than number of ndoes<br\/>    if (temp == NULL || temp-&gt;next == NULL)<br\/>         return;<br\/> <br\/>    \/\/ Node temp-&gt;next is the node to be deleted<br\/>    \/\/ Store pointer to the next of node to be deleted<br\/>    struct node *next = temp-&gt;next-&gt;next;<br\/> <br\/>    \/\/ Unlink the node from linked list<br\/>    free(temp-&gt;next);  \/\/ Free memory<br\/> <br\/>    temp-&gt;next = next;  \/\/ Unlink the deleted node from list<br\/>}<br\/> <br\/>\/\/ This function prints contents of linked list starting from<br\/>\/\/ the given node<br\/>void printList(struct node *node)<br\/>{<br\/>    while (node != NULL)<br\/>    {<br\/>        printf(&quot; %d &quot;, node-&gt;data);<br\/>        node = node-&gt;next;<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\/>    push(&amp;head, 7);<br\/>    push(&amp;head, 1);<br\/>    push(&amp;head, 3);<br\/>    push(&amp;head, 2);<br\/>    push(&amp;head, 8);<br\/> <br\/>    puts(&quot;Created Linked List: &quot;);<br\/>    printList(head);<br\/>    deleteNode(&amp;head, 4);<br\/>    puts(&quot;\\nLinked List after Deletion at position 4: &quot;);<br\/>    printList(head);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Created Linked List: \r\n 8  2  3  1  7 \r\nLinked List after Deletion at position 4: \r\n 8  2  3  1<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Cpp Algorithm &#8211; Delete a Linked List node at a given position &#8211; Linked List &#8211; Given a singly linked list and a position, delete a linked list<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,79476,79478],"tags":[83607,83605,83609,83608,83604,83606,83716,83610,83711,83712,83715,83713,83697,83714,79919,79923],"class_list":["post-26848","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-linked-list","category-singly-linked-list","tag-algorithm-c-definition","tag-algorithm-c-example","tag-c-algorithm-book","tag-c-algorithm-pdf","tag-c-algorithm-sort","tag-c-algorithm-tutorial","tag-c-stl-algorithm-cheat-sheet","tag-c-stl-algorithm-tutorial","tag-delete-node-at-given-position-in-a-linked-list-in-c","tag-delete-node-at-given-position-in-a-linked-list-java","tag-delete-node-at-given-position-in-a-singly-linked-list","tag-delete-node-from-singly-linked-list-in-c","tag-delete-nth-node-in-linked-list-c","tag-delete-nth-position-in-linked-list-using-c","tag-delete-specific-node-in-linked-list","tag-write-a-program-to-delete-a-node-from-linked-list-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26848","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=26848"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26848\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}