12.20环形单向链表

环形单向链表实现

package zhangs;

public class Clinked {
    public static void main(String[] args) {
        Linkedboy linkedboy = new Linkedboy();
        linkedboy.addBoy(25);
        linkedboy.show();
    }
}
class Linkedboy{
    public Boy first = null;
    public void addBoy(int number){
        Boy cur = first;
        if (number == 0){
            System.out.println("不能为0");
            return;
        }
        //循环创建节点,让第一个进来的节点为first节点.并让他组成环形,最后一个节点next指向第一个节点
        for (int i = 1 ; i <= number+1 ;i++){
            Boy boy = new Boy(i);
            if(first == null){
                first = boy;
                first.next = first;
                cur = boy;
            }else {
                cur.next = boy;
                boy.next = first;
                cur = boy;
            }
        }
    }
    public void show(){
        Boy cur = first;
        if (first == null){
            System.out.println("空链表");
            return;
        }
        //判断下一个boy是不是为第一个boy,如果是结束循环
        while (cur.next != first){
            System.out.println(cur);
            cur = cur.next;
        }
    }
}
class Boy{
    public int no;
    public Boy next;

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

    @Override
    public String toString() {
        return "boy{" +
                "no=" + no +
                '}';
    }
}

标题约瑟夫问题单向环形链表实现

package zhangs;

public class Clinked {
    public static void main(String[] args) {
        Linkedboy linkedboy = new Linkedboy();
        linkedboy.addBoy(25);
        linkedboy.show();
        linkedboy.countboy(1,2);
    }
}
class Linkedboy{
    public Boy first = null;
    public void countboy(int startnum,int number){
        Boy firstP = first;
        Boy temp = first;
        if (startnum <= 0 || number <= 0){
            System.out.println("数据不合法");
        }
        while (true){
            if (temp.next == first){
                break;
            }
            temp = temp.next;
        }
        for (int i = 0 ;i < startnum-1 ; i++){
            firstP = firstP.next;
            temp = temp.next;
        }
        while (true){
            if (firstP.next == firstP){
                break;
            }
            for (int j = 0 ; j < number-1 ; j++){
                firstP = firstP.next;
                temp = temp.next;
                System.out.println("小孩出圈"+firstP);
            }
            temp.next = firstP.next;
            firstP = temp.next;
        }
        System.out.println("最后小孩出圈"+firstP);
    }
    public void addBoy(int number){
        Boy cur = first;
        if (number == 0){
            System.out.println("不能为0");
            return;
        }
        //循环创建节点,让第一个进来的节点为first节点.并让他组成环形,最后一个节点next指向第一个节点
        for (int i = 1 ; i <= number ;i++){
            Boy boy = new Boy(i);
            if(first == null){
                first = boy;
                first.next = first;
                cur = boy;
            }else {
                cur.next = boy;
                boy.next = first;
                cur = boy;
            }
        }
    }
    public void show(){
        Boy cur = first;
        if (first == null){
            System.out.println("空链表");
            return;
        }
        //判断下一个boy是不是为第一个boy,如果是结束循环
        while (cur.next != first){
            System.out.println(cur);
            cur = cur.next;
        }
        System.out.println(cur);
    }
}
class Boy{
    public int no;
    public Boy next;

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

    @Override
    public String toString() {
        return "boy{" +
                "no=" + no +
                '}';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值