数据结构之约瑟夫问题

解决思路

在这里插入图片描述

代码实现

package DataStructrues.linkedList;

/**
 * @author xiaye
 * @create 2020-06-24-7:09
 */
public class Josepfu {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(20);
        circleSingleLinkedList.showBoy();
        System.out.println("---------------约瑟夫问题处理------------------");
        circleSingleLinkedList.countBoy(1, 2, 5);

    }
}

//单向环形链表
class CircleSingleLinkedList {
    //创建一个头节点,当前没 有编号
    private Boy first = null;

    //添加小孩节点,构建一个环形的链表
    public void addBoy(int nums) {
        if (nums < 1) {
            System.out.println("nums 的值不正确,添加失败");
            return;
        }
        //创建一个辅助节点, 因为头节点不能动
        Boy curBoy = null;

        for (int i = 1; i <= nums; i++) {
            //根据编号创建节点
            Boy boy = new Boy(i);
            //如果是第一个节点
            if (i == 1) {
                first = boy;
                first.setNext(first);  //构成环
                curBoy = first;  //curboy指向第一个小孩
            } else {
                //后面的节点
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }

        }
    }

    //显示循环链表的方法
    public void showBoy() {
        if (first == null) {
            System.out.println("没有任何小孩~");
            return;
        }
        //因为first不能动,所以我们使用 一个辅助指针来遍历
        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 (startNo < 1 || first == null || startNo > nums) {
            System.out.println(" 参数输入有误,请重新输入~");
            return;
        }
        //创建辅助指针,帮助小孩完成出圈
        Boy helper = first;
        // 需求创建一个辅助指针(变量) helper , 事先应该指向环形链表的最后这个节点
        while (true) {
            //让辅助指针指向最后一个小孩
            if (helper.getNext() == first) {
                break;
            }
            helper = helper.getNext();
        }

        // 将头节点移到需要的位置,小孩报数前,先让 first 和  helper 移动 startNo - 1次
        for (int j = 0; j < startNo - 1; j++) {
            first = first.getNext();
            helper = helper.getNext();
        }

        //当小孩报数时,让first 和 helper 指针同时 的移动  countNum  - 1 次, 然后出圈
        while (true) {
            if (helper == first) {
                //循环链表中只有一个小孩了
                break;
            }
            //每次报数的时候都要让头节点和辅助节点移动countNum-1 次,做个for循环
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            //这个时候,first指向的节点就是我们要找到的小孩
            System.out.printf("小孩%d出圈\n", first.getNo());
            //这时将first指向的小孩节点出圈
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩的编号为%d\n", first.getNo());

    }
}


//单向环形链表的节点
class Boy {
    private Integer no;
    private Boy next;


    public void setNext(Boy next) {
        this.next = next;
    }

    public Integer getNo() {
        return no;
    }

    public Boy(Integer no) {
        this.no = no;
    }

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

    public Boy getNext() {
        return next;
    }


}

依据b站尚硅谷韩顺平老师的课程进行的学习,仅供参考!
视频地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值