Exemple #1
0
int splithalf(SLink **head, SLink **ahead, SLink **bhead)
{
	//take local reference to traverse through the link
	SLink *temp, *temp2, *temp3;
	temp = *head;
	
	//get the mid_value of the given list that needs to be split
	int mid_value, i;
	mid_value = find_mid(*head);
	
	//check if list is empty
	if (mid_value == EMPTYLIST)
	{
		return EMPTYLIST;
	}
	
	//if head has only one element,
	if((*head) -> link == NULL)
	{
		*ahead = *head;
		*bhead = NULL;
		return SUCCESS;
	}		
	//if mid_value is > 0
	else
	{
		//iterate to the mid of a list and copy this into alist
		while(temp)
		{
			if(temp -> data == mid_value)
			{
				break;
			}
			insert_at_last(ahead, temp -> data); 
			temp = temp -> link;
		}
		temp3 = temp;
		
		// blist will be updated with temp2 which has second half
		*bhead = temp3; 	
		return SUCCESS;
	}
	
	return FAILURE;
}
main()
{
	node *head=NULL;
	int n,k;
	while(1)
	{
		printf("Enter any number: ");
		scanf("%d",&n);
		if(n!=-99)
		{
			head=insert_at_last(head,n);
		}
		else break;
	}
	display(head);
	printf("\nEnter the value of k:");
	scanf("%d",&k);
	rotate(head,k);
}