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. 安装Varnish 及遇到的坑

      转自:http://ixdba.blog.51cto.com/2895551/682555   一.安装Varnish Varnish的安装非常简单,下面逐步介绍: 1.安装前的准备  Varni ...

  2. MySQL索引类型一览 让MySQL高效运行起来(转)

    转自:http://www.php100.com/html/webkaifa/database/Mysql/2010/0409/4279.html 索引是快速搜索的关键.MySQL索引的建立对于MyS ...

  3. 无意进去UIView随笔闹腾着玩 -by 胡 xu

    1 @interface UIView : UIResponder<NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem> ...

  4. ROS::message_filters中的一个报错(mt::TimeStamp……)

    『方便检索』 ros::Time msg_time = mt::TimeStamp<typename mpl::at_c<Messages, i>::type>::value( ...

  5. 3、前端--伪元素选择器、选择器优先级、字体、背景、边框、display、盒子模型

    伪元素选择器 # 首字调整>>>:也是一种文档布局的方式 p:first-letter { font-size: 48px; /*字体大小*/ color: red; } # 在文本 ...

  6. windows设备相关位图与设备无关位图

    windows支持两种位图格式,DDB(device-dependent bitmap),DIB(device-independent bitmap).设备相关位图用于windows显示系统中,其图像 ...

  7. verification 验证环境配置传递

    验证环境配置传递 tc配置env 继承关系: tc_base->tc_base_bt->tc_xx base_env->xx_env base_env_cfg->xx_env_ ...

  8. Redis 源码简洁剖析 16 - 客户端

    整体概述 客户端属性 套接字描述符 标志 输入缓冲区 命名及命令参数 命令的实现函数 输出缓冲区 客户端的创建与关闭 创建普通客户端 关闭普通客户端 参考链接 Redis 源码简洁剖析系列 整体概述 ...

  9. spring循环依赖的产生与解决

    1.循环依赖的产生 在spring中对象默认都是单例的 ,意味整个容器中只有一个该类的对象. 如图,B类有一个属性a,A类有一个属性b.当B类创建对象时,要给a属性赋值:当A类创建对象时,要给b属性赋 ...

  10. Nginx频繁报“500 Internal Server Error”错误

    服务器导致访问量激增,频繁报"500 Internal Server Error"错误.我查了一下nginx的错误日志(apt-get方式安装的nginx的错误日志在/var/lo ...