单向环形链表(Josepfu)

➢Josephu 问题
Josephu 问题为:设编号为1,2, … n的n个人围坐一-圈,约定编号为k (1<=k<=n) 的人
从1开始报数,数到m的那个人出列,它的下一位又从1开始报数,数到m的那个人又出
列,依次类推,直到所有人出列为止,由此产生-一个出队编号的序列。

构建-个单向的环形链表思路
1.先创建第-一个节点,让first指向该节点,并形成环形
2.后面当我们每创建一个新的节点,就把该节点,加入到已有的环形链表中即可
在这里插入图片描述

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

出圈
根据用户的输入,生成-一个小孩出圈的顺序
n=5,即有5个人
k=1,从第一个人开始报数
m=2,数2下
1.需求创建一个辅助指针(变量) helper ,事先应该指向环形链表的最后这个节点.
补充:小孩报数前,先让first和helper移动k- 1次
2.当小孩报数时,让first 和helper指针同时的移动m-1次
3.这时就可以将first指向的小孩节点出圈
first = first .next
helper.next=first
原来first指向的节点就没有任何引用,就会被回收
出圈的顺序
2->4->1->38->3
在这里插入图片描述

package com.main.dataStructures.LinkedList;

public class Josepfu {
    public static void main(String[] args) {
        CircleSingleLinkedList list = new CircleSingleLinkedList();
        list.add(23);
//        list.showBoy();
        list.countBoy(1,6,23);

    }
}


class CircleSingleLinkedList{
    private Boy first = null;
    public void add(int nums){
        if (nums<1){
            System.out.println("输入的数值不对");
            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 showBoy(){
        if (first == null){
            System.out.println("为空");
            return;
        }
        Boy boy = first;
        while (true){
            System.out.println(boy.getNo());
            if (boy.getNext() == first){
                break;
            }
            boy = boy.getNext();
        }
    }
    //出圈
    public void countBoy(int startNum,int countNum ,int nums) {//startNum:从第几个开始  countNum:数几下  nums:总共有多少个
        if (startNum < 1 || startNum > nums || first == null) {
            System.out.println("参数不符合要求");
            return;
        }
        Boy helper = first;

        while (true) {
            if (helper.getNext() == first) {
                break;
            }
            helper = helper.getNext();
        }

        for (int i = 0; i < startNum - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        while (true) {
            if (helper == first) {//只剩一个人
                break;
            }
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            System.out.println("出圈顺序为:"+first.getNo());

            first = first.getNext();
            helper.setNext(first);
        }
        System.out.println("最后剩下:" + helper.getNo());
    }
}
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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值