* elected the leader. N players in a circle, starting from the first person in order to count 1,2,3.3 who reported out of the circle, and finally left in the circle of the people of the leader.
* Node number is starting from 0 instead of starting from 1.
*/
public class Selecthead
{
/**
* Create a linked list. Creates a list of a given number of lists and connects them to the beginning and end of the loop.
*
* @param n
* @return The first node of the list
*/
public node createlist (int n)
{
node Headnode = new node ();
int i = 1;
headnode.setdata (0); Head node
Node A = Headnode;
while (i < n)
{
Node B = new node ();
B.setdata (i);
A.setlink (b);
a = b;
i++; The loop cannot be lost, otherwise it becomes a dead loop
}
A.setlink (Headnode); form the Ring
return headnode;
}
/**
* Output The data in the current list
*
* @param the head node
of the linklist linked list
*/
public void Outputdata (node linklist)
{
After the
//formation of the ring to pay attention to identify the location of the head node, for only one node of the chain it may form a dead loop.
Node A;
a = linklist;
Node B = A.getlink ();
while (B.getdata ()!= a.getdata ())
{
//Because it is the ring, so judged, but the judgment is not strict
System.out.print (B.getdata ());
B = B.getlink (); Get Next node
}
}
/**
* Execute the game
* @param the head node
of the linklist linked list
* @param n The total number of the linked list
*/
public void Play (node linklist, int n)
{
int k;
Node A = linklist;
Node B;
k = n;
while (k > 1)
{
//After a node is down from the current node, the current count should be 2
a = A.getlink (); The node should be numbered 2
B = A.getlink (); The count of this node should be 3, to remove
A.setlink (B.getlink ()); Delete the B node.
a = A.getlink (); Swap the current node for the node after the deletion node.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.