环形链表

发布时间 2023-08-03 11:40:39作者: 林浅

约瑟夫问题

描述

分析:

代码:

public class Test5 {
    public static void main(String[] args) {
        //测试
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);//加入五个小孩节点
        circleSingleLinkedList.showBoy();
        //测试小孩出圈是否正确
        circleSingleLinkedList.countBoy(1,2,5);//    2->4->1->5->3

    }
}
//创建一个环形的单向链表
class CircleSingleLinkedList{
    //创建一个first节点,当前没有编号
    private Boy first = new Boy(-1);
    //添加小孩节点,构建成环形链表
    public void addBoy(int num){
        //num做一个简单的数据校验
        if (num < 2){
            System.out.println("num的值不正确");
            return;
        }
        Boy curBoy = null;//辅助指针,帮助构建环形链表
        //使用一个for循环来创建环形链表
        for (int i = 1; i <= num; i++) {
            //根据编号,创建小孩节点
            Boy boy = new Boy(i);
            //如果是第一个小孩
            if (i==1){
                first = boy;
                first.setNext(first);
                curBoy=first;//让curBoy指向第一个小孩
            } else {
                curBoy.setNext(boy);//curBoy相当于指向最后节点 所以让curBoy的next指向新节点
                boy.setNext(first);//让新的节点指向第一个节点形成环
                curBoy=boy;//让curBoy指向最后这个节点
            }
        }
    }
    //遍历当前链表
    public void showBoy(){
        //判断链表是否为空
        if (first==null){
            System.out.println("链表为空");
            return;
        }
        //因为first不能动 所以需要一个辅助指针 从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  表示最初有多少个小孩在圈中
     */
    public void countBoy(int startNo,int countNum,int num){
        //先对数据进行校验
        if (first==null || startNo < 1 || startNo > num){
            System.out.println("参数输入有误,请重新输入");
            return;
        }
        //创建一个辅助指针,帮助完成小孩出圈
        Boy helper = first;
        //需求创建一个辅助指针helper,事先应该指向环形链表的最后这个节点
        while (true){
            if (helper.getNext()==first){
                //说明helper指向最后小孩节点
                break;
            }
            helper=helper.getNext();
        }
        //小孩报数前,先让first指向初始节点startNo,helper指向first前一个节点
        //所以得移动节点,移动startNo-1位,使他们指向对的位置
        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同时移动count-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=first.getNext();
            helper.setNext(first);
        }
        System.out.println("最后留在圈中的小孩编号"+first.getNo());
    }

}
//创建一个Boy类,表示一个节点
class Boy{
    private int no;//编号
    private Boy next;//指向下一个节点,默认为null
    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;
    }
}