java实现双链表(差点没写吐系列...)
刚才把单链表写完了,现在又把双链表写了,双链表和单链表的区别就是每个节点有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实现双链表(差点没写吐系列...)的更多相关文章
- 数组、单链表和双链表介绍 以及 双向链表的C/C++/Java实现
概要 线性表是一种线性结构,它是具有相同类型的n(n≥0)个数据元素组成的有限序列.本章先介绍线性表的几个基本组成部分:数组.单向链表.双向链表:随后给出双向链表的C.C++和Java三种语言的实现. ...
- JAVA 链表操作:单链表和双链表
主要讲述几点: 一.链表的简介 二.链表实现原理和必要性 三.单链表示例 四.双链表示例 一.链表的简介 链表是一种比较常用的数据结构,链表虽然保存比较复杂,但是在查询时候比较便捷,在多种计算机语言都 ...
- 图解双链表(Java实现)
原创公众号:bigsai 文章已收录在 全网都在关注的数据结构与算法学习仓库 前言 前面有很详细的讲过线性表(顺序表和链表),当时讲的链表以但链表为主,但实际上在实际应用中双链表的应用多一些就比如Li ...
- XObject.java 对象还没写完,希望电脑不会丢失。坏笑,早点见。
/*面向对象强调的是对象, 面向过程强调的是功能行为,打开行为,关闭行为,执行行为,把多个行为封装成对象执行更强大的功能就是面向对象,是把多个函数, 多 个行为封装在一起,单一的函数执行对象的功能太困 ...
- 双链表算法原理【Java实现】(八)
前言 前面两节内容我们详细介绍了ArrayList,一是手写实现ArrayList数据结构,而是通过分析ArrayList源码看看内置实现,关于集合内容一如既往,本节课我们继续学习集合LinkedLi ...
- Word 双栏排版最后多一页空白页删不掉、左栏文字没写完就到右栏了
1. 问题 问题:Word双栏排版,最后多一页空白页,删不掉.如图: 原因分析:删不掉是因为末尾文字处其实有个下一页分节符,只不过可能看不到. 如何清晰的看到? 视图 > 大纲,就可以看到了.如 ...
- Java双链表
一.概述 二.英雄类 class HeroNode { //值域 public int id; public String name; public String nickName; //指针域 pu ...
- JAVA容器-模拟LinkedList实现(双链表)
概述 LinkedList实质上就是双向链表的拓展的实现,我们将关注一下问题.LinkedList 1.双向链表怎么来实现插入.删除.查询? 2.利用二分法提高查询效率. 3.不同步,线程不安全,需要 ...
- 再谈LRU双链表内存管理
N年前我写了个双链表也发了博客,还添了代码.但是那个代码不但复杂,而且还有有问题的,一直懒得整理,放在空间误导别人.最近在写服务端,今天抽点空补一篇. 关于LRU网上随便搜,有过后端经验的人应该很多都 ...
随机推荐
- 大叔也学Xamarin系列
回到占占推荐博客索引 我就是我,请叫我仓储大叔 大叔听很多客户说,xamarin的资料网上太少了,是的,大叔也相信,因为大叔在学xamarin里确实很费劲,只能看看androd for java了,呵 ...
- java中集合类中Collection接口中的List接口的常用方法熟悉
1:集合类,在java语言中的java.util包提供了一些集合类,这些集合类又被称作容器. 2:区别集合类和数组.(1)数组的长度是固定的,集合的长度是可变的.(2)数组是用来存放基本数据类型的,集 ...
- Android笔记——判断程序是否第一次启动
public class Welcome extends Activity { private final long SPLASH_LENGTH = 2000; Handler handler = n ...
- OPEN CASCADE Curve Continuity
OPEN CASCADE Curve Continuity eryar@163.com Abstract. 设计一条复杂曲线时,出于设计和制造上的考虑,常常通过多段曲线组合而成,这就需要解决曲线段之间 ...
- Minor【 PHP框架】4.服务容器与服务提供者
框架Github地址:github.com/Orlion/Minor (如果觉得还不错给个star哦(^-^)V) 框架作者: Orlion 知乎:https://www.zhihu.com/peop ...
- 了解HTML锚点
概念 <a>元素 (或HTML锚元素, Anchor Element)通常用来表示一个锚点/链接.但严格来说,<a>元素不是一个链接,而是超文本锚点,可以链接到一个新文件.用i ...
- javascript运动系列第七篇——鼠标跟随运动
× 目录 [1]眼球转动 [2]苹果菜单[3]方向跟随 前面的话 运动除了直线运动和曲线运动两种运动形式外,还有一种运动形式是鼠标跟随运动,而这种跟随运动需要用到三角函数的相关内容或者需要进行比例运算 ...
- 理解javascript中的浏览器窗口——窗口基本操作
× 目录 [1]窗口位置 [2]窗口大小 [3]打开窗口[4]关闭窗口 前面的话 BOM全称是brower object model(浏览器对象模型),主要用于管理窗口及窗口间的通讯,其核心对象是wi ...
- 趣味GPS
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 简介 GPS的全称是全球定位系统(the Global Positioning S ...
- 应用程序框架实战三十:表现层及ASP.NET MVC介绍(一)
本文将介绍表现层及ASP.NET MVC的一些要点,特别是ASP.NET MVC的一些抽象和封装技巧,如果你对MVC还不了解,可以参考<ASP.NET MVC4 高级编程>,作者Jon G ...