单向链表

package com.ywx.link;
/**
* 单向链表
* @author vashon
*
*/
public class LinkTest {
public static void main(String[] args) {
Link l=new Link();
l.addNode("A");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
System.out.println("==========增加之后的内容==========");
l.printNode();
System.out.println("\n包含D:"+l.contains("D"));
System.out.println("==========删除之前的内容==========");
l.deleteNode("A");
System.out.println("==========删除之后的内容==========");
l.printNode();
}
}
class Link{//链表的完成类
class Node{//保存每个节点
private String data;//节点内容
private Node next;//下一个节点
public Node(String data){
this.data=data;
}
public void add(Node newNode) {//将节点加入到合适的位置
if(this.next==null){
this.next=newNode;
}else{
this.next.add(newNode);
}
}
public void print() {//输出节点的内容
System.out.print(this.data+"\t");
if(this.next!=null){
this.next.print();//递归调用输出
}
}
public boolean search(String data){//内部搜索的方法
if(data.equals(this.data)){
return true;
}else{
if(this.next!=null){//向下继续判断
return this.next.search(data);
}else{
return false;
}
}
}
public void delete(Node previous, String data) {
if(data.equals(this.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(root==null){
root=newNode;
}else{
root.add(newNode);
}
}
public void printNode(){//链表的输出
if(root!=null){
root.print();
}
}
public boolean contains(String name){//判断元素是否存在
return this.root.search(name);
}
public void deleteNode(String data){//链表删除节点
if(this.contains(data)){
if(this.root.data.equals(data)){//如果是根节点
this.root=this.root.next;//修改根节点
}else{
this.root.next.delete(root,data);//把下一个节点的前节点和要删除的节点内容一起传入
}
}
}
}

另:

一、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. django单表操作 增 删 改 查

    一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取出数据. 目的:通过classes(班 ...

  2. Java操作MongoDB:连接&增&删&改&查

    1.连接 ①方式一 MongoClientOptions.Builder builder = MongoClientOptions.builder(); //可以通过builder做各种详细配置 Mo ...

  3. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  4. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  5. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  6. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  7. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  8. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  9. python基础中的四大天王-增-删-改-查

    列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...

随机推荐

  1. Android-fragment的替换

    fragment的替换:是指一个Activity加载多个Fragment,当某些动作的时候在Activity替换Fragment显示: 昨天写的这几篇博客,Android-fragment简介-fra ...

  2. string 和String的区别

    string 是 System.String 的别名,习惯上,我们把字符串当作对象时(有值的对象实体),我们用string.而我们把它当类时(需要字符串类中定义的方法),我们用String,比如: s ...

  3. SQL 2012新分页方式

    --2012的OFFSET分页方式 (推荐使用 - 较为方便) select number from spt_values where type='p' order by number offset ...

  4. Netbeans8.1设置Consola字体并解决中文乱码问题

    netbeans是php非常好用的ide,并且还是免费的!但是好多字体不支持中文,会显示如下乱码: 解决方法如下: 通过修改jre的配置文件成功解决了这个问题. 1. 进入jdk安装目录下/jre/l ...

  5. ASP.NET MVC Areas View 引用 外部母版视图

    ASP.NET MVC Area => Areas View 引用 外部母版视图 创建项目:MVCSite.Area 创建mvc area 1.Areas View 引用 外部母版视图 1.1 ...

  6. The service definition selected is invalid

    吐槽下 最近在学Java 听闻Java生态很好 社区很多 但实际操作起来确实另一番风景 不多说了 说正事 添加WebService服务Client时有密码认证得服务 Eclipse抛出 The ser ...

  7. JEECG(一) 如何配置自己的业务包

    使用自己的业务包开发,不在com.jeecg业务包下 请首先阅读这篇文章[官网] http://www.jeecg.org/forum.php?mod=viewthread&tid=1832& ...

  8. TensorFlow从1到2(十)带注意力机制的神经网络机器翻译

    基本概念 机器翻译和语音识别是最早开展的两项人工智能研究.今天也取得了最显著的商业成果. 早先的机器翻译实际脱胎于电子词典,能力更擅长于词或者短语的翻译.那时候的翻译通常会将一句话打断为一系列的片段, ...

  9. FFmpeg编写的代码

    //初始化解封装    av_register_all();    avformat_network_init();    avcodec_register_all();    //封装文件的上下文  ...

  10. python使用venv环境报Python.h : No such file or direc

    这个是因为域名 install python36的时候没有按照python36-devel ,只需要 yum install python36-devel 然后重新安装所需模块即可.