public class Link {
public long lData;
public Link next;
public Link(long lData){
this.lData=lData;
}
public void displayLink(){
System.out.print(lData+" ");
}
}
public class CircleList {
private Link current;
private int nItems;
public CircleList(){
current=null;
}
public void insert(long value){
Link newLink = new Link(value);
if(current==null){
current=newLink;
newLink.next=newLink;
}else{
newLink.next=current.next;
current.next=newLink;
current=newLink;
}
nItems++;
}
public long remove(){
long temp = current.next.lData;
if(current.next==current){
current=null;
}else{
current.next=current.next.next;
}
nItems--;
return temp;
}
public long peek(){
return current.next.lData;
}
public Link find(long value){
Link temp =current;
Link result=null;
if(current==null){
return result;
}
do{
step();
if(current.lData==value){
result=current;
current=temp;
}
}while(current!=temp);
return result;
}
public void step(){
if(current!=null){
current=current.next;
}
}
public void display(){
if(current!=null){
Link temp =current;
do{
step();
System.out.print(current.lData + " ");
}while(current!=temp);
}
System.out.println();
}
public boolean isEmpty(){
return (current==null);
}
public int size(){
return nItems;
}
}
public class Josephus {
/**
* @param args
*/
public static long getJosephus(int number){
CircleList theCircleList = new CircleList();
for(int i=1;i<=number;i++){
theCircleList.insert(i);
}
System.out.print("原始圈子:");
theCircleList.display();
System.out.print("出圈顺序:");
for(int i=1;i<number;i++){
for(int j=1;j<4;j++){
theCircleList.step();
}
System.out.print(theCircleList.remove()+" ");
}
System.out.println();
return theCircleList.peek();
}
public static void main(String[] args) {
long number = Josephus.getJosephus(7);
System.out.println("Josephus的编号是:"+number);
}
}