Java数据结构与算法Day03 【环形链表】

这个寒假有点长呀~~~~之前学数据结构与算法时,用C语言实现,现在开始学Java了,就决定用Java重新学习一下,学以致用呀!
每天一点点,积少成多!-_-
学习视频网址:Java数据结构与算法视频教程

单向环形链表

一、问题引入:约瑟夫环问题

1. 约瑟夫环问题

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

2. 解决思路

提示:用一个不带头节点的环形链表来处理约瑟夫问题,先构成一个有n个节点的单向环环链表,然后由k节点起从1开始计数,计到m时,对应节点从链表中删除,然后再从被删除节点的下一个节点从1开始计数,直到最后一个节点从链表中删除,算法结束。

二、代码实现:约瑟夫问题

1.分析一下

1.1 构建一个单向的环形链表
  1. 先创建第一个节点,让first指向该节点,并形成环形;
  2. 每当我们每创建一个新的节点,就该把该节点加入到已有的环形链表中。
1.2 遍历环形链表
  1. 先让一个辅助指针变量curBoy,指向first节点;
  2. 通过一个while循环遍历该环形链表即可。当curBoy.next == first时结束。
1.3 生成出圈序列
  1. 需要创建一个辅助变量helper,指向环形链表的最后一个节点;
  2. 小孩报数前,先让first和helper移k-1次;
  3. 当小孩报数时,first和helper指针同时移动m-1次;
  4. 将first指向的小孩节点出圈;
    first = first.next; helper.next = first;
  5. 原来first指向的节点将没有任何引用,将被回收。

2. 代码实现

2.1 创建Boy类
//创建一个Boy类,表示一个节点
class Boy{
    private int no; //编号
    private Boy next;//指向下一个节点,默认为null
    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;
    }
}
2.2 创建环形的单向链表
//创建一个环形的单向链表
class CircleSingleLinkedList{
    //创建一个first节点,当前没有编号
    private Boy first = null;

    //添加小孩节点,构建成一个环形链表
    public void addBoy(int nums){
        if (nums < 1){
            System.out.println("nums的值不正确!");
            return;
        }

        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;
            }
        }
    }
}

2.3 遍历环形链表
    public void showBoy(){
        //判断链表是否为空
        if (first == null){
            System.out.println("没有任何小孩!");
            return;
        }

        //由于first不能动,使用一个辅助指针完成遍历
        Boy curBoy = first;
        while(true){
            System.out.printf("小孩的编号为%d\n",curBoy.getNo());
            if (curBoy.getNext() == first){  //遍历完毕
                break;
            }
            curBoy = curBoy.getNext();  //curBoy后移
        }
    }
2.4 实现出圈序列
    //根据用户的输入,计算出小孩出圈的序列
    /**
     *
     * @param startNo   表示从第几个小孩开始数数
     * @param countNum  表示数到几【m】
     * @param nums      表示最初有多少个孩子在圈中
     */
     public void countBoy(int startNo, int countNum, int nums){

         //先对数据进行校验
         if (first == null || startNo < 1 || startNo > nums){
             System.out.println("参数输入有误,请重新输入!");
             return;
         }

         //创建一个辅助变量helper,指向环形链表的最后一个节点
         Boy helper = first;   //辅助指针
         while (true){
             if (helper.getNext() == first){
                 break;
             }
             helper = helper.getNext();
         }
         //小孩报数前,先让first和helper移startNo-1次
         for (int j = 0; j < startNo -1; j++){
             first = first.getNext();
             helper = helper.getNext();
         }

         //当小孩报数时,first和helper指针同时移动countNum-1次,然后出圈
         while(true){
             if (helper == first){  //圈中只有一个节点
                 break;
             }
             //first和helper指针同时移动countNum-1次
             for (int j = 0; j < countNum - 1; j++){
                 first = first.getNext();
                 helper = helper.getNext();
             }
             //这时first要指向的节点,就是要出圈的小孩节点
             System.out.printf("小孩%d出圈\n",first.getNo());
             //将first指向的小孩节点出圈
             first = first.getNext();
             helper.setNext(first);
         }

         System.out.printf("最后留在圈中的小孩编号为%d\n",first.getNo());
     }
发布了32 篇原创文章 · 获赞 3 · 访问量 1356

猜你喜欢

转载自blog.csdn.net/weixin_44270855/article/details/104213583
今日推荐