环形链表和约瑟夫问题

package com.dai.linkedList;

public class Josepfu {

    public static void main(String[] args) {
        // 构建环形链表和遍历是否ok
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(500);// 加入5个小孩节点
        circleSingleLinkedList.showBoy();
        circleSingleLinkedList.countBoy(1, 5, 500);
    }

}

//创建一个环形链表
class CircleSingleLinkedList {
    private Boy first = null;

    // 添加小孩,构建环形链表
    public void addBoy(int 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;
            } 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();
        }
    }

    // startNo:表示从第几个小孩开始数数
    // countNo: 表示数几下
    // nums: 表示最初有多少个小孩在圈中
    public void countBoy(int startNo, int countNum, int nums) {
        // 首先校验数据
        if (first == null || startNo < 1 || startNo > nums) {
            System.out.println("参数输入有误,重新输入");
            return;
        }
        Boy helper = first;
        while (true) {
            if (helper.getNext() == first) {
                break;
            }
            helper = helper.getNext();
        }
        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();
            }
            System.out.printf("小孩%d出圈\n", first.getNo());
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩编号为%d\n", first.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;
    }

}

 

posted @ 2021-01-31 22:12  shengtudai  阅读(65)  评论(0编辑  收藏  举报