{"id":26941,"date":"2017-12-28T21:43:00","date_gmt":"2017-12-28T16:13:00","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26941"},"modified":"2017-12-28T21:43:00","modified_gmt":"2017-12-28T16:13:00","slug":"sorted-insert-circular-linked-list-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/sorted-insert-circular-linked-list-2\/","title":{"rendered":"Sorted insert for circular linked list"},"content":{"rendered":"<p>Write a C function to insert a new value in a sorted Circular Linked List (CLL). For example, if the input CLL is following:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26957\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll1.png\" alt=\"Sorted insert for circular linked list\" width=\"1002\" height=\"180\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll1.png 1002w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll1-300x54.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll1-768x138.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll1-990x178.png 990w\" sizes=\"(max-width: 1002px) 100vw, 1002px\" \/><\/p>\n<p>After insertion of <strong>7<\/strong>, the above CLL should be changed to following<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26959\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_inserted-2.png\" alt=\"Sorted insert for circular linked list\" width=\"887\" height=\"187\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_inserted-2.png 887w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_inserted-2-300x63.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_inserted-2-768x162.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/p>\n<p><strong>Algorithm:<\/strong><br \/>\nAllocate memory for the newly inserted node and put data in the newly allocated node. Let the pointer to the new node be new_node. After memory allocation, following are the three cases that need to be handled.<\/p>\n<pre>1) <em>Linked List is empty:<\/em>  \r\n    a)  since new_node is the only node in CLL, make a self loop.      \r\n          new_node->next = new_node;  \r\n    b) change the head pointer to point to new node.\r\n          *head_ref = new_node;\r\n2) <em>New node is to be inserted just before the head node:<\/em>    \r\n  (a) Find out the last node using a loop.\r\n         while(current->next != *head_ref)\r\n            current = current->next;\r\n  (b) Change the next of last node. \r\n         current->next = new_node;\r\n  (c) Change next of new node to point to head.\r\n         new_node->next = *head_ref;\r\n  (d) change the head pointer to point to new node.\r\n         *head_ref = new_node;\r\n3) <em>New node is to be  inserted somewhere after the head: <\/em>\r\n   (a) Locate the node after which new node is to be inserted.\r\n         while ( current->next!= *head_ref && \r\n             current->next->data < new_node->data)\r\n         {   current = current->next;   }\r\n   (b) Make next of new_node as next of the located pointer\r\n         new_node->next = current->next;\r\n   (c) Change the next of the located pointer\r\n         current->next = new_node;<\/pre>\n[ad type=\u201dbanner\u201d]\n<p>C Programming:<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20structure%20for%20a%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20function%20to%20insert%20a%20new_node%20in%20a%20list%20in%20sorted%20way.%0A%20%20%20Note%20that%20this%20function%20expects%20a%20pointer%20to%20head%20node%0A%20%20%20as%20this%20can%20modify%20the%20head%20of%20the%20input%20linked%20list%20*%2F%0Avoid%20sortedInsert(struct%20node**%20head_ref%2C%20struct%20node*%20new_node)%0A%7B%0A%20%20struct%20node*%20current%20%3D%20*head_ref%3B%0A%20%0A%20%20%2F%2F%20Case%201%20of%20the%20above%20algo%0A%20%20if%20(current%20%3D%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20%20new_node-%3Enext%20%3D%20new_node%3B%0A%20%20%20%20%20*head_ref%20%3D%20new_node%3B%0A%20%20%7D%0A%20%0A%20%20%2F%2F%20Case%202%20of%20the%20above%20algo%0A%20%20else%20if%20(current-%3Edata%20%3E%3D%20new_node-%3Edata)%0A%20%20%7B%0A%20%20%20%20%2F*%20If%20value%20is%20smaller%20than%20head\u2019s%20value%20then%0A%20%20%20%20%20%20we%20need%20to%20change%20next%20of%20last%20node%20*%2F%0A%20%20%20%20while(current-%3Enext%20!%3D%20*head_ref)%0A%20%20%20%20%20%20%20%20current%20%3D%20current-%3Enext%3B%0A%20%20%20%20current-%3Enext%20%3D%20new_node%3B%0A%20%20%20%20new_node-%3Enext%20%3D%20*head_ref%3B%0A%20%20%20%20*head_ref%20%3D%20new_node%3B%0A%20%20%7D%0A%20%0A%20%20%2F%2F%20Case%203%20of%20the%20above%20algo%0A%20%20else%0A%20%20%7B%0A%20%20%20%20%2F*%20Locate%20the%20node%20before%20the%20point%20of%20insertion%20*%2F%0A%20%20%20%20while%20(current-%3Enext!%3D%20*head_ref%20%26%26%20%0A%20%20%20%20%20%20%20%20%20%20%20current-%3Enext-%3Edata%20%3C%20new_node-%3Edata)%0A%20%20%20%20%20%20current%20%3D%20current-%3Enext%3B%0A%20%0A%20%20%20%20new_node-%3Enext%20%3D%20current-%3Enext%3B%0A%20%20%20%20current-%3Enext%20%3D%20new_node%3B%0A%20%20%7D%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20linked%20list%20*%2F%0Avoid%20printList(struct%20node%20*start)%0A%7B%0A%20%20struct%20node%20*temp%3B%0A%20%0A%20%20if(start%20!%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20temp%20%3D%20start%3B%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20do%20%7B%0A%20%20%20%20%20%20printf(%22%25d%20%22%2C%20temp-%3Edata)%3B%0A%20%20%20%20%20%20temp%20%3D%20temp-%3Enext%3B%0A%20%20%20%20%7D%20while(temp%20!%3D%20start)%3B%0A%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20int%20arr%5B%5D%20%3D%20%7B12%2C%2056%2C%202%2C%2011%2C%201%2C%2090%7D%3B%0A%20%20int%20list_size%2C%20i%3B%0A%20%0A%20%20%2F*%20start%20with%20empty%20linked%20list%20*%2F%0A%20%20struct%20node%20*start%20%3D%20NULL%3B%0A%20%20struct%20node%20*temp%3B%0A%20%0A%20%20%2F*%20Create%20linked%20list%20from%20the%20array%20arr%5B%5D.%0A%20%20%20%20Created%20linked%20list%20will%20be%201-%3E2-%3E11-%3E12-%3E56-%3E90%20*%2F%0A%20%20for%20(i%20%3D%200%3B%20i%3C%206%3B%20i%2B%2B)%0A%20%20%7B%0A%20%20%20%20temp%20%3D%20(struct%20node%20*)malloc(sizeof(struct%20node))%3B%0A%20%20%20%20temp-%3Edata%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20sortedInsert(%26start%2C%20temp)%3B%0A%20%20%7D%0A%20%0A%20%20printList(start)%3B%0A%20%0A%20%20return%200%3B%0A%7D%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>1 2 11 12 56 90<\/pre>\n<p>Time Complexity: O(n) where n is the number of nodes in the given linked list.<\/p>\n<p>Case 2 of the above algorithm\/code can be optimized. Please see <a href=\"http:\/\/www.geeksforgeeks.org\/archives\/10846\/comment-page-1#comment-3405\">this <\/a>comment from Pavan. To implement the suggested change we need to modify the case 2 to following.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20Case%202%20of%20the%20above%20algo%0Aelse%20if%20(current-%3Edata%20%3E%3D%20new_node-%3Edata)%0A%7B%0A%20%20%2F%2F%20swap%20the%20data%20part%20of%20head%20node%20and%20new%20node%0A%20%20%2F%2F%20assuming%20that%20we%20have%20a%20function%20swap(int%20*%2C%20int%20*)%0A%20%20swap(%26(current-%3Edata)%2C%20%26(new_node-%3Edata))%3B%20%0A%20%0A%20%20new_node-%3Enext%20%3D%20(*head_ref)-%3Enext%3B%0A%20%20(*head_ref)-%3Enext%20%3D%20new_node%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Sorted insert for circular linked list &#8211; Circular Linked List &#8211; Write a C function to insert a new value in a sorted Circular 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":[79477,79476],"tags":[83775,80096,80433,83778,80434,80115,80432,80436,83773,83774,80435,83777,80437,83776,83779],"class_list":["post-26941","post","type-post","status-publish","format-standard","hentry","category-circular-linked-list","category-linked-list","tag-amcat-automata-questions-with-answers","tag-circular-linked-list-algorithm-pdf","tag-circular-linked-list-geeksforgeeks","tag-circular-linked-list-in-c-geeksforgeeks","tag-circular-linked-list-insert-at-beginning","tag-circular-linked-list-insertion-and-deletion-program","tag-deletion-in-circular-linked-list","tag-insert-a-node-in-a-sorted-linked-list-c","tag-insert-into-a-cyclic-sorted-list-leetcode","tag-insert-into-cycle-linked-list-leetcode","tag-insert-into-sorted-linked-list-java","tag-insert-sorted-list-amcat","tag-insertion-sort-linked-list-c-code","tag-mooshak-the-mouse-has-been-placed-in-a-maze-there-is-a-huge-chunk-of-cheese-somewhere-in-the-maze","tag-write-ac-function-to-insert-a-new-value-in-a-sorted-circular-linked-list-cll"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26941","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=26941"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26941\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}