一、JAVA单向链表的操作(增加节点、查找节点、删除节点)

class Link { // 链表类
class Node { // 保存每一个节点,此处为了方便直接定义成内部类
private String data; // 节点的内容
private Node next; // 保存下一个节点 public Node(String data) { // 通过构造方法设置节点内容
this.data = data;
} public void add(Node node) { // 增加节点
if (this.next == null) { // 如果下一个节点为空,则把新节点加入到next的位置上
this.next = node;
} else { // 如果下一个节点不为空,则继续找next
this.next.add(node);
}
} public void print() { // 打印节点
if (this.next != null) {
System.out.print(this.data + "-->");
this.next.print();
} else {
System.out.print(this.data + "\n");
}
} public boolean search(String data) { // 内部搜索节点的方法
if (this.data.equals(data)) {
return true;
}
if (this.next != null) {
return this.next.search(data);
} else {
return false;
}
} public void delete(Node previous, String data) { // 内部删除节点的方法
if (this.data.equals(data)) {
previous.next = this.next;
} else {
if (this.next != null) {
this.next.delete(this, data);
}
}
}
} private Node root; // 定义头节点 public void addNode(String data) { // 根据内容添加节点
Node newNode = new Node(data); // 要插入的节点
if (this.root == null) { // 没有头节点,则要插入的节点为头节点
this.root = newNode;
} else { // 如果有头节点,则调用节点类的方法自动增加
this.root.add(newNode);
}
} public void print() { // 展示列表的方法
if (root != null) { // 当链表存在节点的时候进行展示
this.root.print();
}
} public boolean searchNode(String data) { // 在链表中寻找指定内容的节点
return root.search(data); // 调用内部搜索节点的方法
} public void deleteNode(String data) { // 在链表中删除指定内容的节点
if (root.data.equals(data)) { // 如果是头节点
if (root.next != null) {
root = root.next;
} else {
root = null;
}
} else {
root.next.delete(this.root, data);
}
}
}

  测试:

public class TestMain {

	public static void main(String[] args) {
Link l = new Link();
l.addNode("A"); l.addNode("B");
l.addNode("C");
l.addNode("D");
System.out.println("原链表:");
l.print();
String searchNode = "B";
System.out.println("查找节点:" + searchNode);
String result = l.searchNode(searchNode)?"找到!":"没找到!";
System.out.println("查找结果:" + result);
System.out.println("删除节点:" + searchNode);
l.deleteNode(searchNode);
System.out.println("删除节点后的链表:");
l.print(); } }

  测试结果如下:

原链表:
A-->B-->C-->D
查找节点:B
查找结果:找到!
删除节点:B
删除节点后的链表:
A-->C-->D

原地址  

 二、双向链表的简单实现

public class DoubleLink<T> {

	/**
* Node<AnyType>类定义了双向链表中节点的结构,它是一个私有类, 而其属性和构造函数都是公有的,这样,其父类可以直接访问其属性
* 而外部类根本不知道Node类的存在。
*
* @author ZHB
*
* @param <T>
* 类型
* @param Data
* 是节点中的数据
* @param pre
* 指向前一个Node节点
* @param next
* 指向后一个Node节点
*/
private class Node<T> {
public Node<T> pre;
public Node<T> next;
public T data; public Node(T data, Node<T> pre, Node<T> next) {
this.data = data;
this.pre = pre;
this.next = next;
} public Node() {
this.data = null;
this.pre = null;
this.next = null;
}
} // 下面是DoubleLinkedList类的数据成员和方法
private int theSize;
private Node<T> Header;
private Node<T> Tail; /*
* 构造函数 我们构造了一个带有头、尾节点的双向链表 头节点的Next指向尾节点 为节点的pre指向头节点 链表长度起始为0。
*/
public DoubleLink() { theSize = 0;
Header = new Node<T>(null, null, null);
Tail = new Node<T>(null, Header, null); Header.next = Tail;
} public void add(T item) { Node<T> aNode = new Node<T>(item, null, null); Tail.pre.next = aNode;
aNode.pre = Tail.pre;
aNode.next = Tail;
Tail.pre = aNode; theSize++;
} public boolean isEmpty() {
return (this.theSize == 0);
} public int size() {
return this.theSize;
} public T getInt(int index) { if (index > this.theSize - 1 || index < 0)
throw new IndexOutOfBoundsException(); Node<T> current = Header.next; for (int i = 0; i < index; i++) {
current = current.next;
} return current.data;
} public void print() { Node<T> current = Header.next; while (current.next != null) { System.out.println(current.data.toString()); current = current.next;
} } public static void main(String[] args) {
DoubleLink<String> dLink = new DoubleLink<String>(); dLink.add("zhb");
dLink.add("zzb");
dLink.add("zmy");
dLink.add("zzj"); System.out.println("size : " + dLink.size());
System.out.println("isEmpty? : " + dLink.isEmpty());
System.out.println("3 : " + dLink.getInt(2));
dLink.print();
}
}

  原文地址

JAVA单向/双向链表的实现的更多相关文章

  1. JAVA实现双向链表的增删功能

    JAVA实现双向链表的增删功能,完整代码 package linked; class LinkedTable{ } public class LinkedTableTest { //构造单链表 sta ...

  2. Java简单双向链表实现 @version 1.0

    package com.list; /** * 数据结构和算法Java表示 双向链表 * * @version 1.0 * @author 小明 * */ public class MyDoublel ...

  3. JAVA单向链表实现

    JAVA单向链表实现 单向链表 链表和数组一样是一种最常用的线性数据结构,两者各有优缺点.数组我们知道是在内存上的一块连续的空间构成,所以其元素访问可以通过下标进行,随机访问速度很快,但数组也有其缺点 ...

  4. Java 单向链表学习

    Java 单向链表学习 链表等同于动态的数组:可以不同设定固定的空间,根据需要的内容动态的改变链表的占用空间和动态的数组同一形式:链表的使用可以更加便于操作. 链表的基本结构包括:链表工具类和节点类, ...

  5. java单向加密算法小结(2)--MD5哈希算法

    上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...

  6. 大话数据结构(八)Java程序——双向链表的实现

    线性链表--双向链表 双向链表定义: 双向链表(double linked list): 是在单表单的每个结点中,再设置一个指向前驱结点的指针域.因此,在双向链表中的结点都有两个指针域,一个指向前驱, ...

  7. Java中双向链表的代码实现

    写在前面: 双向链表是一种对称结构,它克服了单链表上指针单向性的缺点,其中每一个节点即可向前引用,也可向后引用,这样可以更方便的插入.删除数据元素. 由于双向链表需要同时维护两个方向的指针,因此添加节 ...

  8. JAVA 单向链表

    package com.session.link; /** * 单向链表 */public class LinkedList<T> { private Node head;//指向链表头节 ...

  9. Java数据结构--双向链表的实现

    #java学习经验总结------双向链表的实现 双向链表的建立与单链表类似,只是需要使用pre指针指向前一个结点,并且在删除添加时不仅仅考虑next package datastructure; p ...

随机推荐

  1. django中“url映射规则”和“服务端响应顺序”

    1.django搜索路径 使用 import 语句时,Python 所查找的系统目录清单.      查看方式:         import sys        print sys.path   ...

  2. iOS 启动图那些坑

    当我们按照图片尺寸要求将所有的图片添加到工程中后,上传打包的工程时可能会出现一个问题:说工程中不存在启动图.但是我们明明已经导入启动图了,那么问题出在哪呢.我经过多次试验,发现压缩过后的图片作为启动图 ...

  3. Port Hacking

    Port information21 ftp 主要看是否支持匿名,也可以跑弱口令22 ssh23 telnet79 Finger80 web 常见web漏洞以及是否为一些管理后台111 rpcinfo ...

  4. javascript new

    1. 仅function可以使用new 2. function使用new时,会拷贝function中this的内容给新对象,并将function的prototype指向新对象(如果该function没 ...

  5. WC2015 酱油记

    这是真·酱油记! Day0 因为我们在上海,所以只要坐高铁就可以了2333.到了火车站以后我们坐大巴到学军中学恩,结果坐大巴的时间和做坐高铁的时间差不做←_←. 吐槽了一下住宿环境和课程表就已经晚上了 ...

  6. guacamole 0.8.3 项目部署 桌面虚拟化

    Guacamole是一个基于HTML5的虚拟桌面应用程序,其中包含多个组件,由各组件共同构成Guacamole---一个完整的虚拟桌面解决方案,不需要任何插件,只要浏览器支持HTML5就可以实现,而且 ...

  7. curl请求的时候总是提示400

    今天用curl测试一个接口,一直提示400 最后发现是url的问题,如下处理就可以了 $url = str_replace(' ', '+', $url);

  8. Linux-Big-Endian和Little-Endian转换

    转自:http://blog.csdn.net/aklixiaoyao/article/details/7548860 在各种计算机体系结构中,对于字节.字等的存储机制有所不同,因而引发了计算机通信领 ...

  9. 删除IE 下输入后的清除小叉叉

    input::-ms-clear { display: none; } css去掉ie10,11中input[type="text"]后面的X图标 input[type=" ...

  10. Ubuntu 14.04中Mysql中文乱码问题最小化解决

    [client]default-character-set=utf8 [mysqld]default-storage-engine=INNODBcharacter-set-server=utf8col ...