刚才把单链表写完了,现在又把双链表写了,双链表和单链表的区别就是每个节点有prior和next两个指针,不同于单链表的一个next指针,而且,正是因为有这两个指针,所以双链表可以前后两个方向去移动指针,

同时,我所实现的双链表和单链表不同之处在于,主要体现在其不用每次都把指针从头结点开始遍历,而是根据实际情况从选择最优的线路去遍历,移动到想要的位置。差点写吐了....话不多说,上代码

  

 package com.voole.linkedlist;

 public class Test {
public static void main(String[] args) {
// LinkedList linkedList = new LinkedList();
// linkedList.insert(new Node(null, null));
// linkedList.insert(new Node(null, null));
// linkedList.insert(new Node(null, null));
// linkedList.insert(new Node(null, null));
// linkedList.insert(new Node(null, null));
// linkedList.insert(new Node(null, null),2);
// Node node = new Node(null, null);
// linkedList.update(node,2);
// System.out.println(linkedList.select(2));
// System.out.println(node);
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
doubleLinkedList.insert(new Node(null, null, null));
doubleLinkedList.insert(new Node(null, null, null));
doubleLinkedList.insert(new Node(null, null, null));
doubleLinkedList.insert(new Node(null, null, null));
doubleLinkedList.insert(new Node(null, null, null),3);
doubleLinkedList.select(2);
}
} package com.voole.linkedlist; /**
* 双链表(该链表的效率比单链表高,主要体现在其不用每次都把指针从头结点开始遍历,而是根据实际情况从选择最优的线路去遍历,移动到想要的位置)
* @author TMAC-J
*
*/
public class DoubleLinkedList {
/**
* 头结点
*/
private Node head = null;
/**
* 链表长度
*/
private int size = 0;
/**
* 定义指针
*/
private int point = 0;
/**
* 当前指针指向的节点
*/
private Node currentNode = null;
/**
* 构造方法
*/
public DoubleLinkedList(){
head = new Node(null, null, null);
}
/**
* 增(从末尾增加)
*/
public void insert(Node node){
if(size == 0){
head.next = node;
currentNode = node;
node.prior = head;
}
else{
while(point<size){
currentNode = currentNode.next;
point++;
}
currentNode.next = node;
node.prior = currentNode;
currentNode = node;
}
point++;
size++;
LinkedListLog.getInstance().insert();
}
/**
* 增(从任意位置添加)
*/
public void insert(Node node,int position){
checkPosition(position);
//如果指针离插入位置更近
if(Math.abs(position-point)<=position){
if((position-point)>0){
while(point<position-1){
currentNode = currentNode.next;
point++;
}
node.next = currentNode.next;
currentNode.next = node;
node.prior = currentNode;
currentNode = node;
}
else if((position-point)<=0){
while(point>position){
currentNode = currentNode.prior;
point--;
}
node.prior = currentNode.prior;
currentNode.prior = node;
node.next = currentNode;
currentNode = node;
}
}
//如果头结点离插入位置更近
else{
point = 0;
while(point<position-1){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
if(currentNode!=null){
node.next = currentNode.next;
currentNode.next = node;
node.prior = currentNode;
currentNode = node;
}
else{
head.next = node;
node.prior = head;
}
}
size++;
LinkedListLog.getInstance().insert();
}
/**
* 删(从末尾删除)
*/
public void delete(){
if(size == 0){
LinkedListLog.getInstance().error();
return;
}
while(point<size){
currentNode = currentNode.next;
point++;
}
currentNode = currentNode.prior;
currentNode.next = null;
point--;
size--;
LinkedListLog.getInstance().delete();
}
/**
* 删(任意位置删除)
*/
public void delete(int position){
checkPosition(position);
if(size == 0){
LinkedListLog.getInstance().error();
return;
}
//如果指针离插入位置更近
if(Math.abs(position-point)<=position){
if((position-point)>0){
while(point<position-1){
currentNode = currentNode.next;
point++;
}
try{
currentNode.next = currentNode.next.next;
currentNode.next.next.prior = currentNode;
}catch(Exception e){
/**
* 这里为了防止currentNode.next.next为空设定,
* 若以后发现currentNode.next.next为空的情况存在,这里在做一些措施
* 目前逻辑有点复杂,不想看了....等抛出再处理
*/
System.out.println("有参数为空!");
}
}
else if((position-point)<=0){
while(point>position){
currentNode = currentNode.prior;
point--;
}
try{
currentNode.next.prior = currentNode.prior.next;
currentNode.prior.next = currentNode.next.prior;
currentNode = currentNode.next;
}catch(Exception e){
/**
* 理由同上
*/
System.out.println("有参数为空!");
}
}
}
//如果头结点离插入位置更近
else{
point = 0;
while(point<position-1){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
try{
currentNode.next = currentNode.next.next;
currentNode.next.next.prior = currentNode;
}catch(Exception e){
/**
* 理由如上
*/
System.out.println("参数为空!");
}
}
size--;
LinkedListLog.getInstance().delete();
}
/**
* 改
*/
public void update(Node node,int position){
checkPosition(position);
if(size == 0){
LinkedListLog.getInstance().error();
return;
}
//如果指针离插入位置更近
if(Math.abs(position-point)<=position){
if((position-point)>0){
while(point<position-1){
currentNode = currentNode.next;
point++;
}
node.next = currentNode.next.next;
node.prior = currentNode;
currentNode.next.next.prior = node;
currentNode.next = node;
}
else if((position-point)<=0){
while(point>position){
currentNode = currentNode.prior;
point--;
}
node.next = currentNode.next;
node.prior = currentNode.prior;
currentNode.next.prior = node;
currentNode.prior.next = node;
currentNode = node;
}
}
//如果头结点离插入位置更近
else{
point = 0;
while(point<position-1){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
node.next = currentNode.next.next;
node.prior = currentNode;
currentNode.next.next.prior = node;
currentNode.next = node;
}
LinkedListLog.getInstance().update();
}
/**
* 查
*/
public Node select(int position){
checkPosition(position);
if(size == 0){
LinkedListLog.getInstance().error();
return null;
}
//如果指针离插入位置更近
if(Math.abs(position-point)<=position){
if((position-point)>0){
while(point<position-1){
currentNode = currentNode.next;
point++;
}
LinkedListLog.getInstance().select();
return currentNode.next;
}
else if((position-point)<=0){
while(point>position){
currentNode = currentNode.prior;
point--;
}
LinkedListLog.getInstance().select();
return currentNode;
}
}
//如果头结点离插入位置更近
else{
point = 0;
while(point<position-1){
if(point == 0){
currentNode = head.next;
}
else{
currentNode = currentNode.next;
}
point++;
}
LinkedListLog.getInstance().select();
return currentNode.next;
}
LinkedListLog.getInstance().error();
return null;
}
/**
* 检查位置是否正确
*/
public void checkPosition(int position){
if(position>size+1||position<=0){
LinkedListLog.getInstance().error();
return;
}
}
} package com.voole.linkedlist;
/**
* @description 链表节点
* @author TMAC-J
*
*/
public class Node {
/**
* 定义下一个指针
*/
public Node next = null;
/**
* 定义上一个指针
*/
public Node prior = null;
/**
* 定义数据域
*/
public Data data = null;
/**
* @description 构造方法
*/
public Node(Node prior,Node next,Data data){
this.prior = prior;
this.next = next;
this.data = data;
}
} package com.voole.linkedlist; import java.io.Serializable; public class Data implements Serializable{ private static final long serialVersionUID = 1L; } package com.voole.linkedlist;
/**
* 单例日志类(饿汉)
* @author TMAC-J
*
*/
public class LinkedListLog {
private static final LinkedListLog instance = new LinkedListLog(); private LinkedListLog(){} public static LinkedListLog getInstance(){
return instance;
} public void insert(){
System.out.println("插入成功!");
} public void delete(){
System.out.println("删除成功!");
} public void update(){
System.out.println("修改成功!");
} public void select(){
System.out.println("查询成功!");
} public void error(){
System.out.println("错误!");
}
}

  可能还会有一些小bug,以后碰到的时候再改吧,先写到这了。

java实现双链表(差点没写吐系列...)的更多相关文章

  1. 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现

    概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的实现. ...

  2. JAVA 链表操作:单链表和双链表

    主要讲述几点: 一.链表的简介 二.链表实现原理和必要性 三.单链表示例 四.双链表示例 一.链表的简介 链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都 ...

  3. 图解双链表(Java实现)

    原创公众号:bigsai 文章已收录在 全网都在关注的数据结构与算法学习仓库 前言 前面有很详细的讲过线性表(顺序表和链表),当时讲的链表以但链表为主,但实际上在实际应用中双链表的应用多一些就比如Li ...

  4. XObject.java 对象还没写完,希望电脑不会丢失。坏笑,早点见。

    /*面向对象强调的是对象, 面向过程强调的是功能行为,打开行为,关闭行为,执行行为,把多个行为封装成对象执行更强大的功能就是面向对象,是把多个函数, 多 个行为封装在一起,单一的函数执行对象的功能太困 ...

  5. 双链表算法原理【Java实现】(八)

    前言 前面两节内容我们详细介绍了ArrayList,一是手写实现ArrayList数据结构,而是通过分析ArrayList源码看看内置实现,关于集合内容一如既往,本节课我们继续学习集合LinkedLi ...

  6. Word 双栏排版最后多一页空白页删不掉、左栏文字没写完就到右栏了

    1. 问题 问题:Word双栏排版,最后多一页空白页,删不掉.如图: 原因分析:删不掉是因为末尾文字处其实有个下一页分节符,只不过可能看不到. 如何清晰的看到? 视图 > 大纲,就可以看到了.如 ...

  7. Java双链表

    一.概述 二.英雄类 class HeroNode { //值域 public int id; public String name; public String nickName; //指针域 pu ...

  8. JAVA容器-模拟LinkedList实现(双链表)

    概述 LinkedList实质上就是双向链表的拓展的实现,我们将关注一下问题.LinkedList 1.双向链表怎么来实现插入.删除.查询? 2.利用二分法提高查询效率. 3.不同步,线程不安全,需要 ...

  9. 再谈LRU双链表内存管理

    N年前我写了个双链表也发了博客,还添了代码.但是那个代码不但复杂,而且还有有问题的,一直懒得整理,放在空间误导别人.最近在写服务端,今天抽点空补一篇. 关于LRU网上随便搜,有过后端经验的人应该很多都 ...

随机推荐

  1. Step by step 如何创建一个新森林

    原创地址:http://www.cnblogs.com/jfzhu/p/4006118.html 转载请注明出处 创建一个新森林就是在一台计算机上安装AD DS,并将这台计算机提升为域控制器. 演示环 ...

  2. 《Entity Framework 6 Recipes》中文翻译系列 (18) -----第三章 查询之结果集扁平化和多属性分组

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 3-14  结果集扁平化 问题 你有一对多关联的两个实体,你想通过一个查询,获取关联 ...

  3. Rxjava异常处理

    异常处理 在Rxjava订阅的Observable有时会抛出异常,在RxJava中有两大类策略,一个是准备备用的Observable,在发生异常时将subscriber订阅到新的Observable上 ...

  4. 解决OracleConnection ORA-1017 和 HRESULT:0x8007000B 错误

    试图加载格式不正确的程序. (异常来自HRESULT:0x8007000B) 解决方案: IIS下 winform下: ORA-1017 错误

  5. fir.im Log Guru 正式开源,快速找到 iOS 应用无法安装的原因

    很开心的宣布 Log Guru 正式开源! Log Guru,是 fir.im 开发团队创造的小轮子,用在 Mac 电脑上的日志获取,Github 地址:FIRHQ/LogGuru. Log Guru ...

  6. Text文档编码识别方法

    Text文档编码识别方法 在做文档读取的时候,时常碰到编码格式不正确的问题,而要怎么样正确识别文档的编码格式,成了很多程序员的一块心病,今天我就要试着治好这块心病,这段代码的浓缩来自上千万文档的数据分 ...

  7. HTML5的实用

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #ffffff } p.p2 ...

  8. UIViewController 生命周期

    创建: 1. alloc 创建对象,分配空间 2.init (initWithNibName) 初始化对象,初始化数据 3.loadView 从nib载入视图 ,通常这一步不需要去干涉.除非你没有使用 ...

  9. PDMS RvmTranslator

    PDMS RvmTranslator eryar@163.com Abstract. AVEVA Review is used for 3D model visualisation for plant ...

  10. 详解Java中ArrayList、Vector、LinkedList三者的异同点(转)

    本文转自http://my.oschina.net/zzw922cn/blog/491631 一.ArrayList ArrayList是一个可以处理变长数组的类型,这里不局限于“数”组,ArrayL ...