Split a Circular Linked List into two halves

Original Linked List

Split a Circular Linked List into two halves

Result Linked List 1

Split a Circular Linked List into two halves

Result Linked List 2

C Programming

[pastacode lang=”c” manual=”%2F*%20Program%20to%20split%20a%20circular%20linked%20list%20into%20two%20halves%20*%2F%0A%23include%3Cstdio.h%3E%20%0A%23include%3Cstdlib.h%3E%20%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%20%0A%20%0A%2F*%20Function%20to%20split%20a%20list%20(starting%20with%20head)%20into%20two%20lists.%0A%20%20%20head1_ref%20and%20head2_ref%20are%20references%20to%20head%20nodes%20of%20%0A%20%20%20%20the%20two%20resultant%20linked%20lists%20*%2F%0Avoid%20splitList(struct%20node%20*head%2C%20struct%20node%20**head1_ref%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20struct%20node%20**head2_ref)%0A%7B%0A%20%20struct%20node%20*slow_ptr%20%3D%20head%3B%0A%20%20struct%20node%20*fast_ptr%20%3D%20head%3B%20%0A%20%0A%20%20if(head%20%3D%3D%20NULL)%0A%20%20%20%20return%3B%0A%20%20%0A%20%20%2F*%20If%20there%20are%20odd%20nodes%20in%20the%20circular%20list%20then%0A%20%20%20%20%20fast_ptr-%3Enext%20becomes%20head%20and%20for%20even%20nodes%20%0A%20%20%20%20%20fast_ptr-%3Enext-%3Enext%20becomes%20head%20*%2F%0A%20%20while(fast_ptr-%3Enext%20!%3D%20head%20%26%26%0A%20%20%20%20%20%20%20%20%20fast_ptr-%3Enext-%3Enext%20!%3D%20head)%20%0A%20%20%7B%0A%20%20%20%20%20fast_ptr%20%3D%20fast_ptr-%3Enext-%3Enext%3B%0A%20%20%20%20%20slow_ptr%20%3D%20slow_ptr-%3Enext%3B%0A%20%20%7D%20%20%0A%20%0A%20%2F*%20If%20there%20are%20even%20elements%20in%20list%20then%20move%20fast_ptr%20*%2F%0A%20%20if(fast_ptr-%3Enext-%3Enext%20%3D%3D%20head)%0A%20%20%20%20fast_ptr%20%3D%20fast_ptr-%3Enext%3B%20%20%20%20%20%20%0A%20%20%20%0A%20%20%2F*%20Set%20the%20head%20pointer%20of%20first%20half%20*%2F%0A%20%20*head1_ref%20%3D%20head%3B%20%20%20%20%0A%20%20%20%20%20%20%0A%20%20%2F*%20Set%20the%20head%20pointer%20of%20second%20half%20*%2F%0A%20%20if(head-%3Enext%20!%3D%20head)%0A%20%20%20%20*head2_ref%20%3D%20slow_ptr-%3Enext%3B%0A%20%20%20%0A%20%20%2F*%20Make%20second%20half%20circular%20*%2F%20%20%0A%20%20fast_ptr-%3Enext%20%3D%20slow_ptr-%3Enext%3B%0A%20%20%20%0A%20%20%2F*%20Make%20first%20half%20circular%20*%2F%20%20%0A%20%20slow_ptr-%3Enext%20%3D%20head%3B%20%20%20%20%20%20%20%0A%7D%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20*%2F%0A%2F*%20Function%20to%20insert%20a%20node%20at%20the%20begining%20of%20a%20Circular%20%0A%20%20%20linked%20lsit%20*%2F%0Avoid%20push(struct%20node%20**head_ref%2C%20int%20data)%0A%7B%0A%20%20struct%20node%20*ptr1%20%3D%20(struct%20node%20*)malloc(sizeof(struct%20node))%3B%0A%20%20struct%20node%20*temp%20%3D%20*head_ref%3B%20%0A%20%20ptr1-%3Edata%20%3D%20data%3B%20%20%0A%20%20ptr1-%3Enext%20%3D%20*head_ref%3B%20%0A%20%20%20%0A%20%20%2F*%20If%20linked%20list%20is%20not%20NULL%20then%20set%20the%20next%20of%20%0A%20%20%20%20last%20node%20*%2F%0A%20%20if(*head_ref%20!%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20while(temp-%3Enext%20!%3D%20*head_ref)%0A%20%20%20%20%20%20temp%20%3D%20temp-%3Enext%3B%20%20%20%20%20%20%20%20%0A%20%20%20%20temp-%3Enext%20%3D%20ptr1%3B%20%0A%20%20%7D%0A%20%20else%0A%20%20%20%20%20ptr1-%3Enext%20%3D%20ptr1%3B%20%2F*For%20the%20first%20node%20*%2F%0A%20%0A%20%20*head_ref%20%3D%20ptr1%3B%20%20%20%20%20%0A%7D%20%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20Circular%20linked%20list%20*%2F%0Avoid%20printList(struct%20node%20*head)%0A%7B%0A%20%20struct%20node%20*temp%20%3D%20head%3B%20%0A%20%20if(head%20!%3D%20NULL)%0A%20%20%7B%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%20head)%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%20list_size%2C%20i%3B%20%0A%20%20%20%0A%20%20%2F*%20Initialize%20lists%20as%20empty%20*%2F%0A%20%20struct%20node%20*head%20%3D%20NULL%3B%0A%20%20struct%20node%20*head1%20%3D%20NULL%3B%0A%20%20struct%20node%20*head2%20%3D%20NULL%3B%20%20%0A%20%0A%20%20%2F*%20Created%20linked%20list%20will%20be%2012-%3E56-%3E2-%3E11%20*%2F%0A%20%20push(%26head%2C%2012)%3B%20%0A%20%20push(%26head%2C%2056)%3B%20%20%20%0A%20%20push(%26head%2C%202)%3B%20%20%20%0A%20%20push(%26head%2C%2011)%3B%20%20%20%0A%20%0A%20%20printf(%22Original%20Circular%20Linked%20List%22)%3B%0A%20%20printList(head)%3B%20%20%20%20%20%20%0A%20%20%0A%20%20%2F*%20Split%20the%20list%20*%2F%0A%20%20splitList(head%2C%20%26head1%2C%20%26head2)%3B%0A%20%20%0A%20%20printf(%22%5CnFirst%20Circular%20Linked%20List%22)%3B%0A%20%20printList(head1)%3B%20%20%0A%20%0A%20%20printf(%22%5CnSecond%20Circular%20Linked%20List%22)%3B%0A%20%20printList(head2)%3B%20%20%0A%20%20%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D%20″ message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

Original Circular Linked List
11 2 56 12 
First Circular Linked List
11 2 
Second Circular Linked List
56 12