用惯了C++,java写起来果然不太爽。。。不废话了,上代码。。。

package javaInnerclassDemo;

class Link{
class Node{
private String name;
private Node next;
public Node(String name){
this.name=name;
}
public void setname(String name){
this .name = name;
}
public String getname(){
return this.name;
}
public void addnode(Node newnode){
if(this.next==null)
this.next=newnode;
else
this.next.addnode(newnode);
}
public void printnode(){
if(this.next!=null){
System.out.print(this.name);
System.out.print("——>");
}
else
System.out.println(this.name);
if(this.next!=null)
this.next.printnode();
}
public boolean searchnode(String name){
if(this.name.equals(name)){
return true ;
}
else{
if(this.next!=null){
return this.next.searchnode(name) ;
}
else{
return false ;
}
}
}
public void deleteNode(Node preNode,String name){
if(this.name.equals(name)){
preNode.next = this.next ;
}else{
this.next.deleteNode(this,name) ;
}
}
}
private Node root;
public void add(String name){
Node newnode = new Node(name);
if(this.root==null)
this.root=newnode;
else
this.root.addnode(newnode);
}
public void print(){
if(this.root!=null){ //之所以在外部判断,因为printnode需要迭代
this.root.printnode();
}
else
System.out.println("链表为空,无法打印!");
}
public boolean search(String name){
if(this.root.searchnode(name)==true)
return true;
else
return false;
}
public void delete(String name){
if(this.search(name)){ // 判断此节点是否存在
if(this.root.name.equals(name)){
if(this.root.next!=null){
this.root = this.root.next ; // 改变根节点
}
else{
this.root = null ; // 取消
}
}
else{
if(this.root.next!=null){
this.root.next.deleteNode(root,name) ;
}
}
}
else
System.out.println("所要删除节点不存在!");
}
}
public class LinklistDemo { public static void main(String[] args) {
Link l = new Link();
l.add("walkthehorizon");
l.add("已经");
l.add("无人");
l.add("能挡");
l.add("了");
l.add("么");
System.out.println("打印链表");
l.print();
System.out.println("查找链表");
System.out.println(l.search("walkthehorizon"));
System.out.println(l.search("放逐之刃"));
System.out.println("删除节点");
l.delete("么");
l.print();
}
}

java的单链表实现核心在于多层次迭代。

java单链表代码实现的更多相关文章

  1. Java单链表反转 详细过程

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/guyuealian/article/details/51119499 Java单链表反转 Java实 ...

  2. Java单链表反转图文详解

    Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...

  3. java 单链表 练习

    练习一下java单链表的简单习题 package com.test1; import java.util.Stack; public class SingleListDemo { /** * 返回单链 ...

  4. java单链表常用操作

    总结提高,与君共勉 概述. 数据结构与算法亘古不变的主题,链表也是面试常考的问题,特别是手写代码常常出现,将从以下方面做个小结 [链表个数] [反转链表-循环] [反转链表-递归] [查找链表倒数第K ...

  5. JAVA单链表的实现-不带头结点但带有尾指针

    1,本程序实现了线性表的链式存储结构.实现的链表带有两个指针,一个始终指向链表中的第一个结点,另一个指针始终指向链表中的最后一个结点. 之所以设置尾指针,是因为,在插入元素到链表中的末尾时,可以通过尾 ...

  6. java 单链表反转

    最近与人瞎聊,聊到各大厂的面试题,其中有一个就是用java实现单链表反转.闲来无事,决定就这个问题进行一番尝试. 1.准备链表 准备一个由DataNode组成的单向链表,DataNode如下: pub ...

  7. JAVA单链表的实现-不带头结点且没有尾指针

    本程序采用JAVA语言实现了线性表的链式实现.首先定义了线性表的接口ListInterface,然后LList类实现了ListInterface完成了链表的实现. 本实现中,链表是不带表头结点的,且有 ...

  8. Java单链表简单实现* @version 1.0

    package com.list; /** * 数据结构与算法Java表示 * @version 1.0 * @author 小明 * */ public class MyLinkedList { p ...

  9. 基于python实现单链表代码

    1 """ 2 linklist.py 3 单链表的构建与功能操作 4 重点代码 5 """ 6 7 class Node: 8 " ...

随机推荐

  1. whereis linux文件搜索

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...

  2. codeforces 27E . Number With The Given Amount Of Divisors 搜索+数论

    题目链接 首先要知道一个性质, 一个数x的因子个数等于 a1^p1 * a2^p2*....an^pn, ai是x质因子, p是质因子的个数. 然后就可以搜了 #include <iostrea ...

  3. UIKit之浅析UIButton

    UIButton * button =[[UIButton alloc]init]; button.backgroundColor=[UIColor redColor]; [button setTit ...

  4. Esxi主机虚拟机迁移注意事项

    1. Esxi主机上的虚拟机迁移只能是低----->高,或版本一样的才能进行迁移 [如Esxi5.1---->Esxi5.5]ok, 而Esxi5.5----->Esxi5.1 no ...

  5. visual leak dector内存泄漏检测方法

    http://vld.codeplex.com/ QT 内存泄露时,你们一般用什么工具检测啊 ------解决方案--------------------这篇你觉得详细么 :http://newfac ...

  6. 【Windows 8 Store App】学习一:获取设备信息

    原文http://www.cnblogs.com/java-koma/archive/2013/05/22/3093306.html 通常情况下我们需要知道用户设备的一些信息:deviceId, os ...

  7. CSV 客座文章系列: Pruffi 通过 Windows Azure 挖掘社交媒体的强大招聘潜能

    编辑人员注释:今天这篇文章由 Pruffi 创始人 Alena Vladimirskaya 和 Pruffi 的 CTO Alexander Ivanov 联合撰写,介绍了该公司如何使用 Window ...

  8. linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客

    linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客 linux中读写锁的rwlock介绍 2013-02-26 13:59:35 分类: C/C++   http://yaro ...

  9. thenjs的应用

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. css组件规范

    7月份研究了下 写了下总结. 笔记地址