环形单链表(约瑟夫问题)

josephu 问题

在这里插入图片描述
思路:
在这里插入图片描述
在这里插入图片描述
代码实现:

package com.aiguigu.linkedlist;

import javax.crypto.spec.PSource;

public class josephu {
    public static void main(String[] args) {
        CircleSingleLinkedList list = new CircleSingleLinkedList();
        list.addBoy(5);
        list.countBoy(1,2,5);
    }
}

class CircleSingleLinkedList{
    //创建一个first节点,当前没有编号
    private Boy first;
    
    //添加节点,构成环形的链表
    public void addBoy(int nums){
        //对传进来的 nums 做校验
        if (nums < 2){
            System.out.println("nums 的值不正确");
            return;
        }
        //创建一个变量来辅助添加节点,first不能动
        Boy curboy = null;
        for (int i = 1; i <= nums ; i++) {
            //创建节点
            Boy boy = new Boy(i);
            //将first指向第一个节点
            if (i == 1){
                first = boy;
                first.setNext(first);//构成环状
                curboy = first;//让 辅助节点 curboy指向第一个节点
            }else {
                curboy.setNext(boy);//ji将辅助节点指向新节点
                boy.setNext(first);//将新节点的next指向first节点,形成环状
                curboy = boy;//curboy后移
            }
        }
    }

    //遍历当前的环形链表
    public void show(){
        //判断链表是否为空
        if (first == null){
            System.out.println("链表为空");
            return;
        }
        //创建一个辅助节点帮助遍历
        Boy helpNode = first;
        //遍历
        while (true){
            System.out.printf("小孩节点的编号为:%d\n",helpNode.getNo());
            if (helpNode.getNext() == first){//遍历结束
                break;
            }
                helpNode =helpNode.getNext();//辅助节点后移

        }
    }

    //根据用户的输入,计算出小孩出圈的顺序
/**
* @param startNo 表示从第几个节点开始数数
 * @param countNum 表示 数几下
 * @param nums 表示最初有多少个节点
 */
    public void countBoy(int startNo,int countNum,int nums){
        //先对数据进行校验
        if (startNo < 1 || countNum > nums || first==null){
            System.out.println("输入参数有误,请重新输入");
            return;
        }
        //创建一个复制指针,帮助节点完成出圈,这个指针实现指向最后一个节点
        Boy helper = first;
        while (true){
            if (helper.getNext() == first){//说明helper已经指向最后的节点
                break;
            }
            helper = helper.getNext();
        }
        //让 first 移动到要报数的节点的位置,helper也跟着移动
        for (int i = 0; i <startNo- 1 ; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        //当小孩报数时,让 first 和 helper 指针同时 的移动 countNum -1 次
        while (true){
            if (helper == first){//说明圈中只有一个节点
                break;
            }
            //让 first 和 helper 指针 同时 的移动 countNum -1
            for (int j = 0; j < countNum - 1; j++) {
                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());
    }

}

class Boy{
    private int no;
    private Boy next;

    public Boy() {
    }

    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出圈
小孩4出圈
小孩1出圈
小孩5出圈
最后留在圈中的小孩编号3

看韩顺平数据结构笔记

https://www.bilibili.com/video/BV1E4411H73v?p=29

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值