丢手帕问题_环形链表的使用
/*设编号为1,2.....n个人坐一圈,约定编号为k的人从1开始报数, 数到m的那个人出列,他的下一位从1开始报数,数到m的那个人出列, 直到所有人出列*/ public class丢手帕问题 { public static void main(String[] args) { Cyclink cyclink=new Cyclink(); //9个人 cyclink.setLen(5); cyclink.createLink(); //从第2个人开始数 cyclink.setK(2); //数2下 cyclink.setM(2); cyclink.show(); cyclink.play(); } } class Child1{ //编号 int no; //不知道下一个在哪,设置为空 Child1 nextChild=null; public Child1(int no){ this.no=no; } } //环形链表 class Cyclink{ //定义一个指向链表第一个小孩的引用 //指向第一个小孩的引用不能动 Child1 firstChild=null; //设置一个游标,相当于跑动丢手绢的小孩 Child1 temp=null; //共有几个小孩 int len=0; int k=0; int m=0; //数几下 public void setM(int m){ this.m=m; } //设置从第几个人开始数数 public void setK(int k){ this.k=k; } //设置链表大小 public void setLen(int len){ this.len=len; } //初始化环形链表 public void createLink(){ for(int i=1;i<=len;i++){ if(i==1){ //创建第一个小孩 Child1 ch=new Child1(i); //firstChild,temp指针指向 this.firstChild=ch; this.temp=ch; }else{ //创建最后一个小孩 if(i==len){ Child1 ch=new Child1(i); temp.nextChild=ch; temp=ch; //与第一个小孩形成链 temp.nextChild=this.firstChild; }else{ //如果不是第一个小孩,那继续创建小孩 Child1 ch=new Child1(i); //与下一个小孩形成链 temp.nextChild=ch; //指针指向 temp=ch; } } } } //打印环形链表 public void show(){ //定义一个丢手绢的人 Child1 temp=this.firstChild; do{ System.out.println(temp.no); temp=temp.nextChild; }while(temp!=this.firstChild); } public void play(){ Child1 temp=this.firstChild; //1)先找到开始数数的人 for(int i=1;i<k;i++){ temp=temp.nextChild; } while(this.len!=1){ //2)数m下 for(int j=1;j<m;j++){ temp=temp.nextChild; } //找到要删除的前一个小孩 Child1 temp2=temp; while(temp2.nextChild!=temp){ temp2=temp2.nextChild; } //3)将数到的小孩删除(断链) temp2.nextChild=temp.nextChild; //让temp指向下一个数数的小孩 temp=temp.nextChild; //出一个少一个 this.len--; } //最后一个小孩 System.out.println("最后出圈的小孩:"+temp.no); } }