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网上随便搜,有过后端经验的人应该很多都 ...
随机推荐
- Step by step Dynamics CRM 2011升级到Dynamics CRM 2013
原创地址:http://www.cnblogs.com/jfzhu/p/4018153.html 转载请注明出处 (一)检查Customizations 从2011升级到2013有一些legacy f ...
- [CORS:跨域资源共享] W3C的CORS Specification
随着Web开放的程度越来越高,通过浏览器跨域获取资源的需求已经变得非常普遍.在我看来,如果Web API不能针对浏览器提供跨域资源共享的能力,它甚至就不应该被称为Web API.从另一方面来看,浏览器 ...
- 获取bing.com的图片并在gnome3中设置自动切换
发现 bing.com 上的图片很好看,因此打算每天把 bing.com 的图片下载下来,用作桌面. 需要做的是两个部分,爬取图片到目录和设置目录图片为桌面背景并可以自动切换. 第一部分,下载图片,使 ...
- Android—自定义Dialog
在 Android 日常的开发中,Dialog 使用是比较广泛的.无论是提示一个提示语,还是确认信息,还是有一定交互的(弹出验证码,输入账号密码登录等等)对话框. 而我们去看一下原生的对话框,虽然随着 ...
- iOS-技巧性总结
1.AFN与ASI对比 -- AFN1. 基于 NSURLConnection & NSURLSession 进行的封装2. 使用简单3. 提供了自动的序列化 & 反序列化支持! AF ...
- Android Drawable 那些不为人知的高效用法
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43752383,本文出自:[张鸿洋的博客] 1.概述 Drawable在我们平时的 ...
- WaitType:ASYNC_NETWORK_IO
官方文档的定义,是指SQL Server 产生的结果集需要经过Network传递到Client,Network不能很快将结果集传输到Client,导致结果集仍然驻留在SQL Server的Sessio ...
- Entity Framework Code First执行SQL语句、视图及存储过程
1.Entity Framework Code First查询视图 Entity Framework Code First目前还没有特别针对View操作的方法,但对于可更新的视图,可以采用与Table ...
- Task C# 多线程和异步模型 TPL模型
Task,异步,多线程简单总结 1,如何把一个异步封装为Task异步 Task.Factory.FromAsync 对老的一些异步模型封装为Task TaskCompletionSource 更通用, ...
- 【原创】开源Math.NET基础数学类库使用(17)C#计算矩阵条件数
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 上个月 ...