Java环形链表约瑟夫环问题

1,先创建一个节点,让frist指向改节点,并形成环形
2,后面当我们每创建一个新节点,就把该节点加入已有环形链表中即可
遍历环形链表
1.让辅助指针,指向first
2然后用while遍历
好了,上代码

package yanhaochen;

import java.util.Scanner;

public class hello{

    public static void main(String[] args) {
      //test
    CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
    System.out.println("请输入编号范围");
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    circleSingleLinkedList.addBoy(n);
    circleSingleLinkedList.showBoy();
    }
}
//创建一个环形的单项链表

class CircleSingleLinkedList {

        //创建一个first节点,没有编号

    private Boy first = null;
    //添加小孩,构建环形链表

    public void addBoy(int nums){
        //nums数据校验
        if (nums<1){
            System.out.println("数据不正确");
            return;
        }
        //用for去创建链表
        Boy curboy = null;
        //辅助指针

        for (int i=1;i<=nums;i++){
            //创建节点

            Boy boy = new Boy(i);
            //如果是第一个小孩
            if (i==1){
                first=boy;
                first.setNext(first);
                //构成环状
                curboy=first;
            }else {
                curboy.setNext(boy);
                boy.setNext(first);
                curboy = boy;
            }
        }
    }
    //遍历
    public void showBoy(){
        //链表是否为空
        if(first==null){
            System.out.println("链表为空!!");
            return;
        }
        Boy curboy = first;
        while (true){
            System.out.printf("小孩的编号%d\n",curboy.getNo());
            if (curboy.getNext()==first){
                System.out.println("遍历完毕");
                break;
            }
            curboy=curboy.getNext();
            //currboy后移
        }
    }
}

//创建一个Boy,表示一个节点

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 next) {
        this.next = next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
约瑟夫环问题是一个经典的问题,可以使用双向循环链表来解决。具体的思路可以按照以下步骤来实现: 1. 创建一个双向循环链表,并添加指定数量的节点 2. 定义一个计数器,用于计数当前报数的人数 3. 从链表的头部开始遍历,每次遍历到一个节点时,计数器加1,如果计数器的值等于指定的报数值,就将该节点从链表中删除,并将计数器重置为1 4. 当链表中只剩下一个节点时,即为最后的获胜者 下面是一个 Java 实现的示例代码: ```java public class JosephCircle { // 定义双向链表节点 private static class Node<T> { T item; Node<T> prev; Node<T> next; Node(T item, Node<T> prev, Node<T> next) { this.item = item; this.prev = prev; this.next = next; } } public static void main(String[] args) { int n = 7; // 总人数 int m = 3; // 报数值 Node<Integer> head = null; Node<Integer> tail = null; // 创建双向链表 for (int i = 1; i <= n; i++) { if (head == null) { head = new Node<>(i, null, null); tail = head; head.prev = tail; tail.next = head; } else { Node<Integer> node = new Node<>(i, tail, head); tail.next = node; tail = node; head.prev = tail; } } // 从头节点开始遍历链表 Node<Integer> current = head; int count = 1; while (current.next != current) { count++; if (count == m) { count = 1; System.out.println("删除节点:" + current.item); current.prev.next = current.next; current.next.prev = current.prev; current = current.next; } else { current = current.next; } } System.out.println("获胜者:" + current.item); } } ``` 输出结果: ``` 删除节点:3 删除节点:6 删除节点:2 删除节点:7 删除节点:5 删除节点:1 获胜者:4 ``` 以上就是使用 Java 双向循环链表求解约瑟夫环问题的示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值