{"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-&gt;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-&gt;next != *head_ref)\r\n            current = current-&gt;next;\r\n  (b) Change the next of last node. \r\n         current-&gt;next = new_node;\r\n  (c) Change next of new node to point to head.\r\n         new_node-&gt;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-&gt;next!= *head_ref &amp;&amp; \r\n             current-&gt;next-&gt;data &lt; new_node-&gt;data)\r\n         {   current = current-&gt;next;   }\r\n   (b) Make next of new_node as next of the located pointer\r\n         new_node-&gt;next = current-&gt;next;\r\n   (c) Change the next of the located pointer\r\n         current-&gt;next = new_node;<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>C Programming:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* structure for a node *\/<br\/>struct node<br\/>{<br\/>  int data;<br\/>  struct node *next;<br\/>};<br\/> <br\/>\/* function to insert a new_node in a list in sorted way.<br\/>   Note that this function expects a pointer to head node<br\/>   as this can modify the head of the input linked list *\/<br\/>void sortedInsert(struct node** head_ref, struct node* new_node)<br\/>{<br\/>  struct node* current = *head_ref;<br\/> <br\/>  \/\/ Case 1 of the above algo<br\/>  if (current == NULL)<br\/>  {<br\/>     new_node-&gt;next = new_node;<br\/>     *head_ref = new_node;<br\/>  }<br\/> <br\/>  \/\/ Case 2 of the above algo<br\/>  else if (current-&gt;data &gt;= new_node-&gt;data)<br\/>  {<br\/>    \/* If value is smaller than head&#039;s value then<br\/>      we need to change next of last node *\/<br\/>    while(current-&gt;next != *head_ref)<br\/>        current = current-&gt;next;<br\/>    current-&gt;next = new_node;<br\/>    new_node-&gt;next = *head_ref;<br\/>    *head_ref = new_node;<br\/>  }<br\/> <br\/>  \/\/ Case 3 of the above algo<br\/>  else<br\/>  {<br\/>    \/* Locate the node before the point of insertion *\/<br\/>    while (current-&gt;next!= *head_ref &amp;&amp; <br\/>           current-&gt;next-&gt;data &lt; new_node-&gt;data)<br\/>      current = current-&gt;next;<br\/> <br\/>    new_node-&gt;next = current-&gt;next;<br\/>    current-&gt;next = new_node;<br\/>  }<br\/>}<br\/> <br\/>\/* Function to print nodes in a given linked list *\/<br\/>void printList(struct node *start)<br\/>{<br\/>  struct node *temp;<br\/> <br\/>  if(start != NULL)<br\/>  {<br\/>    temp = start;<br\/>    printf(&quot;\\n&quot;);<br\/>    do {<br\/>      printf(&quot;%d &quot;, temp-&gt;data);<br\/>      temp = temp-&gt;next;<br\/>    } while(temp != start);<br\/>  }<br\/>}<br\/> <br\/>\/* Driver program to test above functions *\/<br\/>int main()<br\/>{<br\/>  int arr[] = {12, 56, 2, 11, 1, 90};<br\/>  int list_size, i;<br\/> <br\/>  \/* start with empty linked list *\/<br\/>  struct node *start = NULL;<br\/>  struct node *temp;<br\/> <br\/>  \/* Create linked list from the array arr[].<br\/>    Created linked list will be 1-&gt;2-&gt;11-&gt;12-&gt;56-&gt;90 *\/<br\/>  for (i = 0; i&lt; 6; i++)<br\/>  {<br\/>    temp = (struct node *)malloc(sizeof(struct node));<br\/>    temp-&gt;data = arr[i];<br\/>    sortedInsert(&amp;start, temp);<br\/>  }<br\/> <br\/>  printList(start);<br\/> <br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\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\" target=\"_blank\" rel=\"noopener\">this <\/a>comment from Pavan. To implement the suggested change we need to modify the case 2 to following.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Case 2 of the above algo<br\/>else if (current-&gt;data &gt;= new_node-&gt;data)<br\/>{<br\/>  \/\/ swap the data part of head node and new node<br\/>  \/\/ assuming that we have a function swap(int *, int *)<br\/>  swap(&amp;(current-&gt;data), &amp;(new_node-&gt;data)); <br\/> <br\/>  new_node-&gt;next = (*head_ref)-&gt;next;<br\/>  (*head_ref)-&gt;next = new_node;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}