博客
关于我
数据结构-----环形链表
阅读量: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/

    你可能感兴趣的文章
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>
    npm配置安装最新淘宝镜像,旧镜像会errror
    查看>>
    NPM酷库052:sax,按流解析XML
    查看>>
    npm错误 gyp错误 vs版本不对 msvs_version不兼容
    查看>>
    npm错误Error: Cannot find module ‘postcss-loader‘
    查看>>
    npm,yarn,cnpm 的区别
    查看>>
    NPOI
    查看>>
    NPOI之Excel——合并单元格、设置样式、输入公式
    查看>>
    NPOI初级教程
    查看>>
    NPOI利用多任务模式分批写入多个Excel
    查看>>
    NPOI在Excel中插入图片
    查看>>
    NPOI将某个程序段耗时插入Excel
    查看>>
    NPOI格式设置
    查看>>