一、概述:

1、什么是双向链表:
链表中的每个节点即指向前面一个节点,也指向后面一个节点,就像丢手绢游戏一样,每个人都手拉手

2、从头部插入
要对链表进行判断,如果为空则设置尾节点为新添加的节点,如果不为空,还要设置头节点的一个前节点为新节点

3、从尾部进行插入
如果链表为空,则直接设置头节点为新添加的节点,否则设置尾节点的后一个节点为新添加的节点。同时设置新添加的节点的前一个节点为尾节点

4、从头部删除
判断节点是否有下个节点,如果没有则设置节点为null,并且删除下个节点指向前节点的指针

5、删除尾部节点
如果头节点没有其它节点,把尾节点设置为Null。否则设置尾节点前一个节点的next为Null。设置尾节点为前一个节点

6、删除方法
此时不需要记录last的Node
删除时使用current.previous.next= current.next;

二、实现:

package com.struct.linklist;

/**
* @描述 双向链表
* @项目名称 Java_DataStruct
* @包名 com.struct.linklist
* @类名 LinkList
* @author chenlin
* @date 2010年6月26日 上午8:00:28
* @version 1.0
*/ public class DoubleLinkList { //头
private Node first;
//尾
private Node last; public DoubleLinkList(){
first = null;
last = null;
} /**
* 插入数据
* @param value
*/
public void insertFirst(long value){
Node newNode = new Node(value);
if (first == null) {
last = newNode;
}else {
first.previous = newNode;
//把first节点往下移动
newNode.next = first;
}
//把插入的节点作为新的节点
first = newNode;
} /**
* 插入数据
* @param value
*/
public void insertLast(long value){
Node newNode = new Node(value);
if (first == null) {
first = newNode;
}else {
last.next = newNode;
//first.previous = newNode;
newNode.previous = last;
}
//把最后个节点设置为最新的节点
last = newNode;
} public boolean isEmpty(){
return first == null;
} /**
* 删除头节点时要去除两个指针,一个指向下个的next ,一个是next的previous指向前面的
*
* @param value
* @return
*/
public Node deleteFirst(){
if (first == null) {
throw new RuntimeException("链表数据不存在");
}
Node temp = first;
if (first.next == null) {
last = null;
}else {
first.next.previous = null;
}
first = temp.next;
return temp;
} /**
* 删除头节点时要去除两个指针,一个指向下个的next ,一个是next的previous指向前面的
*
* @param value
* @return
*/
public Node deleteLast(){
if (first == null) {
throw new RuntimeException("链表数据不存在");
} Node temp = last;
if (first.next == null) {
last = null;
//把第一个删除
first = null;
}else {
last.previous.next = null;
}
last = temp.previous;
return temp;
} /**
* 删除
* @param key
* @return
*/
public Node deleteByKey(long key){
Node current = first;
while(current.data != key){
if (current.next == null) {
System.out.println("没找到节点");
return null;
}
current = current.next;
}
if (current == first) {
//return deleteFirst();
//指向下个就表示删除第一个
first = first.next;
}else {
current.previous.next = current.next;
}
return current;
} /**
* 显示所有的数据
*/
public void display(){
if (first == null) {
//throw new RuntimeException("链表数据不存在");
return;
}
Node current = first;
while(current != null){
current.display();
current = current.next;
}
System.out.println("---------------");
} /**
* 查找节点1
* @param value
* @return
*/
public Node findByValue(long value){
Node current = first;
while(current != null){
if (current.data != value) {
current = current.next;
}else {
break;
}
}
if (current == null) {
System.out.println("没找到");
return null;
}
return current;
} /**
* 查找节点2
*
* @param key
* @return
*/
public Node findByKey(long key) {
Node current = first;
while (current.data != key) {
if (current.next == null) {
System.out.println("没找到");
return null;
}
current = current.next;
}
return current;
} /**
* 根据索引查找对应的值
* @param position
* @return
*/
public Node findByPosition(int position){
Node current = first;
//为什么是position - 1,因为要使用遍历,让current指向下一个, 所以position - 1的下个node就是要找的值
for (int i = 0; i < position - 1 ; i++) {
current = current.next;
}
return current;
} public static void main(String[] args) {
DoubleLinkList linkList = new DoubleLinkList();
linkList.insertFirst(21);
linkList.insertFirst(22);
linkList.insertFirst(23);
linkList.insertLast(24);
linkList.insertLast(25);
linkList.insertLast(26);
linkList.insertLast(27); linkList.display(); System.out.println("---查找-------------------------------------"); linkList.findByKey(25).display(); System.out.println("--删除first-------------------------------------"); //linkList.deleteFirst().display();
///linkList.deleteFirst().display();
//linkList.deleteFirst().display();
//linkList.deleteFirst().display(); System.out.println("-删除指定值---------------------------------------");
linkList.deleteByKey(27).display();
linkList.deleteByKey(21).display(); System.out.println("----------------------------------------");
linkList.display(); }
}

Java 数据结构之双向链表的更多相关文章

  1. JAVA数据结构--LinkedList双向链表

    链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括两个部分: ...

  2. java数据结构-05双向链表

    一.双向链式存储: ①简述:要是节点中包含两个指针部分,一个指向前驱元,一个指向后继元,Java中LinkedList集合类的实现就是双向链表 (以下图片为网络收集,侵删) ②特点:数据是非连续的,链 ...

  3. 图解Java数据结构之双向链表

    上一篇文章说到了单链表,也通过案例具体实现了一下,但是单链表的缺点也显而易见. 单向链表查找的方向只能是一个方向 单向链表不能自我删除,需要靠辅助节点 而双向链表则能够很轻松地实现上面的功能. 何为双 ...

  4. Java数据结构之双向链表

    管理单向链表的缺点分析: 单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找. 单向链表不能自我删除,需要靠辅助节点 ,而双向链表,则可以自我删除,所以前面我们单链表删除时节点,总是找 ...

  5. Java数据结构之线性表

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  6. (6)Java数据结构-- 转:JAVA常用数据结构及原理分析

    JAVA常用数据结构及原理分析  http://www.2cto.com/kf/201506/412305.html 前不久面试官让我说一下怎么理解java数据结构框架,之前也看过部分源码,balab ...

  7. Java数据结构和算法(一)线性结构

    Java数据结构和算法(一)线性结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 线性表 是一种逻辑结构,相同数据类型的 ...

  8. 一文掌握关于Java数据结构所有知识点(欢迎一起完善)

    在我们学习Java的时候,很多人会面临我不知道继续学什么或者面试会问什么的尴尬情况(我本人之前就很迷茫).所以,我决定通过这个开源平台来帮助一些有需要的人,通过下面的内容,你会掌握系统的Java学习以 ...

  9. Java数据结构和算法(四)--链表

    日常开发中,数组和集合使用的很多,而数组的无序插入和删除效率都是偏低的,这点在学习ArrayList源码的时候就知道了,因为需要把要 插入索引后面的所以元素全部后移一位. 而本文会详细讲解链表,可以解 ...

随机推荐

  1. webpack4学习笔记(二)

    webpack打包各种javascript文件 (本文只是提供一个学习记录,大部分内容来自网络) 一,打包js文件和es6代码 1,webpack命令打包js文件 Tip: 在webpack4.x之前 ...

  2. GraphicsMagick 1.3.25 Linux安装部署

    1.安装相关依赖包 yum install -y gcc libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-d ...

  3. puma 配置,启动脚本

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/menxu_work/article/details/24547923 配置: puma_server ...

  4. Python数据库连接池实例——PooledDB

    不用连接池的MySQL连接方法 import MySQLdb conn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='m ...

  5. repo使用

    repo常用指令: 1.repo init(下载repo并克隆manifest) repo init -u URL [OPTIONS] Options: -u:制定一个URL,其连接到一个manife ...

  6. Go实现查找目录下(包括子目录)替换文件内容

    [功能] 按指定的目录查找出文件,如果有子目录,子目录也将进行搜索,将其中的文件内容进行替换. [缺陷] 1. 没有过滤出文本文件 2. 当文件过大时,效率不高 [代码] package main i ...

  7. PAT 1077 Kuchiguse [一般]

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  8. jmeter接口测试实战

    请求方法:get/post 接口请求地址:http://172.22.24.26:8080/fundhouse/external/getdata?name=xxxx &fund_udid=35 ...

  9. 设置 Quick-Cocos2d-x 在 Windows 下的编译环境

    http://cn.cocos2d-x.org/tutorial/show?id=1304 设置 Quick-Cocos2d-x 在 Windows 下的编译环境 Liao Yulei2014-08- ...

  10. jar打包方法使用整理

    dos窗口下操作jar:(JDK的命令) jar命令能够把Java应用打包成一个文件,这个文件的扩展名为.jar,称为JAR文件.JAR 文件非常类似 ZIP 文件.准确的说,它就是 ZIP 文件,所 ...