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

    你可能感兴趣的文章
    Nginx 负载均衡详解
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置https(一)—— 自签名证书
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx 配置解析:从基础到高级应用指南
    查看>>
    nginx+Tomcat性能监控
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
    查看>>
    nginx添加模块与https支持
    查看>>
    Nginx用户认证
    查看>>
    Nginx的Rewrite正则表达式,匹配非某单词
    查看>>
    Nginx的使用总结(一)
    查看>>
    Nginx的使用总结(二)
    查看>>
    Nginx的可视化神器nginx-gui的下载配置和使用
    查看>>
    Nginx的是什么?干什么用的?
    查看>>
    Nginx访问控制_登陆权限的控制(http_auth_basic_module)
    查看>>
    nginx负载均衡器处理session共享的几种方法(转)
    查看>>
    nginx负载均衡的5种策略(转载)
    查看>>
    nginx负载均衡的五种算法
    查看>>
    Nginx运维与实战(二)-Https配置
    查看>>