05.单向环形链表应用 -- 约瑟夫环


/**
 * 单向环形链表应用 -- 约瑟夫环
 * Josephu  问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,
 * 数到m 的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,
 * 由此产生一个出队编号的序列。
 */
public class Josepfu {
    public static void main(String[] args){
        CircleSingleLinkedList linkedList = new CircleSingleLinkedList();
        linkedList.add(5);
//        linkedList.show();
        linkedList.countBoyNode(1,2,5);
        //从1开始~
        //2出圈
        //4出圈
        //1出圈
        //5出圈
        //圈中最后一个:3
    }
}
class CircleSingleLinkedList{
    private BoyNode first = null;
    //添加nums个结点
    public void add(int nums){
        if (nums<1){
            System.out.println("num");
            return;
        }
        //辅助指针
        BoyNode c = null;
        for (int i = 1; i <= nums; i++) {
            BoyNode boyNode = new BoyNode(i);
            if (i==1){
                first = boyNode;
                first.setNext(first);//构成环
                c = first;//c从first开始
            }else {
                c.setNext(boyNode);
                boyNode.setNext(first);//环形相当于插在first前面
                c = boyNode;
            }
        }
    }

    /**
     * BoyNode出圈顺序
     * @param startNo 从startNo开始数
     * @param countNum 数到countNum
     * @param nums 初始化CircleSingleLinkedList的结点数
     */
    public void countBoyNode(int startNo,int countNum,int nums){
        if (first==null||startNo<1||startNo>nums){
            System.out.println("参数有误");
            return;
        }
        BoyNode c = first;
        //找到最后一个结点
        while (true){
            if (c.getNext()==first){
                break;
            }
            c = c.getNext();
        }
        //first移动到startNo,c为startNo的前一个
        for (int i = 1; i < startNo; i++) {
            first = first.getNext();
            c = c.getNext();
        }
        System.out.println("从"+first.getNo()+"开始~");
        while (true){
            if (c==first){//只有一个结点
                break;
            }
            for (int i = 1; i < countNum; i++) {
                first = first.getNext();
                c = c.getNext();
            }
            System.out.println(first.getNo()+"出圈");
            first = first.getNext();
            c.setNext(first);
        }
        System.out.println("圈中最后一个:"+first.getNo());
    }
    public void show(){
        if (first==null){
            System.out.println("空环~");
            return;
        }
        BoyNode c = first;
        while (true){
            System.out.print(c.getNo()+"\t");
            if (c.getNext()==first){
                break;
            }
            c = c.getNext();
        }
        System.out.println();
    }

}
class BoyNode{
    private int no;
    private BoyNode next;

    public BoyNode(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public BoyNode getNext() {
        return next;
    }

    public void setNext(BoyNode next) {
        this.next = next;
    }
}
posted @ 2019-10-08 15:15  fly_bk  阅读(142)  评论(0编辑  收藏  举报