Example #1
0
/**
 *Function to return answer of Josephus problem
 *@param pointer to head of L.L, start person, Skip count, Total number of persons
 *@return final answer
 */
char josephus(node * head,char start,int c,int n)
{
	createCircularLinkedList(head);
	node *p = head;
	node *q = head;
	int count =c;
	while(1)
	{
		if(p->info == start)
			break;
		p=p->next;
	}
	while(1)
	{
		//Because the current node where I am standing will also be counted, hence decremented count
		count = c-1;
		while(1)
		{
			count--;
			if(count==0)
				break;
			p = p->next;      //To stop one node BEFORE which we have to delete
		}
		p->next = p->next->next;
		p = p->next;
		n--;
		if(n==1)
			break;
	}
	return p->info;
}
Example #2
0
int main(int argc, char *argv[])
{
	int len = 20;
	struct node *head = createCircularLinkedList(len);
	//move slow one, fast two
	struct node *slow = head->next;
	struct node *fast = head->next->next;
	while(slow != fast)
	{
		slow = slow->next;
		fast = fast->next->next;
	}
	//slow and fast have collided, and are LOOP - k away from circleStart
	slow = head;
	while(slow != fast)
	{
		//they will collide at circleStart
		slow = slow->next;
		fast = fast->next;
		len++;
	}
	printf("The collision point is at node %d!\n", fast->data );
	printf("Here is what the list looks like: (with an extra 3 after circle)\n");
	for (int i = 0; i < len + 3; ++i, head = head->next)
	{
		printf("|%d|->", head->data);
	}
	return 0;
}