Joseph's problem is also called the handkerchief problem. Legend has it that the famous Jewish historian, Josephus, used his rules to escape a suicide game and then surrendered to Rome.
Problem:
Known n individuals (denoted by number 1,2,3...N) sit around a table.
* from the number of K-person began to count, the number to m of the person out;
* His next man began counting from 1, and the man who counted to M was out again;
* Repeat this rule until all the people around the table are out.
Use the nodes to simulate the people in the game, and use the list to show that the people in the game are arranged in a certain order. Each node gives a number, starting with a node numbered K, and the node counting to M exits from the linked list.
Then the next node of M is counted from the beginning, in turn ...
A node with a count of M is removed from the list by simply pointing the reference of the previous node of M to the next node of M.
According to the above, our node class requires only two properties, one for its own number, another for reference, point to the next node, and the last node to point to the first node.
You must have one entry node for the first node ———— otherwise you cannot enter the ring list.
We need a walk-in node to handle the count in the game.
Because it is a one-way list, when we find a node that counts as M, we also locate the previous node of M and update its reference so that the node with the Count M loses its reference and is disposed of by the Java garbage collection mechanism.
To define the previous node of M, it is possible to iterate through the list direction to a reference to a node with a count of M, which is the node.
You can also change the list to a doubly linked list, or you can define two walk-in nodes, one to count, and one to then locate the previous node of M.
These methods are not used here, because finding a node with a count of M does not require any processing of it, but instead handles its previous node ———— update its reference to the next node of M.
So we can simply count the m-1 to be processed directly.
Give the complete code as follows:
Public classjosephcycle { Public Static voidMain (String []args) {Cyclelink cl=NewCyclelink (); Cl.setlen (7); CL.SETK (3); Cl.setm (3); Cl.creatlink (); Cl.play (); Cl.show (); }}classnode{intnum; Node NextNode=NULL; PublicNode (intnum) { This. num =num; }}classcyclelink{intLen; intK; intm; Node Firstnode=NULL; Node Temp=NULL; Public voidSetlen (intLen) { This. Len =Len; } Public voidSETK (intk) { This. K =K; } Public voidSetm (intm) { This. m =m; } //Create a linked list Public voidCreatlink () { for(inti = 1; I <= Len; i++){ //processing the first node if(i==1) {Node nd=NewNode (i); Firstnode=nd; Temp=nd; }Else if(i = = len) {//handling stub pointsNode nd =NewNode (i); Temp.nextnode=nd; Temp=nd; Temp.nextnode=Firstnode; }Else{Node nd=NewNode (i); Temp.nextnode=nd; Temp=nd; } } } Public voidPlay () {temp=Firstnode; //Find the node numbered K first for(inti = 1; I < K; i++) {Temp=Temp.nextnode; } while( This. Len!=1){ //Count off , find the previous node of M for(intj = 1; j < m-1; J + +) {Temp=Temp.nextnode; } //because less than 1 is reported, the next node is deleted and the next node restartsSystem.out.println ("Node to delete:" +temp.nextNode.num); /*** If the delete node is firstnode, update the Firstnode to the next node * Note that it cannot be judged by number, because the node that corresponds to the new number may be deleted again*/ if(temp.nextnode==Firstnode) {Firstnode=Temp.nextNode.nextNode; } Temp.nextnode=Temp.nextNode.nextNode; Temp=Temp.nextnode; //this.show (); This. len--; } } /*** Traverse linked list to print the entire list*/ Public voidShow () {temp=Firstnode; Do{System.out.println (temp.num); Temp=Temp.nextnode; } while(Temp! =Firstnode); }}
This code can be run directly, running the result:
The last remaining nodes are: 6
In practical applications, such as queuing, according to different k,m,len values, for some special cases, there may be different algorithms I do not know, guess so!
Why this conjecture, the reader can see: Joseph Ring question detailed
The Java Ring List implements Joseph's question