博客
关于我
数据结构-----环形链表
阅读量:79 次
发布时间:2019-02-26

本文共 3314 字,大约阅读时间需要 11 分钟。

????????????????

???????????????????n???????????????????????????????????????????????????

????

???????????????1?n?n???????????????????m??????????????????????????????????????????

????n=5?k=1?m=2???????2?4?1?5?3?

??????

???????????????????????????????????????

  • ???????????????????????????????????????????????????????????????

  • ????????????????????????helper?????????????????????????????????

  • ???????????????????first??m-1?????????????????

  • ???????????????????????????????????first???????????

  • ????????????????????????

  • ????

    package com.wang.linkedlist;class Boy {    private int no;    private Boy next;    public Boy(int no) {        this.no = no;        this.next = null;    }    public int getNo() {        return no;    }    public void setNext(Boy next) {        this.next = next;    }}class CircleSingleLinkedList {    private Boy first;    public CircleSingleLinkedList() {        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;            }        }    }    public void showBoy() {        if (first == null) {            System.out.println("?????????");            return;        }        Boy curBoy = first;        while (true) {            System.out.println("??????" + curBoy.getNo());            if (curBoy.getNext() == first) {                break;            }            curBoy = curBoy.getNext();        }    }    public void countBoy(int startNo, int countNum, int nums) {        if (first == null || startNo < 1 || startNo > nums) {            System.out.println("????????????");            return;        }        Boy helper = first;        while (true) {            if (helper.getNext() == first) {                break;            }            helper = helper.getNext();        }        for (int j = 0; j < startNo - 1; j++) {            helper = helper.getNext();        }        Boy current = helper;        while (nums > 0) {            for (int i = 0; i < countNum - 1; i++) {                helper = helper.getNext();                if (helper == null) {                    helper = first;                }            }            System.out.println("?????" + current.getNo());            current = current.getNext();            if (current == null) {                current = first;            }            first = first.getNext();            helper.setNext(first);            nums--;        }    }    public static void main(String[] args) {        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();        circleSingleLinkedList.addBoy(5);        circleSingleLinkedList.showBoy();        circleSingleLinkedList.countBoy(1, 2, 5);    }}

    ????

  • Boy?????????????????????????

  • CircleSingleLinkedList??

    • addBoy(int nums)??????????n????????????
    • showBoy()??????????????????
    • countBoy(int startNo, int countNum, int nums)??????????????
  • ??????????????????????????????????????

  • ??

    ????????????????????????????????????????????????????????????

    转载地址:http://ooik.baihongyu.com/

    你可能感兴趣的文章
    React input defaultValue不会更新状态怎么办?
    查看>>
    PHP函数__autoload失效原因(与smarty有关)
    查看>>
    PHP函数判断移动端和PC端
    查看>>
    Springboot基础入门
    查看>>
    php函数性能优化中应注意哪些问题?
    查看>>
    PHP函数操作数字和汉字互转(100以内)
    查看>>
    PHP函数方法
    查看>>
    PHP创建目录mkdir无写入权限的问题解决方案
    查看>>
    PHP删除指定目录下的所有文件和文件夹 | 删除指定文件
    查看>>
    php删除文件夹下面所有文件包括(删除文件夹)不删除文件夹
    查看>>
    React Collapse Pane 项目教程
    查看>>
    php判断ip黑名单程序代码
    查看>>
    php判断复选框是否被选中的方法
    查看>>
    PHP判断指定目录下是否存在文件
    查看>>
    php判断数组是否为空
    查看>>
    PHP判断数组是否有重复值、获取重复值
    查看>>
    springboot基于Web的社区留守儿童管理系统源码毕设+论文
    查看>>
    Springboot基于Redisson实现Redis分布式可重入锁【案例到源码分析】
    查看>>
    PHP利用正则表达式实现手机号码中间4位用星号(*)替换显示
    查看>>
    PHP加密与安全的最佳实践
    查看>>