Joseph Java implementation problems with one-way circular list

public class lianbiao2 {

class Node{
Node next;
int number;

public Node getNext() {
return next;
}

public void setNext(Node next) {
this.next = next;
}

public Node(int i) {
this.number = i;
}

public Node(Node next, int number) {
this.next = next;
this.number = number;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}
}

Public Node firsetnode;
public tempNode, Node;
int len;

/ **
* key ring is made:
* a first Node regenerating time, so that the two references (firsetnode and tempNode,) pointing him, after the cycles, each reference point by operating tempNode, end node, adding the newly generated node
* when enough length, the next node to the newly generated reference beginning of firsetnode has not changed, i.e. a node points to the first generated complete annular
* /

public void setcrcle (int length) {
for (int I =. 1; I <= length; I ++) {
the Node Node new new = the Node (I);
IF (I ==. 1) {
firsetnode = Node;
Node = tempNode,;
} the else {
IF (! I = length) {
tempNode.next = Node;
Node = tempNode,;
} the else {
tempNode.next = Node;
node.next = firsetnode;
}
}
len ++;
}
System.out.println (len);
}

public int Play (int keillNumber) {
return Play (. 1, keillNumber);
}

/ **
* to find the start node.
* Open from the start node cycle through the entire ring, exit conditions, tempNode no follow-up and not equal to itself
* After the cycle to achieve, when count is equal to the number of deaths directly delete the next node, count reset to 1
*
*
* @param Start starting position
* @param killNumber death toll
* @return
* /
Public int Play (Start int, int killNumber) {
int = COUNT. 1;
the Node tempNode, firsetnode =;

for (int I =. 1; I <Start; I ++) {
tempNode, = firsetnode.next; // find the initial node
}

the while (tempNode.next = null && tempNode.next = tempNode,!!) {
IF (COUNT = killNumber. 1-!) {
COUNT ++;
tempNode, = tempNode.next; // for recycling
} the else {
System.out.println ( "dead It is "+ tempNode.next.getNumber ());
tempNode.next = tempNode.next.next; // delete the node
tempNode = tempNode.next; // for recycling
count = 1;
}

}
return tempNode.getNumber();
}

public static void main(String[] args) {
lianbiao2 l =new lianbiao2();
l.setcrcle(10);
System.out.println("存活的是"+l.play(2,5));;
}
}

Guess you like

Origin www.cnblogs.com/xlblog/p/11551158.html