Java数据结构与算法——环形单向链表(约瑟夫问题)2

约瑟夫问题思路(小孩出圈)
1.需要创建一个辅助指针(变量) helper,事先应该指向环形链表的最后这
个节点. .
补充:小孩报数前,先让first和helper移动countNum-1次,这样可以让first指向开始的节点,helper指向最后一个节点。
2.当小孩报数时,让first 和helper指针同时的移动Nums -1次
3.这时就可以将first指向的小孩节点出圈
first= first .getBoy();
helper.setBoy(first);
原来first指向的节点就没有任何引用,就会被回收

这里是约瑟夫问题核心代码,展现了环形链表中出表的过程。

/**
     * @@param startNo 表示从哪一个节点开始数
     * @@param countNum 数多少下
     * @@param nums 总共多少节点
     */
    public void countBoy(int startNo,int countNum,int nums){
        //首先判断链表是否为空,开始节点是否合理,报数次数是否合理。
        if (first == null || startNo < 1 || startNo > nums){
            System.out.println("");
            return;
        }
        Boy helper = first; //定义辅助节点
        while(true){
            if (helper.getNext() == first){//helper节点指向最后节点。
                break;
            }
            helper = helper.getNext();
        }
        //first指向开始节点
        for (int j = 0;j < startNo-1;j++ ){
            first = first.getNext();
            helper = helper.getNext();
        }
        //找到需要出圈的节点
        while(true){
            if (helper == first){
                break;
            }
            for (int i = countNum;i > 1;i--){
                first = first.getNext();
                helper = helper.getNext();
            }
            //出圈操作
            System.out.printf("小孩%d出圈。\n",first.getNo());
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后圈中小孩编号%d",first.getNo());
    }
}

下面是完整代码

import sun.awt.geom.AreaOp;

public class Josephu {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(25);
        circleSingleLinkedList.showBoy();
        //
        circleSingleLinkedList.countBoy(1,2,5);
    }

}
//创建环形单向链表
class CircleSingleLinkedList{
    //创建一个First节点,无编号
    private Boy first = new Boy(-1);
    //添加节点
    public void addBoy(int nums){
        if (nums<1){//数据校验
            System.out.println("nums的值错误!");
            return;
        }
        Boy curBoy = null;//辅助指针。帮助构建环形链表。
        for (int i = 1;i <= nums;i++){//利用for循环进行节点添加
            Boy boy = new Boy(i);
            if (i == 1){
                first = boy;
                first.setNext(first);//连接新节点
                curBoy = first;//形成闭环
            }else {
                curBoy.setNext(boy);//连接上新节点
                boy.setNext(first);//最后一个节点指向first节点
                curBoy = boy;//curBoy后移
            }
        }
    }
    //链表遍历
    public void showBoy(){
        //判断链表是否为空
        if (first == null){
            System.out.println("链表空!!");
            return;
        }
        Boy curBoy = first;  //辅助遍历节点
        while(true){
            System.out.printf("小孩的编号%d\n",curBoy.getNo());
            if (curBoy.getNext() == first){
                break;      //遍历完成的条件
            }
            curBoy = curBoy.getNext();//curBoy节点后移
        }
    }
    //

    /**
     * @@param startNo 表示从哪一个节点开始数
     * @@param countNum 数多少下
     * @@param nums 总共多少节点
     */
    public void countBoy(int startNo,int countNum,int nums){
        //首先判断链表是否为空,开始节点是否合理,报数次数是否合理。
        if (first == null || startNo < 1 || startNo > nums){
            System.out.println("");
            return;
        }
        Boy helper = first; //定义辅助节点
        while(true){
            if (helper.getNext() == first){//helper节点指向最后节点。
                break;
            }
            helper = helper.getNext();
        }
        //first指向开始节点
        for (int j = 0;j < startNo-1;j++ ){
            first = first.getNext();
            helper = helper.getNext();
        }
        //找到需要出圈的节点
        while(true){
            if (helper == first){
                break;
            }
            for (int i = countNum;i > 1;i--){
                first = first.getNext();
                helper = helper.getNext();
            }
            //出圈操作
            System.out.printf("小孩%d出圈。\n",first.getNo());
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后圈中小孩编号%d",first.getNo());
    }
}

//创建Boy节点
class Boy{
    private int no;
    private Boy next;
    public Boy(int no){
        this.no = no;
    }
    public int getNo() {
        return no;
    }

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

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值