单向环形链表之Josephu问题的实现代码

问题:

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

package LinkeList;

public class Josephu {
    public static void main(String[] args) {
        //测试环形链表的构建与遍历是否正确
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);//加入5个小孩节点
        System.out.println("小孩形成的环形链表为:");
        circleSingleLinkedList.showBoy();

        //测试小孩出圈是否正确
        System.out.println("小孩的出圈顺序为:");
        circleSingleLinkedList.countBoy(1,2,5);
    }
}

//1. 定义一个Boy类,每一个boy对象就是一个结点
class Boy{
    private int no; //编号
    private Boy next; //指向下一个节点,默认null
    //构造方法
    public Boy(int no){
        this.no = no;
    }
    public int getNo() {
        return no;
    }
    public Boy setNo(int no) {
        this.no = no;
        return this;
    }
    public Boy getNext() {
        return next;
    }
    public Boy setNext(Boy next) {
        this.next = next;
        return this;
    }
}
//2. 定义一个环形的单向链表【无表头】
class CircleSingleLinkedList{
    //创建一个first节点,当前没有编号
    private Boy first = new Boy(-1);
    //(1)添加小孩的节点,构建成环形链表
    public void addBoy(int nums){//nums:整个环形链表有多少小孩
        //nums,需做一个数据校验
        if (nums < 1){
            System.out.println("nums的值不正确");
            return;
        }
        Boy curBoy = null; //辅助指针,帮助我们构建环形链表
        //使用for循环创建环形链表
        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; //移动辅助指针
            }
        }
    }
    //(2)遍历当前的环形链表
    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 num:表示最初有多少小孩在圈
     */
    //(3)根据用户的输入形成小孩出圈的顺序
    public void countBoy(int startNo,int countNum,int num){
        //先对数据进行校验
        if (first == null || startNo < 1 || startNo > num){
            System.out.println("参数输入有误");
        }
        //创建一个辅助指针,帮助完成小孩出圈
        Boy helper = first;
        //helper指向环形链表的最后这个节点,first指向要删除的
        while (true){
            if (helper.getNext() == first){//说明helper指向环形链表的最后这个节点
                break;
            }
            helper = helper.getNext();
        }
        // 小孩报数前,先让first和helper移动k-1次
        for (int j = 0;j < startNo - 1;j++){
            first = first.getNext();
            helper = helper.getNext();
        }
        //开始报数,然后出圈,直到圈中只有一个节点
        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);//是helper的next域指向first
        }
        System.out.printf("最后留在最后的小孩编号%d\n",first.getNo());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值