package com.fenzi;
public class yueshefu {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkTable lb = new LinkTable(2,3);
lb.createTable();
lb.printTable();
lb.play();
}
}
class Node{
public int i;
Node Pnext=null;
public Node(int i){this.i = i;}
}
class LinkTable{
public int k = 5;
Node Pfirst = null;
Node temp= null;
int startPeople;
int num;
public LinkTable(int startPeople, int num){
this.startPeople = startPeople;
this.num = num;
}
public void createTable(){
for(int p=1; p<=k;p++){
if (p == 1){
Node demoNode = new Node(p);
this.Pfirst = demoNode;
this.temp = demoNode;
}
else{
if (p==k){
Node demoNode = new Node(p);
temp.Pnext = demoNode;
temp = demoNode;
temp.Pnext = this.Pfirst;
}else{
Node demoNode = new Node(p);
temp.Pnext = demoNode;
temp = demoNode;
}
}
}
}
public void printTable(){
Node temp = this.Pfirst;
do{
System.out.println(temp.i);
temp = temp.Pnext;
}while(temp!=this.Pfirst);
}
public void play(){
Node startpeople = Pfirst;
while (this.k!=1){
for (int i=1;i<startPeople;i++){
startpeople = startpeople.Pnext;
}
//找出要出队前人的前一个人的位置
for (int i=1;i<num;i++){
startpeople = startpeople.Pnext;
}
//找出要出队那人的位置
Node startpeoples = Pfirst;
for (int i=1;i<=num;i++){
startpeoples = startpeoples.Pnext;
}
startpeople = startpeoples.Pnext;
this.k--;
}
System.out.println("最后的人是"+startpeople.i);
}
}