Write a C function to insert a new value in a sorted Circular Linked List (CLL). For example, if the input CLL is following:

Sorted insert for circular linked list

After insertion of 7, the above CLL should be changed to following

Sorted insert for circular linked list

Algorithm:
Allocate 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.

1) Linked List is empty:  
    a)  since new_node is the only node in CLL, make a self loop.      
          new_node->next = new_node;  
    b) change the head pointer to point to new node.
          *head_ref = new_node;
2) New node is to be inserted just before the head node:    
  (a) Find out the last node using a loop.
         while(current->next != *head_ref)
            current = current->next;
  (b) Change the next of last node. 
         current->next = new_node;
  (c) Change next of new node to point to head.
         new_node->next = *head_ref;
  (d) change the head pointer to point to new node.
         *head_ref = new_node;
3) New node is to be  inserted somewhere after the head: 
   (a) Locate the node after which new node is to be inserted.
         while ( current->next!= *head_ref && 
             current->next->data < new_node->data)
         {   current = current->next;   }
   (b) Make next of new_node as next of the located pointer
         new_node->next = current->next;
   (c) Change the next of the located pointer
         current->next = new_node;
[ad type=”banner”]

C Programming:

[pastacode lang=”c” manual=”%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’s%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” message=”” highlight=”” provider=”manual”/]

Output:

1 2 11 12 56 90

Time Complexity: O(n) where n is the number of nodes in the given linked list.

Case 2 of the above algorithm/code can be optimized. Please see this comment from Pavan. To implement the suggested change we need to modify the case 2 to following.

[pastacode lang=”c” manual=”%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” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]