{"id":27667,"date":"2018-04-09T20:16:05","date_gmt":"2018-04-09T14:46:05","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27667"},"modified":"2018-09-16T13:26:21","modified_gmt":"2018-09-16T07:56:21","slug":"c-algorithm-move-last-element-front-given-linked-list-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-move-last-element-front-given-linked-list-2\/","title":{"rendered":"C Algorithm &#8211; Move last element to front of a given Linked List"},"content":{"rendered":"<p>Write a C function that moves last element to front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4.<span id=\"more-6850\"><\/span><\/p>\n<p>Algorithm:<br \/>\nTraverse the list till last node. Use two pointers: one to store the address of last node and other for address of second last node. After the end of loop do following operations.<br \/>\ni) Make second last as last (secLast->next = NULL).<br \/>\nii) Set next of last as head (last->next = *head_ref).<br \/>\niii) Make last as head ( *head_ref = last)<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F*%20C%20Program%20to%20move%20last%20element%20to%20front%20in%20a%20given%20linked%20list%20*%2F%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20linked%20list%20node%20*%2F%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*%20We%20are%20using%20a%20double%20pointer%20head_ref%20here%20because%20we%20change%0A%20%20%20head%20of%20the%20linked%20list%20inside%20this%20function.*%2F%0Avoid%20moveToFront(struct%20node%20**head_ref)%0A%7B%0A%20%20%20%20%2F*%20If%20linked%20list%20is%20empty%2C%20or%20it%20contains%20only%20one%20node%2C%0A%20%20%20%20%20%20then%20nothing%20needs%20to%20be%20done%2C%20simply%20return%20*%2F%0A%20%20%20%20if%20(*head_ref%20%3D%3D%20NULL%20%7C%7C%20(*head_ref)-%3Enext%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%2F*%20Initialize%20second%20last%20and%20last%20pointers%20*%2F%0A%20%20%20%20struct%20node%20*secLast%20%3D%20NULL%3B%0A%20%20%20%20struct%20node%20*last%20%3D%20*head_ref%3B%0A%20%0A%20%20%20%20%2F*After%20this%20loop%20secLast%20contains%20address%20of%20second%20last%0A%20%20%20%20node%20and%20last%20contains%20address%20of%20last%20node%20in%20Linked%20List%20*%2F%0A%20%20%20%20while%20(last-%3Enext%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20secLast%20%3D%20last%3B%0A%20%20%20%20%20%20%20%20last%20%3D%20last-%3Enext%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Set%20the%20next%20of%20second%20last%20as%20NULL%20*%2F%0A%20%20%20%20secLast-%3Enext%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20Set%20next%20of%20last%20as%20head%20node%20*%2F%0A%20%20%20%20last-%3Enext%20%3D%20*head_ref%3B%0A%20%0A%20%20%20%20%2F*%20Change%20the%20head%20pointer%20to%20point%20to%20last%20node%20now%20*%2F%0A%20%20%20%20*head_ref%20%3D%20last%3B%0A%7D%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20*%2F%0A%2F*%20Function%20to%20add%20a%20node%20at%20the%20begining%20of%20Linked%20List%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20linked%20list%20*%2F%0Avoid%20printList(struct%20node%20*node)%0A%7B%0A%20%20%20%20while(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%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*%20Druver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20struct%20node%20*start%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20The%20constructed%20linked%20list%20is%3A%0A%20%20%20%20%201-%3E2-%3E3-%3E4-%3E5%20*%2F%0A%20%20%20%20push(%26start%2C%205)%3B%0A%20%20%20%20push(%26start%2C%204)%3B%0A%20%20%20%20push(%26start%2C%203)%3B%0A%20%20%20%20push(%26start%2C%202)%3B%0A%20%20%20%20push(%26start%2C%201)%3B%0A%20%0A%20%20%20%20printf(%22%5Cn%20Linked%20list%20before%20moving%20last%20to%20front%5Cn%22)%3B%0A%20%20%20%20printList(start)%3B%0A%20%0A%20%20%20%20moveToFront(%26start)%3B%0A%20%0A%20%20%20%20printf(%22%5Cn%20Linked%20list%20after%20removing%20last%20to%20front%5Cn%22)%3B%0A%20%20%20%20printList(start)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre> Linked list before moving last to front \r\n1 2 3 4 5 \r\n Linked list after removing last to front \r\n5 1 2 3<\/pre>\n<p>Time Complexity: O(n) where n is the number of nodes in the given Linked List.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Move last element to front of a given Linked List &#8211; Linked List &#8211; Write a C function that moves last element to front in a given Singly<\/p>\n","protected":false},"author":1,"featured_media":31285,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81917,81924,81919,81920,81922,81925,81918,81586,81923,81921],"class_list":["post-27667","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-java-linked-list-move-element-to-front","tag-java-list-move-element-to-another-position","tag-move-last-element-to-front-of-a-given-linked-list-java","tag-move-node-from-one-linked-list-to-another","tag-move-to-front-java","tag-sorted-vector","tag-swap-every-two-nodes-in-a-linked-list","tag-swapping-two-nodes-in-a-singly-linked-list","tag-unsorted-linked-list","tag-write-a-method-to-add-two-polynomial-equations-using-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27667","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=27667"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27667\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31285"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}