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

    你可能感兴趣的文章
    node.js 配置首页打开页面
    查看>>
    node.js+react写的一个登录注册 demo测试
    查看>>
    Node.js中环境变量process.env详解
    查看>>
    Node.js卸载超详细步骤(附图文讲解)
    查看>>
    Node.js安装与配置指南:轻松启航您的JavaScript服务器之旅
    查看>>
    Node.js安装及环境配置之Windows篇
    查看>>
    Node.js安装和入门 - 2行代码让你能够启动一个Server
    查看>>
    Node.js的循环与异步问题
    查看>>
    NodeJS @kubernetes/client-node连接到kubernetes集群的方法
    查看>>
    Nodejs express 获取url参数,post参数的三种方式
    查看>>
    nodejs http小爬虫
    查看>>
    nodejs libararies
    查看>>
    nodejs npm常用命令
    查看>>
    NodeJS 导入导出模块的方法( 代码演示 )
    查看>>
    nodejs 的 Buffer 详解
    查看>>
    nodejs 读取xlsx文件内容
    查看>>
    nodejs 运行CMD命令
    查看>>
    nodejs-mime类型
    查看>>
    NodeJs——(11)控制权转移next
    查看>>
    NodeJS、NPM安装配置步骤(windows版本)
    查看>>