单向循环链表-约瑟夫问题

单向环形列表

应用场景:约瑟夫环问题

image-20221128174004969

思路:

  • 创建第一个节点,让first指向该节点,并形成环状
  • 后面当我们每创建一个新的节点,就把该节点,加入到已有的环形链表中即可

遍历环形链表

  • 先让一个辅助变量,指向frist节点
  • 然后通过一个while循环遍历该环形链表即可 curBoy.next == first结束

代码实现

  • 添加和遍历单向环形链表

image-20221128224144151

package com.wiselee.linkedlist;

/**
 * @PROJECT_NAME: DataStruct
 * @DESCRIPTION:
 * @USER: 28416
 * @DATE: 2022/11/28 22:18
 * 约瑟夫环问题
 */
public class Josepfu {
    public static void main(String[] args) {
        circleSingeLinked circleSingeLinked = new circleSingeLinked();
        circleSingeLinked.addBoy(12);
        circleSingeLinked.showList();
    }
}

//创建环形链表
class  circleSingeLinked{
    private Boy first = null;
    //加入小孩
    public void addBoy(int nums){
        if (nums <2 ){
            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;
            }else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }
    //遍历当前所有的节点
    public void showList(){
        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();//cuyBoy后移
        }
    }


}

//首先定义一个节点
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;
    }
}

约瑟夫问题:出圈的问题

根据用户的输入,生成一个小孩出圈的顺序

  • 需要创建一个辅助指针(变量)helper,事先应该指向环形链表的最后这个节点
  • 还需要事先将frist和helper移动到k-1这个节点
  • 当小孩报数是,让first和helper同时移动m-1次
  • 这时就可以考虑first指向的小孩节点出圈 first =first helper.next = first

image-20221128224930370

/**
     *
     * @param startNo  表示从第几个小孩开始计数
     * @param count 表示数几下
     * @param nums 表示最初有多少小孩在圈中
     */
    public void countBoy(int startNo,int count,int nums){
        if (first == null || startNo <1 || startNo >nums ||nums <1){
            System.out.println("参数输入有误,请重新输入");
            return;
        }
        Boy  helper = first;
        while (true){
            if (helper.getNext() == first){//说明helper指向了最后
                break;
            }
            helper = helper.getNext();
        }
        //报数  frist 和helper 移动几次
        for (int i = 0; i < startNo - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        //移动 出圈  循环操作
        while (true) {
            if (helper == first){//说明圈中只有一个人
                break;
            }
            // 让 first 和helper 移动 count -1 次 出圈
            for (int i = 0; i < count - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            //first 指向的就是要出圈的节点
            System.out.printf("小孩%d出圈\n",first.getNo());
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩编号%d\n",helper.getNo());
    }
posted @ 2022-11-28 23:08  wiselee/  阅读(17)  评论(0编辑  收藏  举报