{"id":26785,"date":"2017-12-24T15:27:52","date_gmt":"2017-12-24T09:57:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26785"},"modified":"2017-12-24T15:40:17","modified_gmt":"2017-12-24T10:10:17","slug":"c-algorithm-deleting-node-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-deleting-node-linked-list\/","title":{"rendered":"C++ Algorithm &#8211; Deleting a node in Linked List"},"content":{"rendered":"<p>We have discussed Linked List Introduction and Linked List Insertion in previous posts on singly linked list.<span id=\"more-142789\"><\/span><\/p>\n<p>Let us formulate the problem statement to understand the deletion process. <em>Given a \u2018key\u2019, delete the first occurrence of this key in linked list<\/em>.<br \/>\nTo delete a node from linked list, we need to do following steps.<br \/>\n1) Find previous node of the node to be deleted.<br \/>\n2) Changed next of previous node.<br \/>\n3) Free memory for the node to be deleted.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-26807\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_deletion-2-1.png\" alt=\"Deleting a node in Linked List | Set 3\" width=\"759\" height=\"243\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_deletion-2-1.png 759w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_deletion-2-1-300x96.png 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><br \/>\nSince every node of linked list is dynamically allocated using malloc() in C, we need to call <a href=\"http:\/\/www.cplusplus.com\/reference\/cstdlib\/free\/\" target=\"_blank\" rel=\"noopener noreferrer\">free()<\/a> for freeing memory allocated for the node to be deleted.<\/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 demonstrate deletion in singly<br\/>\/\/ linked list<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 key, deletes the first occurrence of key in linked list *\/<br\/>void deleteNode(struct node **head_ref, int key)<br\/>{<br\/>    \/\/ Store head node<br\/>    struct node* temp = *head_ref, *prev;<br\/> <br\/>    \/\/ If head node itself holds the key to be deleted<br\/>    if (temp != NULL &amp;&amp; temp-&gt;data == key)<br\/>    {<br\/>        *head_ref = temp-&gt;next;   \/\/ Changed head<br\/>        free(temp);               \/\/ free old head<br\/>        return;<br\/>    }<br\/> <br\/>    \/\/ Search for the key to be deleted, keep track of the<br\/>    \/\/ previous node as we need to change &#039;prev-&gt;next&#039;<br\/>    while (temp != NULL &amp;&amp; temp-&gt;data != key)<br\/>    {<br\/>        prev = temp;<br\/>        temp = temp-&gt;next;<br\/>    }<br\/> <br\/>    \/\/ If key was not present in linked list<br\/>    if (temp == NULL) return;<br\/> <br\/>    \/\/ Unlink the node from linked list<br\/>    prev-&gt;next = temp-&gt;next;<br\/> <br\/>    free(temp);  \/\/ Free memory<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\/> <br\/>    puts(&quot;Created Linked List: &quot;);<br\/>    printList(head);<br\/>    deleteNode(&amp;head, 1);<br\/>    puts(&quot;\\nLinked List after Deletion of 1: &quot;);<br\/>    printList(head);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output<\/strong>:<\/p>\n<pre>Created Linked List:\r\n 2  3  1  7\r\nLinked List after Deletion of 1:\r\n 2  3  7<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Cp++ Algorithm &#8211; Deleting a node in Linked List &#8211; Linked List &#8211; We have discussed Linked List Introduction and Linked List Insertion in previous<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,79476,79478],"tags":[79924,79917,79920,79922,79925,79918,79914,79916,79919,79921,79915,79923],"class_list":["post-26785","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-linked-list","category-singly-linked-list","tag-c-program-to-delete-first-node-in-linked-list","tag-delete-a-node-from-linked-list-algorithm","tag-delete-a-node-in-linked-list-with-single-pointer","tag-delete-a-node-in-singly-linked-list","tag-delete-a-node-in-the-middle-of-a-singly-linked-list","tag-delete-last-node-in-linked-list-in-c","tag-delete-node-at-given-position-in-a-linked-list","tag-delete-node-linked-list-c","tag-delete-specific-node-in-linked-list","tag-deletion-in-linked-list-in-data-structure","tag-linked-list-delete-node-java","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\/26785","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=26785"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26785\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}