{"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->2->3->1->7\r\nOutput: Linked List =  8->3->1->7\r\n\r\nInput: position = 0, Linked List = 8->2->3->1->7\r\nOutput: Linked List = 2->3->1->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[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20A%20complete%20working%20C%20program%20to%20delete%20a%20node%20in%20a%20linked%20list%0A%2F%2F%20at%20a%20given%20position%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20A%20linked%20list%20node%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%20of%20a%20list%0A%20%20%20and%20an%20int%2C%20inserts%20a%20new%20node%20on%20the%20front%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%20of%20a%20list%0A%20%20%20and%20a%20position%2C%20deletes%20the%20node%20at%20the%20given%20position%20*%2F%0Avoid%20deleteNode(struct%20node%20**head_ref%2C%20int%20position)%0A%7B%0A%20%20%20%2F%2F%20If%20linked%20list%20is%20empty%0A%20%20%20if%20(*head_ref%20%3D%3D%20NULL)%0A%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%2F%2F%20Store%20head%20node%0A%20%20%20struct%20node*%20temp%20%3D%20*head_ref%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20head%20needs%20to%20be%20removed%0A%20%20%20%20if%20(position%20%3D%3D%200)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20*head_ref%20%3D%20temp-%3Enext%3B%20%20%20%2F%2F%20Change%20head%0A%20%20%20%20%20%20%20%20free(temp)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20free%20old%20head%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Find%20previous%20node%20of%20the%20node%20to%20be%20deleted%0A%20%20%20%20for%20(int%20i%3D0%3B%20temp!%3DNULL%20%26%26%20i%3Cposition-1%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20temp%20%3D%20temp-%3Enext%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20position%20is%20more%20than%20number%20of%20ndoes%0A%20%20%20%20if%20(temp%20%3D%3D%20NULL%20%7C%7C%20temp-%3Enext%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%2F%2F%20Node%20temp-%3Enext%20is%20the%20node%20to%20be%20deleted%0A%20%20%20%20%2F%2F%20Store%20pointer%20to%20the%20next%20of%20node%20to%20be%20deleted%0A%20%20%20%20struct%20node%20*next%20%3D%20temp-%3Enext-%3Enext%3B%0A%20%0A%20%20%20%20%2F%2F%20Unlink%20the%20node%20from%20linked%20list%0A%20%20%20%20free(temp-%3Enext)%3B%20%20%2F%2F%20Free%20memory%0A%20%0A%20%20%20%20temp-%3Enext%20%3D%20next%3B%20%20%2F%2F%20Unlink%20the%20deleted%20node%20from%20list%0A%7D%0A%20%0A%2F%2F%20This%20function%20prints%20contents%20of%20linked%20list%20starting%20from%0A%2F%2F%20the%20given%20node%0Avoid%20printList(struct%20node%20*node)%0A%7B%0A%20%20%20%20while%20(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%20%25d%20%22%2C%20node-%3Edata)%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20above%20functions*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%0A%20%20%20%20push(%26head%2C%207)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%20push(%26head%2C%203)%3B%0A%20%20%20%20push(%26head%2C%202)%3B%0A%20%20%20%20push(%26head%2C%208)%3B%0A%20%0A%20%20%20%20puts(%22Created%20Linked%20List%3A%20%22)%3B%0A%20%20%20%20printList(head)%3B%0A%20%20%20%20deleteNode(%26head%2C%204)%3B%0A%20%20%20%20puts(%22%5CnLinked%20List%20after%20Deletion%20at%20position%204%3A%20%22)%3B%0A%20%20%20%20printList(head)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}