java环形单向链表 && Josepfu问题


/**
 * @author Drug
 * @create 2020-04-16 18:00
 */
public class Josepfu {
	//测试
    public static void main(String[] args) {
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);
        circleSingleLinkedList.countBoy(1, 2, 5);
    }

}
//环形链表
class CircleSingleLinkedList{
    private Boy fist=null;

    public Boy getFist() {
        return fist;
    }

    //创建环形链表
    public void addBoy(int num){
        if(num < 1){
            System.out.println("输入的数据不正确");
        }
        Boy currBoy = null;
        for(int i=1;i<=num;i++){
            if(i==1){
                Boy boy = new Boy(i);
                fist = boy;
                fist.setNext(fist);
                currBoy = boy;
            }else{
                Boy boy = new Boy(i);
                currBoy.setNext(boy);
                boy.setNext(fist);
                currBoy = boy;
            }
        }
    }

    //遍历环形链表
    public void show(){
        if(fist == null){
            System.out.println("链表为空");
            return;
        }
        Boy currBoy = fist;

        while (true){
            System.out.println("当前编号的节点数据是:"+currBoy.getNo());
            if(currBoy.getNext() == fist){
                break;
            }
            currBoy = currBoy.getNext();
        }
    }

    //约瑟夫问题(依次出圈)
    public void countBoy(int startNo,int countNo,int nums){
        if(fist==null || startNo<1 || startNo>nums){
            System.out.println("输入有误");
            return;
        }
        Boy help = fist;
        //将help指向fist后一个
        while(true){
            if(help.getNext()==fist){
                break;
            }
            help = help.getNext();
        }
		//从几个Count开始,移动两个指针
        for(int i=0;i<startNo-1;i++){
            fist = fist.getNext();
            help = help.getNext();
        }
		//出圈算法
        while(true){
			//fist指针和help指针重合时,圈内只有一个节点
            if(fist == help){
                break;
            }
            for(int i=0;i<countNo-1;i++){
                fist = fist.getNext();
                help = help.getNext();
            }
            System.out.println("本轮出圈孩子的编号是:"+fist.getNo());
            fist = fist.getNext();
            help.setNext(fist);
        }
        System.out.println("最后留下的孩子是:"+help.getNo());
    }

}

class Boy{
    private int no;//编号
    private Boy next;//指针域

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setNext(Boy next) {
        this.next = next;
    }

    public Boy getNext() {
        return next;
    }

    public Boy(int no){
        this.no = no;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值