刚才把单链表写完了,现在又把双链表写了,双链表和单链表的区别就是每个节点有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. PHP的pcntl多进程

    PHP使用PCNTL系列的函数也能做到多进程处理一个事务.比如我需要从数据库中获取80w条的数据,再做一系列后续的处理,这个时候,用单进程?你可以等到明年今天了...所以应该使用pcntl函数了. 假 ...

  2. 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)

    轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...

  3. C#设计模式-装饰者模式

    在软件开发中,我们经常想要对一类对象添加不同的功能,例如要给手机添加贴膜,手机挂件,手机外壳等,如果此时利用继承来实现的话,就需要定义无数的类,如StickerPhone(贴膜是手机类).Access ...

  4. slave IO流程之一:mysql登陆过程(mysql_real_connect)

    最近看了slave IO的源码,发现slave IO的写relay log貌似是单线程单连接的,这让我有点小失望. slave IO的主函数是handle_slave_io,处理流程如下: 图1 ha ...

  5. 【hbase0.96】基于hadoop搭建hbase的心得

    hbase是基于hadoop的hdfs框架做的分布式表格存储系统,所谓表格系统就是在k/v系统的基础上,对value部分支持column family和column,并支持多版本读写. hbase的工 ...

  6. WPF如何仿制QQ2013登录窗口的关闭效果

    昨天,有位朋友问我,WPF能做出像QQ2013窗口在关闭时那个貌似透明过渡的动画吗?我就歪着脸跟他说:"只有你想不到的,没有WPF做不到的". 他又接着说:"我知道肯定会 ...

  7. 【转】Linq Group by

    http://www.cnblogs.com/death029/archive/2011/07/23/2114877.html 1.简单形式: var q = from p in db.Product ...

  8. PHP浅复制与深复制

    原文链接:http://www.orlion.ga/731/ php用clone复制对象有一个问题,下面用代码来说明问题: class Foo{ public $bar; public $name; ...

  9. 链接(extern、static关键词\头文件\静态库\共享库)

    原文链接:http://www.orlion.ga/781/ 一. 多目标文件的链接 假设有两个文件:stack.c: /* stack.c */ char stack[512]; int top = ...

  10. AngularJS之中级Route【二】(七)

    前言 上一篇我们介绍了AngularJS内置的路由ngRoute,我们知道AngularJS被广泛应用于单页应用SPA(Single Page Application)中,此时路由对于我们来讲非常重要 ...