环形链表实现约瑟夫问题

看到网上很多示例编写的代码都不够精练简洁,现对“约瑟夫问题”用JAVA实现如下:

/**
 * 使用环形链表实现约瑟夫问题
 */
class RoundLinked{

    //头节点
    private Node<Integer> head=new Node<>();
    //尾节点
    private Node<Integer> tail=head;
    private int size=0;

    public RoundLinked(int nums){
        make(nums);
        this.size=nums;
    }

    public boolean empty(){
        return head==tail;
    }

    /**
     *
     * @param k 从第几个开始计数
     * @param count 到第几个的时候重新开始数
     */
    public void Josephus(int k,int count){

        if(empty()){
            return;
        }
        if(k>=size){
            System.out.println("k的值不合法");
            return;
        }


        //1.旋转到第k个节点
        int tmpIndex=1;
        do {
            if(k==tmpIndex){
                break;
            }
            head=head.next;
            tail=tail.next;
            tmpIndex++;
        }while (true);


        for (;;){
            if(empty()){
                break;
            }
            //2.开始丢手帕
            for (int i = 0; i < count; i++) {
                head=head.next;
                tail=tail.next;
            }
            System.out.println("死掉:"+head);
            //跳过当前节点
            head= head.next;
            //重连
            tail.next=head;
        }
        //3.留在队列当中的
        System.out.println("幸存:"+head);


    }
    private void make(int nums){
        if(nums<1){
            System.out.println("创建的节点不能为空!");
            return;
        }
        for (int i = 0; i < nums; i++) {
            Node<Integer> node=new Node<Integer>(i+1);
            tail.next=node;
            tail=tail.next;
        }
        //将空节点去掉
        head=head.next;
        //首尾相连
        tail.next=head;
    }

    public void ls(){
        if(empty()) return;
        Node<Integer> cur=head;
        do {
            System.out.println(cur);
            cur=cur.next;
        }while (cur!=head);
    }


    static class Node<T>{
        public T data;
        public Node<T> next;

        public Node(){
            this(null);
        }
        public Node(T data,Node<T> next){
            this.data=data;
            this.next=next;
        }

        public Node(T data){
            this(data,null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "data=" + data +
                    '}';
        }
    }
}

测试:

public class RoundLinkedDemo {
    public static void main(String[] args) {
        RoundLinked linked=new RoundLinked(10);
        linked.Josephus(1,1);
    }
}

结果:

死掉:Node{data=2}
死掉:Node{data=4}
死掉:Node{data=6}
死掉:Node{data=8}
死掉:Node{data=10}
死掉:Node{data=3}
死掉:Node{data=7}
死掉:Node{data=1}
死掉:Node{data=9}
幸存:Node{data=5}

发博客真好玩儿._.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值