【数据结构与算法(java)】环形链表(约瑟夫环)

环形链表(约瑟夫环)

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

n=5,即有 5 个人

k=1,从第一个人开始报数

m=2,数 2 下

出队列的顺序:2 4 1 5 3

利用单向环形链表完成。

  1. 创建一个辅助指针 (变量) helper,事先应该指向环形链表的最后这个节点。
    补充:小孩报数前,先让 first 和 helper 移动 k - 1 次
  2. 当小孩报数时,让 first 和 helper 指针同时的移动 m - 1 次
  3. 这时就可以将 first 指向的小孩节点出圈
    first = first.next;
    helper.next = first;
    原来 first 指向的节点就没有任何引用,就会被回收
    出圈的顺序
    2->4->1->5->3

public class Josephus {

    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);
        circleSingleLinkedList.showBoy();

        circleSingleLinkedList.countBoy(1, 2, 5);
    }
}

class CircleSingleLinkedList {

    private Boy first;

    public void addBoy(int nums) {
        if (nums < 1) {
            System.out.println("无效值");
            return;
        }
        Boy curBoy = new Boy(-1);
        for (int i = 1; i <= nums; i++) {
            Boy boy = new Boy(i);
            if (i == 1) {
                first = boy;
                first.setNext(first); // 构成环
            } else {
                curBoy.setNext(boy);
                boy.setNext(first);
            }
            curBoy = boy;
        }
    }

    /**
     *
     * @param startNo 从几开始
     * @param countNum 每轮数几下
     * @param 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 (helper.getNext()!=first) {
            helper=helper.getNext();
        }
        // 让helper指向最后一个节点
        for (int i=0;i<startNo-1;i++) {
            first=first.getNext();
            helper=helper.getNext();
        }
        // helper==first时,圈中只有一个节点
        while (helper!=first) { 
            // 注意是countNum-1,
            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("最后的节点为" + first.getNo());
    }

    public void showBoy() {
        if (first == null) {
            System.out.println("链表为空");
            return;
        }
        Boy curBoy = first;
        while (curBoy.getNext() != first) {
            System.out.println(curBoy.getNo());
            curBoy = curBoy.getNext();
        }
    }
}

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 boy) {
        this.next = boy;
    }
}

posted @ 2025-06-25 16:19  ToFuture$  阅读(16)  评论(0)    收藏  举报