数据结构与算法java - 05 环形单向链表 约瑟夫问题

单向环形链表

Josephu 约瑟夫环问题

编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m的那个人出列,他的下一位又从1开始报数,报到m的那个人出列,以此类推,直到所有人出列,由此产生一个出队编号的序列

构建单向环形链表

  1. 先创建第一个节点,让 first 指向该节点,并形成环形
  2. 后面当每创建一个新的节点,就把该节点加入到已有的环形链表中即可

遍历环形链表

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

出队列

  1. 需要创建一个辅助指针(变量)helper,事先指向环形链表的最后一个节点

    报数前,先让 first 和 helper 移动 k-1 次

  2. 当小孩报数时,让 first 和 helper 指针同时的移动 m-1 次

  3. 这是可以讲 first 指向需要出列的小孩

  4. 出列:

    first = first.next

    helper.next = first

    原来 first 指向的节点就没有任何引用,被垃圾回收


代码实现

package doublelinkedlist;

public class CircleLinkedList {
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(25);
        circleSingleLinkedList.showBoy();
        circleSingleLinkedList.countBoy(1,2,25);
    }
}

// create circle single linked list
class CircleSingleLinkedList{
    // create a first node
    private Boy first = null;
    // add boy nodes
    public void addBoy(int nums){ // nums: # of nodes
        // check nums
        if(nums < 1){
            System.out.println("nums incorrect");
            return;
        }
        Boy curBoy = null; // auxiliary pointer
        // for loop create circle single LL
        for (int i = 1; i <= nums; i++) {
            Boy boy = new Boy(i);
            if(i==1){
                first = boy;
                first.setNext(first); // form a circle with one element
                curBoy = first;
            }else{
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }
    // traverse list
    public void showBoy(){
        // whether the list is empty?
        if(first == null){
            System.out.println("empty");
            return;
        }
        Boy curBoy = first;
        while(true){
            System.out.printf("id of boy is %d\n",curBoy.getId() );
            if(curBoy.getNext()==first){ // traversal done
                break;
            }
            curBoy = curBoy.getNext();
        }
    }
    // pop out list

    /**
     *
     * @param startId start from which id
     * @param countNum count how many times
     * @param nums # of id in the list initially
     */
    public void countBoy(int startId, int countNum,int nums){
        // check data
        if(first == null || startId<1 || startId>nums){
            System.out.println("Some wrong with parameters");
            return;
        }
        // create a helper pointer
        Boy helper = first;
        while(true){
            if(helper.getNext()==first){ // helper point to the last boy
                break;
            }
            helper = helper.getNext();
        }
        // before function goes, move first and helper k-1 step
        for (int j = 0; j < startId-1; j++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        // when boy reported, move first and helper m-1 step at the same time
        // loop, till there only 1 boy left in the list
        while(true){
            if(helper == first){ // only 1 node left
                break;
            }
            // move helper and first countNum-1 step
            for (int i = 0; i < countNum-1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            // now, first point to the node should be popped out
            System.out.printf("child %d go out\n",first.getId());
            // move the node pointed by first out
            first = first.getNext();
            // helper.next = first; next is private;
            helper.setNext(first);
        }
        System.out.printf("the last node is %d\n",first.getId());

    }
}

// create a class named boy, means a node
class Boy {
    private int id;
    private Boy next; // point to next node, default null
    public Boy(int id){
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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、付费专栏及课程。

余额充值