刚才把单链表写完了,现在又把双链表写了,双链表和单链表的区别就是每个节点有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. [公告]Senparc.Weixin.MP v14.2.1 升级说明

    在Senparc.Weixin.MP v14.2.1中,所有Senparc.Weixin.MP下的Container,命名空间已经从 Senparc.Weixin.MP.CommonAPIs 改为了  ...

  2. sublime3 插件

    Sublime Text 3能用支持的插件推荐 从二月份用测试版本build 3012开始用sublime text 3,虽然很多插件在sublime text 3不工作了,因为sublime tex ...

  3. 携程App的网络性能优化实践

    首先介绍一下携程App的网络服务架构.由于携程业务众多,开发资源导致无法全部使用Native来实现业务逻辑,因此有相当一部分频道基于Hybrid实现.网络通讯属于基础&业务框架层中基础设施的一 ...

  4. 负载均衡session会话保持方法

    负载均衡时,为了保证同一用户session会被分配到同一台服务器上,可以使用以下方法:1.使用cookie将用户的session存入cookie里,当用户分配到不同的服务器时,先判断服务器是否存在该用 ...

  5. AngularJS表单验证实现方法详解

    本文主要是通过源码实例和大家分享AngularJS中的表单验证相关知识,希望通过本文的分享,对大家学习AngularJS有所帮助. 1.常规表单验证: 2.AngularJs中提供的表单验证实例. 实 ...

  6. 修改Credentials 密码

    今天,Leader 吩咐要修改管理账户的密码,我负责的Part是修改package和 Replication的Job的密码.仔细想了下,由于我们使用的Windows验证方式,而Job在执行时,是使用P ...

  7. Panorama和Pivot的区别

    Panorama 1.提供了更丰富的用户体验(建议最多4个Items项) 2.Item可以设置屏幕方向为水平,支持多于一个屏幕的显示 3.可以使用任意大小的背景图片,Panorama会自动地缩放为屏幕 ...

  8. Xdebug+ZendStudio配置

    原文链接:http://www.orlion.ga/689/ 好久之前就知道有这么个东西,但是一直没用,一直用exit().var_dump() debug,效率很低. 首先下载xdebug的dll文 ...

  9. ASP.NET MVC5 网站开发实践(二) Member区域 - 用户部分(1)用户注册

    上一次把基本框架搭建起来了,这次开始整Web部分,终于可以看到界面了小激动一下.web项目部分从用户功能开始,基本有注册,登录.注销.查找.查看.删除等涉及Member区域和Manage区域. 目录: ...

  10. 延时调用--deferred.js原码分析

    有些时候,我们需要等待上一个操作完成之后,才能进行下一步的操作.比如Ajax实现自动提交表单操作的时候,程序需要等待,一旦有返回结果了,则继续进行一下步操作. 这时deferred.js这个库就产生了 ...