public class CircleSinglyLinkList {
private Node head;
CircleSinglyLinkList(){
this.head = null;
}
CircleSinglyLinkList(Node head){
this.head = head;
}
//向表头插入一个值为x的结点
public void insertHead(int x){
Node newNode = new Node(x);
if(this.head == null){
this.head = newNode;
newNode.next = this.head;
}else{
Node last = this.head;
newNode.next = this.head;
this.head = newNode;
while(last.next != newNode.next){
last = last.next;
}
last.next = this.head;
}
}
//向表尾插入一个值为x的结点
public void insertTail(int x){
Node newNode = new Node(x);
if(head == null){
this.head = newNode;
newNode.next = this.head;
}
else {
Node cur = this.head;
while(cur.next != this.head){
cur = cur.next;
}
newNode.next = this.head;
cur.next = newNode;
}
}
public void deleteData(int data){
Node cur = findNode(data);
Node last = this.head; if(cur == null){
return;
}
if(cur == this.head){
while(last.next != this.head){
last = last.next;
}
this.head = cur.next;
last.next = this.head;
cur = null;
return ;
}
Node pre = head;
while(pre.next != cur){
pre = pre.next;
}
pre.next = pre.next.next;
cur = null;
} public Node findNode(int data){
if(head == null){
System.out.println("链表为空");
return null;
}
Node cur = head;
while (cur.next != head && cur.value != data){
cur = cur.next;
}
return cur;
}
@Override
public String toString() {
Node cur = head;
String str = "";
StringBuilder newStr = new StringBuilder("");
while(cur.next != head){
newStr.append(cur.value+"--->");
cur = cur.next;
}
newStr.append(cur.value+"--->头");
return newStr.toString();
} public static void main(String[] args) {
CircleSinglyLinkList circleSinglyLinkedList= new CircleSinglyLinkList();
// Josephus
int peopleNum = 10;
int each = 3;
int count = 1;
for (int i = 0; i < peopleNum; i++) {
circleSinglyLinkedList.insertTail(i+1);
}
// System.out.println(circleSinglyLinkedList.toString());
Node another = circleSinglyLinkedList.head;
Node pre = another;
while(true){
if(another.next == another){
break;
}
if(count%3 == 0){
pre = another.next;
circleSinglyLinkedList.deleteData(another.value);
count++;
another = pre;
System.out.println(circleSinglyLinkedList.toString());
}else{
another = another.next;
count++;
} }
//// circleSinglyLinkedList.deleteData(0);
// circleSinglyLinkedList.deleteData(1);
// circleSinglyLinkedList.insertHead(1);
//// circleSinglyLinkedList.deleteData(1);
// circleSinglyLinkedList.insertTail(2);
//// System.out.println(circleSinglyLinkedList.findNode(1).value);
//// circleSinglyLinkedList.deleteData(1);
//// circleSinglyLinkedList.deleteData(90);
// circleSinglyLinkedList.deleteData(1);
// System.out.println(circleSinglyLinkedList.toString());
}
}

这个是java语言的实现,(我写的),没有写很多功能,但是基本的有了,可以实现那个算法了。注意的问题都在c语言里面解释了。

单循环链表(基于java语言)的更多相关文章

  1. 基于JAVA语言的多线程技术

    1.简介 多线程技术属于操作系统范围内的知识: 进程与线程 可以这么理解,一个应用程序就是一个进程,在一个进程中包含至少一个线程:进程就是线程的容器,真正工作.处理任务的是线程. 进程是操作系统分配资 ...

  2. JFinal -基于Java 语言的MVC极速 web 开发框架

    JFinal概述 JFinal 是基于Java 语言的极速 web 开发框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restful.在拥有Java语言所有优势的同时再 ...

  3. 基于Java语言开发jt808、jt809技术文章精华索引

    很多技术开发人员喜欢追逐最新的技术,如Node.js, go等语言,这些语言只是解决了某一个方面,如只是擅长异步高并发等等,却在企业管理后台开发方面提供的支持非常不够,造成项目团队技术选项失败,开发后 ...

  4. 【数据结构】之链表(Java语言描述)

    以前写过一篇帖子,记录了链表在C语言中的描述代码.C语言中没有链表的直接实现,因此,我们需要自己编写代码实现.请参考[我的这篇文章]. Java中默认为我们提供了链表的API—— LinkedList ...

  5. 《神经网络算法与实现-基于Java语言》的读书笔记

    文章提纲 全书总评 读书笔记 C1.初识神经网络 C2.神经网络是如何学习的 C3.有监督学习(运用感知机) C4.无监督学习(自组织映射) Rreferences(参考文献) 全书总评 书本印刷质量 ...

  6. 基于java语言的给cube添加custom view来实现权限控制

    今天是农历2014年的最后一个工作日了,在这里提前祝大家新年快乐.羊年大吉!当然本人今天也拿出来点儿真东西,做为献给大家的新年礼物,依次共勉. 下文主要讲述的是使用Java代码来完成对cube基于部门 ...

  7. 基于JAVA语言的selenium测试基础总结

    目录一.基本语句1.循环控制(break,continue)3.字符的替换(replace,repalceFirst,replaceAll,regex)4.字符串的连接("+",a ...

  8. Selenium(基于JAVA语言)-》在eclipse上运行web项目在Mac系统上启动时提示nodename nor servname provided解决办法

    最近使用eclipse进行自动化测试时,遇到一种情况,无法调起浏览器,且有报错,如下: org.openqa.selenium.WebDriverException: failed to lookup ...

  9. 基于JAVA语言的selenium总结

    目录一.基本语句 1.循环控制(break,continue) 3.字符的替换(replace,repalceFirst,replaceAll,regex) 4.字符串的连接("+" ...

随机推荐

  1. 记录netcore一次内存暴涨的坑

    项目用到了Coldairarrow/EFCore.Sharding: Database Sharding For EFCore (github.com)这个组件,最初是因为分表做的还不错所以用了它. ...

  2. Linux 源码安装Ansible 参考篇

    Ansible 源码搭建配置 近期在学习自动化运维相关技术,文章主要模拟内网情况下对Ansible的安装演示,源码安装较为繁琐.枯燥,尤其是在实际安装过程中可能出现各式各样的问题,所有在安装过程中尽量 ...

  3. Java中Integer 和 int的区别

    基本概念的区分: 1.Integer 是 int 的包装类,int 则是 java 的一种基本数据类型 2.Integer 变量必须实例化后才能使用,而int变量不需要 3.Integer 实际是对象 ...

  4. Solution Set -「LOCAL」冲刺省选 Round XXIV

    \(\mathscr{Summary}\)   名副其实的 trash round,希望以后没有了.   A 题算好,确实一个比较关键的简化状态的点没想到,所以只拿了暴力(不考虑 \(\mathcal ...

  5. Filter(过滤器)与Listener(监听器)详解

    11.Filter(重点) Filter:过滤器,用来过滤网站的数据: 处理中文乱码 登陆验证... Filter开发步骤: 导包 编写过滤器 导包不要错 实现Filter接口,重写对应的方法即可 p ...

  6. suse 12 二进制部署 Kubernetets 1.19.7 - 第00章 - 环境准备

    文章目录 0.环境准备 0.0.修改主机名 0.1.添加hosts解析 0.2.配置ssh免密 0.3.发送hosts解析文件到其他节点,并修改hostname 0.4.更新PATH变量 0.5.安装 ...

  7. 聊聊几个阿里 P8、P9 程序员的故事

    大家好,我是对白. 阿里 P8 程序员年薪百万已经是公开的秘密了,有人关心他们年薪百万,而我更加关注阿里这些 P8.P9 程序员的成长故事,在聊这些大牛的故事之前,跟大家稍微简单聊下阿里技术人等级制度 ...

  8. 关于iOS APP转让

    需要以下条件即可

  9. 学着去看开 你不是一个low程序员

    http://www.cocoachina.com/programmer/20160627/16835.html 看完这篇文章 感谢作者

  10. 使用SpringBoot整合MybatisPlus出现 : java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    解决方案一: 将测试类的包路径改为和主启动类的一致 解决方法二: 不想改测试类的路径 就在测试类上添加要测试的类的classes