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

    你可能感兴趣的文章
    pandas去除Nan值
    查看>>
    pandas实战:电商平台用户分析
    查看>>
    Pandas库函数
    查看>>
    Pandas库常用方法、函数集合
    查看>>
    pandas打乱数据的顺序
    查看>>
    pandas指定列数据归一化
    查看>>
    pandas改变一列值(通过apply)
    查看>>
    Pandas数据分析的环境准备
    查看>>
    Pandas数据可视化怎么做?用实战案例告诉你!
    查看>>
    Pandas数据处理与分析教程:从基础到实战
    查看>>
    Pandas数据结构之DataFrame常见操作
    查看>>
    pandas整合多份csv文件
    查看>>
    pandas某一列转数组list
    查看>>
    Pandas模块,我觉得掌握这些就够用了!
    查看>>
    Pandas玩转文本处理!
    查看>>
    SpringBoot 整合 Mybatis Plus 实现基本CRUD功能
    查看>>
    pandas的to_sql方法中使用if_exists=‘replace‘
    查看>>
    Springboot ppt转pdf——aspose方式
    查看>>
    pandas读取csv编码utf-8报错
    查看>>
    pandas读取parquet报错
    查看>>